mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-27 13:25:40 +00:00
Merge pull request #6 from jwetzell/feat/tcp-udp-server-ip-binding
add ability to bind to specific IP address for TCP and UDP servers
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
|||||||
|
|
||||||
type TCPServer struct {
|
type TCPServer struct {
|
||||||
config ModuleConfig
|
config ModuleConfig
|
||||||
|
Ip string
|
||||||
Port uint16
|
Port uint16
|
||||||
framingMethod string
|
framingMethod string
|
||||||
router *Router
|
router *Router
|
||||||
@@ -46,7 +47,20 @@ func init() {
|
|||||||
return nil, fmt.Errorf("tcp framing method must be a string")
|
return nil, fmt.Errorf("tcp framing method must be a string")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &TCPServer{framingMethod: framingMethodString, Port: uint16(portNum), config: config, quit: make(chan interface{})}, nil
|
ipString := "0.0.0.0"
|
||||||
|
|
||||||
|
ip, ok := params["ip"]
|
||||||
|
if ok {
|
||||||
|
|
||||||
|
specificIpString, ok := ip.(string)
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("tcp ip method must be a string")
|
||||||
|
}
|
||||||
|
ipString = specificIpString
|
||||||
|
}
|
||||||
|
|
||||||
|
return &TCPServer{framingMethod: framingMethodString, Port: uint16(portNum), Ip: ipString, config: config, quit: make(chan interface{})}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -116,7 +130,7 @@ ClientRead:
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ts *TCPServer) Run() error {
|
func (ts *TCPServer) Run() error {
|
||||||
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", ts.Port))
|
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", ts.Ip, ts.Port))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type UDPServer struct {
|
type UDPServer struct {
|
||||||
|
Ip string
|
||||||
Port uint16
|
Port uint16
|
||||||
config ModuleConfig
|
config ModuleConfig
|
||||||
router *Router
|
router *Router
|
||||||
@@ -30,7 +31,20 @@ func init() {
|
|||||||
return nil, fmt.Errorf("net.udp.server port must be uint16")
|
return nil, fmt.Errorf("net.udp.server port must be uint16")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &UDPServer{Port: uint16(portNum), config: config}, nil
|
ipString := "0.0.0.0"
|
||||||
|
|
||||||
|
ip, ok := params["ip"]
|
||||||
|
if ok {
|
||||||
|
|
||||||
|
specificIpString, ok := ip.(string)
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("tcp ip method must be a string")
|
||||||
|
}
|
||||||
|
ipString = specificIpString
|
||||||
|
}
|
||||||
|
|
||||||
|
return &UDPServer{Ip: ipString, Port: uint16(portNum), config: config}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -49,7 +63,7 @@ func (us *UDPServer) RegisterRouter(router *Router) {
|
|||||||
|
|
||||||
func (us *UDPServer) Run() error {
|
func (us *UDPServer) Run() error {
|
||||||
|
|
||||||
addr, err := net.ResolveUDPAddr("udp", fmt.Sprintf(":%d", us.Port))
|
addr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", us.Ip, us.Port))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("error resolving UDP address: %v", err)
|
log.Fatalf("error resolving UDP address: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user