forked from hacash/miner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
129 lines (98 loc) · 2.89 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package minerpool
import (
"bytes"
"fmt"
"github.com/hacash/core/fields"
"github.com/hacash/miner/message"
"net"
"sync/atomic"
"time"
)
// Listening port
func (p *MinerPool) startServerListen() error {
port := p.Config.TcpListenPort
listen := net.TCPAddr{IP: net.IPv4zero, Port: port, Zone: ""}
server, err := net.ListenTCP("tcp", &listen)
if err != nil {
return err
}
fmt.Printf("[Miner Pool] Start server and listen on port %d.\n", port)
go func() {
defer server.Close()
for {
conn, err := server.AcceptTCP()
if err != nil {
continue
}
go p.acceptConn(conn)
}
}()
return nil
}
func (p *MinerPool) acceptConn(conn *net.TCPConn) {
defer conn.Close()
if p.currentTcpConnectingCount > int32(p.Config.TcpConnectMaxSize) {
conn.Write([]byte("too_many_connect"))
return
}
// If the block has not been excavated, it will be closed and connected again after a period of time
if p.currentRealtimePeriod == nil || p.currentRealtimePeriod.targetBlock == nil {
conn.Write([]byte("not_ready_yet"))
return
}
// create client
client := NewClient(nil, conn)
atomic.AddInt32(&p.currentTcpConnectingCount, 1)
defer atomic.AddInt32(&p.currentTcpConnectingCount, -1) // subtraction
go func() {
time.Sleep(time.Second * 17)
if client.address == nil {
conn.Close() // err end
}
}()
// read msg
segdata := make([]byte, 2048)
for {
readbuf := bytes.NewBuffer([]byte{})
READNEXTBUF:
rn, err := conn.Read(segdata)
if err != nil {
break
}
readbuf.Write(segdata[0:rn])
newbytes := readbuf.Bytes()
//fmt.Println("MinerPool: rn, err := conn.Read(segdata)", segdata[0:rn])
if len(newbytes) == 4+5 && string(newbytes[0:4]) == "ping" {
if client.belongAccount.realtimePeriod.IsOverEndBlock(newbytes[4:]) {
conn.Write([]byte("end_current_mining")) // return end mining
} else {
conn.Write([]byte("pong")) // ok pong
}
} else if len(newbytes) == 21 { // post address
client.address = fields.Address(newbytes[0:21])
// fmt.Println( client.address.ToReadable() )
account := p.loadAccountAndAddPeriodByAddress(client.address)
//fmt.Println("account.activeClients.Add(client) // add")
account.activeClients.Add(client) // add
client.belongAccount = account // set belong
// send mining stuff
client.belongAccount.realtimePeriod.sendMiningStuffMsg(client)
} else if len(newbytes) == message.PowMasterMsgSize && client.belongAccount != nil {
//fmt.Println( "message.PowMasterMsgSize", segdata[0:rn] )
powresult := message.NewPowMasterMsg()
_, e := powresult.Parse(newbytes, 0)
if e != nil {
// Parsing error, do nothing
continue
}
client.postPowResult(powresult) // return pow results
} else {
goto READNEXTBUF
}
}
// end
//fmt.Println("client.belongAccount.activeClients.Remove(client)")
if client.belongAccount != nil {
client.belongAccount.activeClients.Remove(client) // remove
}
}