From 32c32e94e1584a0bd7b002f896f3acc4eace0212 Mon Sep 17 00:00:00 2001 From: SwimmingTiger Date: Thu, 18 Nov 2021 17:58:05 +0800 Subject: [PATCH] change session id from uint32 to uint16 --- DownSession.go | 6 +++--- Event.go | 2 +- FakeUpSession.go | 10 +++++----- SessionIDManager.go | 12 ++++++------ SessionIDManager_test.go | 14 +++++++------- UpSession.go | 12 ++++++------ 6 files changed, 28 insertions(+), 28 deletions(-) diff --git a/DownSession.go b/DownSession.go index cb90bb3..a1a155a 100644 --- a/DownSession.go +++ b/DownSession.go @@ -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读循环是否在运行 @@ -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 @@ -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 diff --git a/Event.go b/Event.go index b07adb0..4344c96 100644 --- a/Event.go +++ b/Event.go @@ -39,7 +39,7 @@ type EventSendBytes struct { } type EventDownSessionBroken struct { - SessionID uint32 + SessionID uint16 } type EventUpSessionBroken struct { diff --git a/FakeUpSession.go b/FakeUpSession.go index 0fa7f61..1b66165 100644 --- a/FakeUpSession.go +++ b/FakeUpSession.go @@ -8,7 +8,7 @@ import ( type FakeUpSession struct { manager *UpSessionManager - downSessions map[uint32]*DownSession + downSessions map[uint16]*DownSession eventChannel chan interface{} fakeJob *StratumJob @@ -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 @@ -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() { @@ -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 { // 客户端已断开,忽略 @@ -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) { diff --git a/SessionIDManager.go b/SessionIDManager.go index 798f189..b14530a 100644 --- a/SessionIDManager.go +++ b/SessionIDManager.go @@ -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 @@ -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() @@ -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() diff --git a/SessionIDManager_test.go b/SessionIDManager_test.go index 7326811..384f830 100644 --- a/SessionIDManager_test.go +++ b/SessionIDManager_test.go @@ -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 { @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() @@ -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) } diff --git a/UpSession.go b/UpSession.go index 97ca508..87251f1 100644 --- a/UpSession.go +++ b/UpSession.go @@ -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 @@ -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) @@ -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 } @@ -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 { @@ -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 { // 客户端已断开,忽略 @@ -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) {