forked from tbrandon/mbserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
servetcp.go
81 lines (75 loc) · 1.87 KB
/
servetcp.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
package mbserver
import (
"crypto/tls"
"io"
"net"
"strings"
)
func (s *Server) accept(listen net.Listener) error {
for {
conn, err := listen.Accept()
if err != nil {
if strings.Contains(err.Error(), "use of closed network connection") {
return nil
}
s.Logger.Errorf("Unable to accept connections: %#v", err)
return err
}
s.Logger.Debug("Tcp connected:", conn.RemoteAddr())
go func(conn net.Conn) {
defer conn.Close()
defer func() {
s.Logger.Debug("Tcp disconnected:", conn.RemoteAddr())
}()
packet := make([]byte, 512)
for {
select {
case <-s.Ctx.Done():
return
default:
}
bytesRead, err := conn.Read(packet)
if err != nil {
if err != io.EOF {
s.Logger.Errorf("read error %v", err)
}
return
}
// Set the length of the packet to the number of read bytes.
packet = packet[:bytesRead]
frame, err := NewTCPFrame(packet)
if err != nil {
s.Logger.Errorf("bad packet error %v", err)
return
}
s.Logger.Debug("Read Frame:", frame)
request := &Request{conn, frame}
s.requestChan <- request
}
}(conn)
}
}
func (s *Server) ListenWithListener(listen net.Listener) {
s.listeners = append(s.listeners, listen)
go s.accept(listen)
}
// ListenTCP starts the Modbus server listening on "address:port".
func (s *Server) ListenTCP(addressPort string) (err error) {
listen, err := net.Listen("tcp", addressPort)
if err != nil {
s.Logger.Errorf("Failed to Listen: %v", err)
return err
}
s.ListenWithListener(listen)
return err
}
// ListenTLS starts the Modbus server listening on "address:port".
func (s *Server) ListenTLS(addressPort string, config *tls.Config) (err error) {
listen, err := tls.Listen("tcp", addressPort, config)
if err != nil {
s.Logger.Errorf("Failed to Listen on TLS: %v", err)
return err
}
s.ListenWithListener(listen)
return err
}