Ejemplo n.º 1
0
// Close will close the connection, freeing the port
func (s *UDPServer) Close() error {
	e := s.Stop()
	if !err.Check(e) {
		return e
	}
	if e = s.conn.Close(); err.Check(e) {
		s.conn = nil
	}
	return e
}
Ejemplo n.º 2
0
// RunNew is a wrapper around new that also calls Run in a Go routine if the
// server was created without error
func RunNew(port string, packetHandler PacketHandler) (*UDPServer, error) {
	s, e := New(port, packetHandler)
	if err.Check(e) {
		go s.Run()
	}
	return s, e
}
Ejemplo n.º 3
0
// New creates a UDPserver
// passing in ":0" for port will select any open port
func New(port string, packetHandler PacketHandler) (*UDPServer, error) {
	laddr, e := net.ResolveUDPAddr("udp", port)
	if !err.Check(e) {
		return nil, e
	}
	conn, e := net.ListenUDP("udp", laddr)
	if !err.Check(e) {
		return nil, e
	}

	server := &UDPServer{
		conn:          conn,
		packetHandler: packetHandler,
		localIP:       getLocalIP(),
		stop:          false,
		running:       false,
	}
	return server, nil
}