Пример #1
0
        protected void initialConnectionCallback(NetworkState state)
        {
            SFMLPacket packet = new SFMLPacket(state.Buffer);
            state.Buffer = new byte[state.Buffer.Length];
            state.ConnectionState = NetworkState.ConnectionStates.CONNECTED;

            Thread.Sleep(500); // Wait 0.5 seconds to prevent brute-force log-in attempts

            if (packet.packetCode == 101 || packet.packetCode == 102)
            {
                if (packet.packetCode == 101) //Register request
                {
                    Console.WriteLine("Register Request");
                    String username = packet.ExtractString();
                    String realName = packet.ExtractString();
                    String email = packet.ExtractString();
                    String password = packet.ExtractString();
                    if (!registerRequest(username, password, email, realName))
                    {
                        Console.WriteLine("Unable to register new client");
                        Network.Send(state.Socket, "\u0000");
                        Thread.Sleep(5000); // Wait 5 seconds for the send to complete
                        state.Socket.Shutdown(System.Net.Sockets.SocketShutdown.Both);
                        state.Socket.Close();
                        return;
                    }
                }
                else if (packet.packetCode == 102) //Login request
                {
                    Console.WriteLine("Login Request");
                    String username = packet.ExtractString();
                    String password = packet.ExtractString();
                    if (!loginRequest(username, password))
                    {
                        Console.WriteLine("Invalid Login Attempt");
                        Network.Send(state.Socket, "\u0000");
                        Thread.Sleep(5000); // Wait 5 seconds for the send to complete
                        state.Socket.Shutdown(System.Net.Sockets.SocketShutdown.Both);
                        state.Socket.Close();
                        return;
                    }
                }
                Network.Send(state.Socket, "\u0001"); //Send success
                state.CallBack = DataReceivedCallback;
                Network.RequestMoreData(state);
            }
            else
            {
                Console.WriteLine("Illegal request from non logged in client");
                Network.Send(state.Socket, "\u0000");
                Thread.Sleep(5000); // Wait 5 seconds for the send to complete
                state.Socket.Shutdown(System.Net.Sockets.SocketShutdown.Both);
                state.Socket.Close();
            }
        }
Пример #2
0
        protected void DataReceivedCallback(NetworkState state)
        {
            SFMLPacket incomingPacket = new SFMLPacket(state.Buffer);
            state.Buffer = new byte[state.Buffer.Length];
            state.ConnectionState = NetworkState.ConnectionStates.CONNECTED;

            foreach (String data in incomingPacket)
            {
                Console.WriteLine(data);
            }
        }