forked from Dhilip-Kumar-S/tmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtserver.go
114 lines (79 loc) · 1.35 KB
/
tserver.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
package main
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"net"
"runtime"
"time"
)
type MSG struct {
otype int32
mlen int32
m []byte
}
var tps uint64
func printstats() {
for {
select {
case <-time.After(1 * time.Second):
fmt.Printf("tps = %v\n", tps)
tps = 0
}
}
}
func DispStts(s chan int) {
go printstats()
for {
select {
case <-s:
tps++
}
}
}
func HandleConnection(c net.Conn, s chan int) {
for {
var v MSG
var m []byte
m = make([]byte, 40)
debug := false
cnt, err := io.ReadAtLeast(c, m, 40)
if err != nil {
fmt.Println("ReadAtleast :", err)
break
}
if cnt != 40 {
fmt.Println("Bad message len:", cnt)
break
}
if debug {
fmt.Printf("Recived %d bytes %v\n", cnt, err)
}
bf := bytes.NewBuffer(m)
binary.Read(bf, binary.BigEndian, &(v.otype))
binary.Read(bf, binary.BigEndian, &(v.mlen))
s <- 1
if debug {
fmt.Printf("Message Type=%d Mlen=%d %s\n", v.otype, v.mlen, string(m[8:cnt]))
}
}
c.Close()
}
func main() {
l, err := net.Listen("tcp", ":6060")
if err != nil {
fmt.Println("Listen Failed:", err)
panic("Server failed to bind:")
}
runtime.GOMAXPROCS(runtime.NumCPU())
s := make(chan int)
go DispStts(s)
for {
conn, err := l.Accept()
if err != nil {
fmt.Println("Accep failed:", err)
}
go HandleConnection(conn, s)
}
}