コード例 #1
0
 public Disconnection(ClientData clientData) : base(ProtocolMode.write)
 {
     this.clientData = clientData;
 }
コード例 #2
0
        private PacketAction HandleClientRegistration(Client baseClient, Packet packet)
        {
            CustomDataClient <ClientData> client = baseClient as CustomDataClient <ClientData>;

            Connection connection = packet.Read <Connection>();

            if (connection == null)
            {
                return(PacketAction.irrelevant);
            }

            if (credentials.ContainsKey(connection.Username))
            {
                if (credentials[connection.Username] != connection.PassHash)
                {
                    client.Write(new Registration(0, "wrong password"));
                    return(PacketAction.resolved);
                }
            }
            else
            {
                credentials.Add(connection.Username, connection.PassHash);
            }

            ClientData data = client.data;

            data.id     = GetId(connection.Username);
            client.data = data;

            if (clients.ContainsKey(client.data.id))
            {
                DisconnectClient(client, "Account already logged in.");
            }
            else
            {
                client.Write(new Registration(client.data.id));

                List <ClientData> clientData = new List <ClientData>();
                foreach (var otherClient in clients.Values)
                {
                    clientData.Add(otherClient);
                }

                client.Write(new UserList(clientData));

                Console.WriteLine("Registered new connection with client: " + client.data.id);
                client.onPacketReceived    += HandleClientPacket;
                client.onConnectionTimeout += HandleClientTimeout;
                client.onDisconnect        += HandleExternalDisconnect;

                clientListMutex.WaitOne();
                clients.Add(client.data.id, client);
                clientListMutex.ReleaseMutex();
            }

            client.onPacketReceived -= HandleClientRegistration;
            unregisteredClients.Remove(client);


            return(PacketAction.resolved);
        }
コード例 #3
0
ファイル: Server.cs プロジェクト: GlynLeine/Networking
        private PacketAction HandleClientRegistration(Client baseClient, Packet packet)
        {
            CustomDataClient <ClientData> client = baseClient as CustomDataClient <ClientData>;
            Protocol protocol = packet.Read <Protocol>();

            if (protocol == null)
            {
                return(PacketAction.irrelevant);
            }
            if (protocol is Connection)
            {
                Connection connection = protocol as Connection;

                if (credentials.ContainsKey(connection.Username))
                {
                    if (credentials[connection.Username] != connection.PassHash)
                    {
                        client.Write(new Login(0, "wrong password"));
                        return(PacketAction.resolved);
                    }
                }
                else
                {
                    client.Write(new Login(0, "unknown user"));
                    return(PacketAction.resolved);
                }

                ClientData data = client.data;
                data.id       = GetId(connection.Username);
                data.username = connection.Username;
                client.data   = data;

                if (clients.ContainsKey(client.data.id))
                {
                    client.Write(new Login(0, "Account already logged in."));
                    return(PacketAction.resolved);
                }
            }
            else if (protocol is Registration)
            {
                Registration registration = protocol as Registration;
                if (credentials.ContainsKey(registration.Username))
                {
                    client.Write(new Login(0, "Username already taken."));
                    return(PacketAction.resolved);
                }

                credentials.Add(registration.Username, registration.PassHash);
                ClientData data = client.data;
                data.id       = GetId(registration.Username);
                data.username = registration.Username;
                client.data   = data;
            }
            else
            {
                return(PacketAction.irrelevant);
            }

            client.Write(new Login(client.data.id));

            Console.WriteLine("Registered new connection with client: " + client.data.id);
            client.onPacketReceived    += HandleClientPacket;
            client.onConnectionTimeout += HandleClientTimeout;
            client.onDisconnect        += HandleExternalDisconnect;

            clientListMutex.WaitOne();
            clients.Add(client.data.id, client);
            clientListMutex.ReleaseMutex();

            client.onPacketReceived -= HandleClientRegistration;
            unregisteredClients.Remove(client);

            onClientConnect?.Invoke(client);

            return(PacketAction.resolved);
        }
コード例 #4
0
ファイル: GameStart.cs プロジェクト: GlynLeine/Networking
 public GameStart(ClientData opponent, bool firstTurn) : base(ProtocolMode.write)
 {
     this.opponent  = opponent;
     this.firstTurn = firstTurn;
 }