Ejemplo n.º 1
0
        private static void ReceiveCallback(IAsyncResult AR)
        {
            Connection client = null;

            try
            {
                client = ((Socket)AR.AsyncState).HandleSocket();

                int received = client.Socket.EndReceive(AR);

                if (received == 0)
                {
                    return;
                }

                //Deserializes Received bytes
                {
                    byte[] dataBuff = new byte[received];
                    Array.Copy(buffer, dataBuff, received);

                    //dataBuff.PrintData(false);
                    var receivedPacket = dataBuff.Deserialize();
                    TotalPacketsRec++;
                    var data = receivedPacket.Execute(client).Result;

                    var packetType = data.Packet.GetPacketType();

                    ServerHelper.PrintPacketData(ref client, receivedPacket.GetData(), false, packetType);

                    //Send data
                    if (packetType != PacketType.Nothing)
                    {
                        switch (packetType)
                        {
                        case PacketType.Local:
                            foreach (var v in Connections.connectedClients)
                            {
                                var temp = v.Value;
                                data.SendPacket(ref temp);
                                TotalPacketsSent++;
                            }
                            break;

                        case PacketType.Others:
                            foreach (var v in Connections.connectedClients)
                            {
                                var temp = v.Value;
                                if (temp != client)
                                {
                                    data.SendPacket(ref temp);
                                    TotalPacketsSent++;
                                }
                            }
                            break;

                        case PacketType.ReturnToSender:
                            data.SendPacket(ref client);
                            TotalPacketsSent++;
                            break;
                        }
                    }
                }

                //Continue recieving data for client.
                client.Socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, ReceiveCallback, client.Socket);

                ServerHelper.UpdateConsoleTitle();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                if (client != null)
                {
                    Connections.connectedClients.Remove(client.Socket);
                    NumberOfConnections = Connections.connectedClients.Count;
                    ServerHelper.UpdateConsoleTitle();
                }
            }
        }
Ejemplo n.º 2
0
 static void SendPacket(this Result result, ref Connection c)
 {
     result.PacketBytes.SendPacket(ref c);
     ServerHelper.PrintPacketData(ref c, result.Packet.GetData(), true, result.Packet.GetPacketType());
 }