Exemple #1
0
        public void DataReceived(IAsyncResult ar)
        {
            PlayerConnection connection = (PlayerConnection)ar.AsyncState;

            foreach (Packet packet in connection.GetPacketsFromRecievedData(ar))
            {
                try
                {
                    if (connection.Player == null)
                    {
                        packetHandler.ProcessUnauthenticated(packet, connection);
                    }
                    else
                    {
                        packetHandler.ProcessAuthenticated(packet, connection.Player);
                    }
                }
                catch (Exception ex)
                {
                    Log.Info("Exception while processing packet: " + packet + " " + ex);
                }
            }

            if (connection.Open)
            {
                connection.BeginReceive(new AsyncCallback(DataReceived));
            }
            else
            {
                PlayerDisconnected(connection);
            }
        }
Exemple #2
0
        public void ClientAccepted(IAsyncResult ar)
        {
            Log.Info("New client connected");

            Socket socket = (Socket)ar.AsyncState;

            PlayerConnection connection = new PlayerConnection(socket.EndAccept(ar)); // TODO: Will this throw an error if timed correctly?

            connection.BeginReceive(new AsyncCallback(DataReceived));

            socket.BeginAccept(new AsyncCallback(ClientAccepted), socket);
        }