mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-27 13:25:40 +00:00
switch to using only TCPConn in net.tcp.server
This commit is contained in:
@@ -15,13 +15,12 @@ import (
|
|||||||
|
|
||||||
type TCPServer struct {
|
type TCPServer struct {
|
||||||
config ModuleConfig
|
config ModuleConfig
|
||||||
Ip string
|
Addr *net.TCPAddr
|
||||||
Port uint16
|
FramingMethod string
|
||||||
framingMethod string
|
|
||||||
router *Router
|
router *Router
|
||||||
quit chan interface{}
|
quit chan interface{}
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
connections []net.Conn
|
connections []*net.TCPConn
|
||||||
connectionsMu sync.RWMutex
|
connectionsMu sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,7 +64,12 @@ func init() {
|
|||||||
ipString = specificIpString
|
ipString = specificIpString
|
||||||
}
|
}
|
||||||
|
|
||||||
return &TCPServer{framingMethod: framingMethodString, Port: uint16(portNum), Ip: ipString, config: config, quit: make(chan interface{})}, nil
|
addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", ipString, uint16(portNum)))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &TCPServer{FramingMethod: framingMethodString, Addr: addr, config: config, quit: make(chan interface{})}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -82,7 +86,7 @@ func (ts *TCPServer) RegisterRouter(router *Router) {
|
|||||||
ts.router = router
|
ts.router = router
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ts *TCPServer) handleClient(client net.Conn) {
|
func (ts *TCPServer) handleClient(client *net.TCPConn) {
|
||||||
ts.connectionsMu.Lock()
|
ts.connectionsMu.Lock()
|
||||||
ts.connections = append(ts.connections, client)
|
ts.connections = append(ts.connections, client)
|
||||||
ts.connectionsMu.Unlock()
|
ts.connectionsMu.Unlock()
|
||||||
@@ -90,7 +94,7 @@ func (ts *TCPServer) handleClient(client net.Conn) {
|
|||||||
defer client.Close()
|
defer client.Close()
|
||||||
var framer framing.Framer
|
var framer framing.Framer
|
||||||
|
|
||||||
switch ts.framingMethod {
|
switch ts.FramingMethod {
|
||||||
case "LF":
|
case "LF":
|
||||||
framer = framing.NewByteSeparatorFramer([]byte{'\n'})
|
framer = framing.NewByteSeparatorFramer([]byte{'\n'})
|
||||||
case "CR":
|
case "CR":
|
||||||
@@ -160,8 +164,7 @@ ClientRead:
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ts *TCPServer) Run() error {
|
func (ts *TCPServer) Run() error {
|
||||||
// TODO(jwetzell): switch to net.ListenTCP and move addr resolution to init
|
listener, err := net.ListenTCP("tcp", ts.Addr)
|
||||||
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", ts.Ip, ts.Port))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -176,7 +179,7 @@ func (ts *TCPServer) Run() error {
|
|||||||
|
|
||||||
AcceptLoop:
|
AcceptLoop:
|
||||||
for {
|
for {
|
||||||
conn, err := listener.Accept()
|
conn, err := listener.AcceptTCP()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
select {
|
select {
|
||||||
case <-ts.quit:
|
case <-ts.quit:
|
||||||
|
|||||||
Reference in New Issue
Block a user