Esempio n. 1
0
func ClientConnection(clientsock *websocket.Conn) {
	packet := pnet.NewPacket()
	buffer := make([]uint8, pnet.PACKET_MAXSIZE)
	recv, err := clientsock.Read(buffer)
	if err == nil {
		copy(packet.Buffer[0:recv], buffer[0:recv])
		packet.GetHeader()
		g_server.ParseFirstMessage(clientsock, packet)
	} else {
		if err.Error() != "EOF" {
			logger.Println("Client connection error: " + err.Error())
		}
	}
}
Esempio n. 2
0
func (s *Server) Run() {
	// Open new socket listener
	g_logger.Println("Opening server socket on port " + s.Port)
	socket, err := net.Listen("tcp", ":"+s.Port)
	if err != nil {
		g_logger.Printf("[Error] Could not open socket - %v\n", err)
		return
	}
	defer socket.Close() // Defer the close function so that's get done automatically when this method breaks
	defer g_logger.Println("[Notice] Server socket closed")

	g_logger.Println("Server ready to accept new connections")
	for {
		clientsock, err := socket.Accept()
		if err != nil {
			g_logger.Println("[Warning] Could not accept new connection")
			continue
		}

		var headerbuffer [2]uint8
		recv, err := clientsock.Read(headerbuffer[0:])
		if (err != nil) || (recv == 0) {
			g_logger.Printf("[Warning] Could not read packet header: %v", err)
			continue
		}
		// Create new packet
		packet := pnet.NewPacket()
		copy(packet.Buffer[0:2], headerbuffer[0:2]) // Write header buffer to packet
		packet.GetHeader()

		databuffer := make([]uint8, packet.MsgSize)
		recv, err = clientsock.Read(databuffer[0:])
		if recv == 0 || err != nil {
			g_logger.Printf("[Warning] Serer connection read error: %v", err)
			continue
		}
		copy(packet.Buffer[2:], databuffer[:]) // Write rest of the received data to packet

		// Read and execute the first received packet
		go s.ParseMessage(clientsock, packet)
	}
}
Esempio n. 3
0
func (c *Connection) HandleConnection() {
	c.IsOpen = true

	for {
		packet := pnet.NewPacket()
		var buffer []uint8
		err := websocket.Message.Receive(c.Socket, &buffer)
		if err == nil {
			copy(packet.Buffer[0:len(buffer)], buffer[0:len(buffer)])
			packet.GetHeader()
			c.ProcessPacket(packet)
		} else {
			// println(err.Error())
			break
		}
	}

	c.IsOpen = false
	c.Owner.removeConnection()
}