Exemple #1
0
func (s *Server) readFromClient(c *client.Client) {
	buf := make([]byte, 4096)
	for {
		n, err := c.Conn.Read(buf)
		if err != nil {
			s.CloseConn(c)
			return
		}
		p := decode.DecodeRawPacket(buf[0:n])
		s.handlePacket(p, c)
	}
}
Exemple #2
0
func (c *Client) readFromServer() {
	buf := make([]byte, 4096)
	for {
		n, err := c.Conn.Read(buf)
		if err != nil {
			logger.Printf("Server disconnected")
			c.Shutdown()
			return
		}
		p := decode.DecodeRawPacket(buf[0:n])
		c.handlePacket(p)
	}
}
Exemple #3
0
func (s *Server) BroadcastListen() {
	socket, err := net.ListenUDP("udp4", &net.UDPAddr{listen, port})
	logger.Printf("Starting auto discovery server")
	if err != nil {
		panic(err)
	}
	for {
		data := make([]byte, 1024)
		n, addr, err := socket.ReadFromUDP(data)
		if err != nil {
			panic(err)
		}
		p := decode.DecodeRawPacket(data[0:n])
		if p.PacketType() == packet.PING {
			client, err := s.OpenConn(addr.IP)
			if err != nil {
				panic(err)
			}
			s.handlePacket(p, client)
		}
	}
}