Skip to content

Commit

Permalink
change session id from uint32 to uint16
Browse files Browse the repository at this point in the history
  • Loading branch information
SwimmingTiger committed Nov 18, 2021
1 parent fc51278 commit 32c32e9
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 28 deletions.
6 changes: 3 additions & 3 deletions DownSession.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type DownSession struct {
manager *SessionManager // 会话管理器
upSession EventInterface // 所属的服务器会话

sessionID uint32 // 会话ID
sessionID uint16 // 会话ID
clientConn net.Conn // 到矿机的TCP连接
clientReader *bufio.Reader // 读取矿机发送的内容
readLoopRunning bool // TCP读循环是否在运行
Expand All @@ -33,7 +33,7 @@ type DownSession struct {
}

// NewDownSession 创建一个新的 Stratum 会话
func NewDownSession(manager *SessionManager, clientConn net.Conn, sessionID uint32) (down *DownSession) {
func NewDownSession(manager *SessionManager, clientConn net.Conn, sessionID uint16) (down *DownSession) {
down = new(DownSession)
down.manager = manager
down.sessionID = sessionID
Expand Down Expand Up @@ -273,7 +273,7 @@ func (down *DownSession) parseSubscribeRequest(request *JSONRPCLine) (result int
down.clientAgent, _ = request.Params[0].(string)
}

sessionIDString := Uint32ToHex(down.sessionID)
sessionIDString := Uint32ToHex(uint32(down.sessionID))

result = JSONRPCArray{JSONRPCArray{JSONRPCArray{"mining.set_difficulty", sessionIDString}, JSONRPCArray{"mining.notify", sessionIDString}}, sessionIDString, 4}
return
Expand Down
2 changes: 1 addition & 1 deletion Event.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type EventSendBytes struct {
}

type EventDownSessionBroken struct {
SessionID uint32
SessionID uint16
}

type EventUpSessionBroken struct {
Expand Down
10 changes: 5 additions & 5 deletions FakeUpSession.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

type FakeUpSession struct {
manager *UpSessionManager
downSessions map[uint32]*DownSession
downSessions map[uint16]*DownSession
eventChannel chan interface{}

fakeJob *StratumJob
Expand All @@ -18,7 +18,7 @@ type FakeUpSession struct {
func NewFakeUpSession(manager *UpSessionManager) (up *FakeUpSession) {
up = new(FakeUpSession)
up.manager = manager
up.downSessions = make(map[uint32]*DownSession)
up.downSessions = make(map[uint16]*DownSession)
up.eventChannel = make(chan interface{}, UpSessionChannelCache)
up.exitChannel = make(chan bool, 1)
return
Expand Down Expand Up @@ -55,7 +55,7 @@ func (up *FakeUpSession) transferDownSessions() {
up.manager.SendEvent(EventAddDownSession{down})
}
// 清空map
up.downSessions = make(map[uint32]*DownSession)
up.downSessions = make(map[uint16]*DownSession)
}

func (up *FakeUpSession) exit() {
Expand All @@ -68,7 +68,7 @@ func (up *FakeUpSession) exit() {
}
}

func (up *FakeUpSession) sendSubmitResponse(sessionID uint32, id interface{}, status StratumStatus) {
func (up *FakeUpSession) sendSubmitResponse(sessionID uint16, id interface{}, status StratumStatus) {
down, ok := up.downSessions[sessionID]
if !ok {
// 客户端已断开,忽略
Expand All @@ -79,7 +79,7 @@ func (up *FakeUpSession) sendSubmitResponse(sessionID uint32, id interface{}, st
}

func (up *FakeUpSession) handleSubmitShare(e EventSubmitShare) {
up.sendSubmitResponse(uint32(e.Message.Base.SessionID), e.ID, STATUS_ACCEPT)
up.sendSubmitResponse(e.Message.Base.SessionID, e.ID, STATUS_ACCEPT)
}

func (up *FakeUpSession) downSessionBroken(e EventDownSessionBroken) {
Expand Down
12 changes: 6 additions & 6 deletions SessionIDManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ type SessionIDManager struct {
lock sync.Mutex
sessionIDs *bitset.BitSet

count uint32 // how many ids are used now
allocIDx uint32
maxSessionId uint32 // sessionID可以达到的最大数值
count uint16 // how many ids are used now
allocIDx uint16
maxSessionId uint16 // sessionID可以达到的最大数值
}

// NewSessionIDManager 创建一个会话ID管理器实例
func NewSessionIDManager(maxSessionId uint32) (manager *SessionIDManager, err error) {
func NewSessionIDManager(maxSessionId uint16) (manager *SessionIDManager, err error) {
manager = new(SessionIDManager)

manager.maxSessionId = maxSessionId
Expand Down Expand Up @@ -51,7 +51,7 @@ func (manager *SessionIDManager) next() {
}

// AllocSessionID 为调用者分配一个会话ID
func (manager *SessionIDManager) AllocSessionID() (sessionID uint32, err error) {
func (manager *SessionIDManager) AllocSessionID() (sessionID uint16, err error) {
defer manager.lock.Unlock()
manager.lock.Lock()

Expand All @@ -77,7 +77,7 @@ func (manager *SessionIDManager) AllocSessionID() (sessionID uint32, err error)
}

// FreeSessionID 释放调用者持有的会话ID
func (manager *SessionIDManager) FreeSessionID(sessionID uint32) {
func (manager *SessionIDManager) FreeSessionID(sessionID uint16) {
defer manager.lock.Unlock()
manager.lock.Lock()

Expand Down
14 changes: 7 additions & 7 deletions SessionIDManager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestSessionIDManager16Bits(t *testing.T) {

// fill all session ids
{
var i uint32
var i uint16
for i = 0; i <= 0xfffe; i++ {
id, err := m.AllocSessionID()
if err != nil {
Expand All @@ -34,7 +34,7 @@ func TestSessionIDManager16Bits(t *testing.T) {

// free the first one
{
var id1, id2 uint32
var id1, id2 uint16
id1 = 0x0000
m.FreeSessionID(id1)
id2, err := m.AllocSessionID()
Expand All @@ -50,7 +50,7 @@ func TestSessionIDManager16Bits(t *testing.T) {

// free the second one
{
var id1, id2 uint32
var id1, id2 uint16
id1 = 0x0001
m.FreeSessionID(id1)
id2, err := m.AllocSessionID()
Expand All @@ -66,7 +66,7 @@ func TestSessionIDManager16Bits(t *testing.T) {

// free the one in the middle
{
var id1, id2 uint32
var id1, id2 uint16
id1 = 0x5024
m.FreeSessionID(id1)
id2, err := m.AllocSessionID()
Expand All @@ -82,7 +82,7 @@ func TestSessionIDManager16Bits(t *testing.T) {

// free the penult one
{
var id1, id2 uint32
var id1, id2 uint16
id1 = 0xfffd
m.FreeSessionID(id1)
id2, err := m.AllocSessionID()
Expand All @@ -98,7 +98,7 @@ func TestSessionIDManager16Bits(t *testing.T) {

// free the last one
{
var id1, id2 uint32
var id1, id2 uint16
id1 = 0x00fffe
m.FreeSessionID(id1)
id2, err := m.AllocSessionID()
Expand All @@ -114,7 +114,7 @@ func TestSessionIDManager16Bits(t *testing.T) {

// free all ids
{
var i uint32
var i uint16
for i = 0x0000; i <= 0xfffe; i++ {
m.FreeSessionID(i)
}
Expand Down
12 changes: 6 additions & 6 deletions UpSession.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type UpSession struct {
subAccount string
poolIndex int

downSessions map[uint32]*DownSession
downSessions map[uint16]*DownSession
serverConn net.Conn
serverReader *bufio.Reader
readLoopRunning bool
Expand Down Expand Up @@ -60,7 +60,7 @@ func NewUpSession(manager *UpSessionManager, config *Config, subAccount string,
up.slot = slot
up.subAccount = subAccount
up.poolIndex = poolIndex
up.downSessions = make(map[uint32]*DownSession)
up.downSessions = make(map[uint16]*DownSession)
up.stat = StatDisconnected
up.eventChannel = make(chan interface{}, UpSessionChannelCache)
up.submitIDs = make(map[uint16]SubmitID)
Expand Down Expand Up @@ -487,7 +487,7 @@ func (up *UpSession) recvJSONRPC(e EventRecvJSONRPC) {

func (up *UpSession) handleSubmitShare(e EventSubmitShare) {
if e.Message.IsFakeJob {
up.sendSubmitResponse(uint32(e.Message.Base.SessionID), e.ID, STATUS_ACCEPT)
up.sendSubmitResponse(e.Message.Base.SessionID, e.ID, STATUS_ACCEPT)
return
}

Expand All @@ -498,7 +498,7 @@ func (up *UpSession) handleSubmitShare(e EventSubmitShare) {
up.submitIDs[up.submitIndex] = SubmitID{e.ID, e.Message.Base.SessionID}
up.submitIndex++
} else {
up.sendSubmitResponse(uint32(e.Message.Base.SessionID), e.ID, STATUS_ACCEPT)
up.sendSubmitResponse(e.Message.Base.SessionID, e.ID, STATUS_ACCEPT)
}

if err != nil {
Expand All @@ -508,7 +508,7 @@ func (up *UpSession) handleSubmitShare(e EventSubmitShare) {
}
}

func (up *UpSession) sendSubmitResponse(sessionID uint32, id interface{}, status StratumStatus) {
func (up *UpSession) sendSubmitResponse(sessionID uint16, id interface{}, status StratumStatus) {
down, ok := up.downSessions[sessionID]
if !ok {
// 客户端已断开,忽略
Expand Down Expand Up @@ -538,7 +538,7 @@ func (up *UpSession) handleExMessageSubmitResponse(ex *ExMessage) {
}
delete(up.submitIDs, msg.Index)

up.sendSubmitResponse(uint32(submitID.SessionID), submitID.ID, msg.Status)
up.sendSubmitResponse(submitID.SessionID, submitID.ID, msg.Status)
}

func (up *UpSession) recvExMessage(e EventRecvExMessage) {
Expand Down

0 comments on commit 32c32e9

Please sign in to comment.