-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
46 lines (38 loc) · 897 Bytes
/
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
package easycall
import (
"net"
"strconv"
"sync"
"time"
"github.com/starjiang/elog"
)
const (
TCP_KEEPALIVE_PERIOD = 15
)
//Server for EasyService
type Server struct {
}
func (serv *Server) CreateServer(port int, handler PkgHandler) error {
listen, err := net.Listen("tcp", ":"+strconv.Itoa(port))
if err != nil {
elog.Error("listen error: ", err)
return err
}
go func() {
for {
conn, err := listen.Accept()
if err != nil {
elog.Error("accept error: ", err)
continue
}
tcpConn := conn.(*net.TCPConn)
tcpConn.SetKeepAlive(true)
tcpConn.SetKeepAlivePeriod(time.Minute * TCP_KEEPALIVE_PERIOD)
tcpConn.SetNoDelay(true)
client := &EasyConnection{conn: tcpConn, writeChan: make(chan []byte, EASYCALL_WRITE_QUEUE_SIZE), handler: handler, mutex: &sync.Mutex{}, activeTime: time.Now()}
go client.Read()
go client.Write()
}
}()
return nil
}