Skip to content

Commit 32c32e9

Browse files
committed
change session id from uint32 to uint16
1 parent fc51278 commit 32c32e9

File tree

6 files changed

+28
-28
lines changed

6 files changed

+28
-28
lines changed

DownSession.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type DownSession struct {
1414
manager *SessionManager // 会话管理器
1515
upSession EventInterface // 所属的服务器会话
1616

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

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

276-
sessionIDString := Uint32ToHex(down.sessionID)
276+
sessionIDString := Uint32ToHex(uint32(down.sessionID))
277277

278278
result = JSONRPCArray{JSONRPCArray{JSONRPCArray{"mining.set_difficulty", sessionIDString}, JSONRPCArray{"mining.notify", sessionIDString}}, sessionIDString, 4}
279279
return

Event.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type EventSendBytes struct {
3939
}
4040

4141
type EventDownSessionBroken struct {
42-
SessionID uint32
42+
SessionID uint16
4343
}
4444

4545
type EventUpSessionBroken struct {

FakeUpSession.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
type FakeUpSession struct {
1010
manager *UpSessionManager
11-
downSessions map[uint32]*DownSession
11+
downSessions map[uint16]*DownSession
1212
eventChannel chan interface{}
1313

1414
fakeJob *StratumJob
@@ -18,7 +18,7 @@ type FakeUpSession struct {
1818
func NewFakeUpSession(manager *UpSessionManager) (up *FakeUpSession) {
1919
up = new(FakeUpSession)
2020
up.manager = manager
21-
up.downSessions = make(map[uint32]*DownSession)
21+
up.downSessions = make(map[uint16]*DownSession)
2222
up.eventChannel = make(chan interface{}, UpSessionChannelCache)
2323
up.exitChannel = make(chan bool, 1)
2424
return
@@ -55,7 +55,7 @@ func (up *FakeUpSession) transferDownSessions() {
5555
up.manager.SendEvent(EventAddDownSession{down})
5656
}
5757
// 清空map
58-
up.downSessions = make(map[uint32]*DownSession)
58+
up.downSessions = make(map[uint16]*DownSession)
5959
}
6060

6161
func (up *FakeUpSession) exit() {
@@ -68,7 +68,7 @@ func (up *FakeUpSession) exit() {
6868
}
6969
}
7070

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

8181
func (up *FakeUpSession) handleSubmitShare(e EventSubmitShare) {
82-
up.sendSubmitResponse(uint32(e.Message.Base.SessionID), e.ID, STATUS_ACCEPT)
82+
up.sendSubmitResponse(e.Message.Base.SessionID, e.ID, STATUS_ACCEPT)
8383
}
8484

8585
func (up *FakeUpSession) downSessionBroken(e EventDownSessionBroken) {

SessionIDManager.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ type SessionIDManager struct {
1313
lock sync.Mutex
1414
sessionIDs *bitset.BitSet
1515

16-
count uint32 // how many ids are used now
17-
allocIDx uint32
18-
maxSessionId uint32 // sessionID可以达到的最大数值
16+
count uint16 // how many ids are used now
17+
allocIDx uint16
18+
maxSessionId uint16 // sessionID可以达到的最大数值
1919
}
2020

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

2525
manager.maxSessionId = maxSessionId
@@ -51,7 +51,7 @@ func (manager *SessionIDManager) next() {
5151
}
5252

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

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

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

SessionIDManager_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func TestSessionIDManager16Bits(t *testing.T) {
1313

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

3535
// free the first one
3636
{
37-
var id1, id2 uint32
37+
var id1, id2 uint16
3838
id1 = 0x0000
3939
m.FreeSessionID(id1)
4040
id2, err := m.AllocSessionID()
@@ -50,7 +50,7 @@ func TestSessionIDManager16Bits(t *testing.T) {
5050

5151
// free the second one
5252
{
53-
var id1, id2 uint32
53+
var id1, id2 uint16
5454
id1 = 0x0001
5555
m.FreeSessionID(id1)
5656
id2, err := m.AllocSessionID()
@@ -66,7 +66,7 @@ func TestSessionIDManager16Bits(t *testing.T) {
6666

6767
// free the one in the middle
6868
{
69-
var id1, id2 uint32
69+
var id1, id2 uint16
7070
id1 = 0x5024
7171
m.FreeSessionID(id1)
7272
id2, err := m.AllocSessionID()
@@ -82,7 +82,7 @@ func TestSessionIDManager16Bits(t *testing.T) {
8282

8383
// free the penult one
8484
{
85-
var id1, id2 uint32
85+
var id1, id2 uint16
8686
id1 = 0xfffd
8787
m.FreeSessionID(id1)
8888
id2, err := m.AllocSessionID()
@@ -98,7 +98,7 @@ func TestSessionIDManager16Bits(t *testing.T) {
9898

9999
// free the last one
100100
{
101-
var id1, id2 uint32
101+
var id1, id2 uint16
102102
id1 = 0x00fffe
103103
m.FreeSessionID(id1)
104104
id2, err := m.AllocSessionID()
@@ -114,7 +114,7 @@ func TestSessionIDManager16Bits(t *testing.T) {
114114

115115
// free all ids
116116
{
117-
var i uint32
117+
var i uint16
118118
for i = 0x0000; i <= 0xfffe; i++ {
119119
m.FreeSessionID(i)
120120
}

UpSession.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type UpSession struct {
2626
subAccount string
2727
poolIndex int
2828

29-
downSessions map[uint32]*DownSession
29+
downSessions map[uint16]*DownSession
3030
serverConn net.Conn
3131
serverReader *bufio.Reader
3232
readLoopRunning bool
@@ -60,7 +60,7 @@ func NewUpSession(manager *UpSessionManager, config *Config, subAccount string,
6060
up.slot = slot
6161
up.subAccount = subAccount
6262
up.poolIndex = poolIndex
63-
up.downSessions = make(map[uint32]*DownSession)
63+
up.downSessions = make(map[uint16]*DownSession)
6464
up.stat = StatDisconnected
6565
up.eventChannel = make(chan interface{}, UpSessionChannelCache)
6666
up.submitIDs = make(map[uint16]SubmitID)
@@ -487,7 +487,7 @@ func (up *UpSession) recvJSONRPC(e EventRecvJSONRPC) {
487487

488488
func (up *UpSession) handleSubmitShare(e EventSubmitShare) {
489489
if e.Message.IsFakeJob {
490-
up.sendSubmitResponse(uint32(e.Message.Base.SessionID), e.ID, STATUS_ACCEPT)
490+
up.sendSubmitResponse(e.Message.Base.SessionID, e.ID, STATUS_ACCEPT)
491491
return
492492
}
493493

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

504504
if err != nil {
@@ -508,7 +508,7 @@ func (up *UpSession) handleSubmitShare(e EventSubmitShare) {
508508
}
509509
}
510510

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

541-
up.sendSubmitResponse(uint32(submitID.SessionID), submitID.ID, msg.Status)
541+
up.sendSubmitResponse(submitID.SessionID, submitID.ID, msg.Status)
542542
}
543543

544544
func (up *UpSession) recvExMessage(e EventRecvExMessage) {

0 commit comments

Comments
 (0)