-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtcpServer.go
More file actions
93 lines (80 loc) · 2.64 KB
/
tcpServer.go
File metadata and controls
93 lines (80 loc) · 2.64 KB
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
package modbus
import (
"fmt"
"io"
"net"
)
// TCPServer represents a mechanism for receiving connections from remote clients.
// Note that this is not a Modbus server, but a TCP service, ready to accept connections
// and from each connection create a Modbus instance using NewTCPConn(...)
type TCPServer interface {
io.Closer
// WaitClosed will simply wait until the TCP server is closed. This is useful for creating
// programs that don't exit until the listener is terminated.
WaitClosed()
}
type tcpServer struct {
tcpl *net.TCPListener
host string
servers map[byte]Server
closed chan bool
}
// ServeAllUnits is a convenience function to map a Modbus Server instance on to all unitID addresses.
func ServeAllUnits(server Server) map[int]Server {
ret := make(map[int]Server)
ret[0xFF] = server
return ret
}
/*
NewTCPServer establishes a listening socket to accept incoming TCP requests. Use ":{port}" style value to bind
to all interfaces on the host. Use a specific local IP or local hostname to bind to just one interface.
Example bind to all interfaces: NewModbusTCPListener(":502", demux)
Example bind to just localhost: NewModbusTCPListener("localhost:502", demux)
Note that this function accepts a UnitID to Server mapping. Any connections to this server will be initialized
with the supplied servers serving requests to the matching UnitID. It's normal for Modbus-TCP to have 1 server
instance hosting ALL the UnitID addresses on the bus. The standard is to listen on UnitID 0xff. This is made
more convenient with the ServeAllUnits(server) function.const
tcpserv, _ := modbus.NewTCPServer(":502", modbus.ServeAllUnits(server))
*/
func NewTCPServer(host string, servers map[int]Server) (TCPServer, error) {
laddr, err := net.ResolveTCPAddr("tcp", host)
if err != nil {
return nil, err
}
tcpl, err := net.ListenTCP("tcp", laddr)
if err != nil {
return nil, err
}
mservers := make(map[byte]Server)
for u, s := range servers {
mservers[bytePanic(u)] = s
}
tlistener := &tcpServer{tcpl, host, mservers, make(chan bool)}
go tlistener.monitor()
return tlistener, nil
}
func (t *tcpServer) Close() error {
return t.tcpl.Close()
}
func (t *tcpServer) WaitClosed() {
<-t.closed
}
func (t *tcpServer) monitor() {
// defer tcpl.Close()
for {
conn, err := t.tcpl.AcceptTCP()
if err != nil {
fmt.Printf("Error awaiting connections on %v: %v\n", t.host, err)
close(t.closed)
break
}
m, err := NewTCPConn(conn)
if err != nil {
fmt.Printf("Error establishing Modbus connection from remote %v to local %v: %v\n", conn.RemoteAddr(), t.host, err)
} else {
for u, s := range t.servers {
m.SetServer(int(u), s)
}
}
}
}