Beispiel #1
0
        private static void AcceptCallback(IAsyncResult AR)
        {
            Socket socket = serverSocket.EndAccept(AR);

            if (socket.Connect())
            {
                NumberOfConnections = Connections.connectedClients.Count;
                ServerHelper.UpdateConsoleTitle();

                socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), socket);
            }

            serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
        }
Beispiel #2
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();
                }
            }
        }