switch to using only TCPConn in net.tcp.server

This commit is contained in:
Joel Wetzell
2025-12-06 10:58:18 -06:00
parent 190bdd8b44
commit ed4f7dc1f7

View File

@@ -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: