Exemplo n.º 1
0
 public ASyncClient(Socket socket)
 {
     state      = new ConnectionState(socket);
     zoneClient = new ZoneClient();
     zoneClient.UserIPAddress = ((IPEndPoint)state.socket.RemoteEndPoint).Address;
     zoneClient.packetsToBeSent.CollectionChanged += (sender, eventArgs) =>
     {
         if (!zoneClient.IsProcessingOrSendingPackets)
         {
             ASyncClient.SendPackets(this);
         }
     };
 }
Exemplo n.º 2
0
        public void AcceptCallback(IAsyncResult ar)
        {
            waitForNewConnection.Set();

            // Get the socket that handles the client request.
            Socket listener = (Socket)ar.AsyncState;
            Socket handler  = listener.EndAccept(ar);

            var client = new ASyncClient(handler);
            int port   = ((IPEndPoint)handler.LocalEndPoint).Port;

            if (port >= Zone.LOBBY_PORT)
            {
                client.zoneClient.Lobby = Zone.GetZoneLobby(port);
            }
            Log.Inform("ASyncServer", "New connection setup. Waiting for client first message.");
            handler.BeginReceive(client.state.buffer, 0, client.state.buffer.Length, 0, new AsyncCallback(ReadCallback), client);
        }
Exemplo n.º 3
0
 public void ProcessRequest(byte[] receivedData)
 {
     try
     {
         lock (zoneClient.packetsToBeSent)
         {
             zoneClient.IsProcessingOrSendingPackets = true;
             zoneClient.packetsToBeSent.AddRange(
                 Zone.Process(zoneClient, receivedData));
         }
     }
     catch (Protocol.Packet.ZonePacket.NotEnoughBytesException ex)
     {
         zoneClient.IsProcessingOrSendingPackets = false;
         throw ex;
     }
     ASyncClient.SendPackets(this);
 }
Exemplo n.º 4
0
        public static void SendPackets(ASyncClient client)
        {
            client.zoneClient.IsProcessingOrSendingPackets = true;
            if (client.zoneClient.packetsToBeSent.Count == 0)
            {
                client.zoneClient.IsProcessingOrSendingPackets = false;
                return;
            }

            MemoryStream memoryStream;

            lock (client.zoneClient.packetsToBeSent)
            {
                memoryStream =
                    client.zoneClient.packetsToBeSent.Aggregate(
                        new MemoryStream(),
                        (ms, packet) =>
                {
                    var packetBytes = packet.GetBytes(client.zoneClient.SecureKey);
                    ms.Write(packetBytes, 0, packetBytes.Length);
                    return(ms);
                });

                client.zoneClient.packetsToBeSent.Clear();
            }

            try
            {
                client.state.socket.BeginSend(
                    memoryStream.ToArray(), 0, memoryStream.ToArray().Length, SocketFlags.None,
                    new AsyncCallback(SendPacketsCallback), client);
            }
            catch (SocketException ex)
            {
                Log.Debug("ASyncClient", "Client closed socket.");
                Zone.ClientWentOffline(client);
            }

            client.zoneClient.IsProcessingOrSendingPackets = false;
        }