예제 #1
0
        private void OnClickRegistration(object sender, EventArgs e)
        {
            try
            {
                RegistrationData registration = new RegistrationData(
                    registrationPage.textBoxLogin.Text
                    , registrationPage.textBoxPassword.Text
                    , registrationPage.textBoxFullName.Text
                    );
                socket.Send(Packet.RegistrationPacket(registration));

                byte[] buffer = new byte[1024];
                int    readBytes;
                do
                {
                    readBytes = socket.Receive(buffer);
                }while (readBytes == 0);

                Packet packet = Packet.FromBytes(buffer);
                if (packet.PacketType == Packet.Type.RegistrationResult)
                {
                    RegistrationResult result = ( RegistrationResult )packet.Data;
                    switch (result)
                    {
                    case RegistrationResult.OK:
                        Page = signInPage;
                        break;

                    case RegistrationResult.LoginAllreadyExist:
                        MessageBox.Show("Login allready exist");
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
예제 #2
0
        private void OnReceive(object sender, SocketAsyncEventArgs e)
        {
            connectLocker.Set();

            try
            {
                if (e.Buffer != null)
                {
                    Packet packet = Packet.FromBytes(e.Buffer);

                    switch (packet.PacketType)
                    {
                    case Packet.Type.SignIn:
                    {
                        SignInData       signInData   = ( SignInData )packet.Data;
                        RegistrationData registration = database.Find(signInData.Login);
                        if (registration == null || registration.Password != signInData.Password)
                        {
                            e.AcceptSocket.Send(Packet.SignInResultPacket(SignInResult.InvalidPasswordOrLogin));
                        }
                        else
                        {
                            e.AcceptSocket.Send(Packet.SignInResultPacket(SignInResult.OK));
                            this.InvokeEx(() => listBoxFreeClients.Items.Add(registration));
                            this.InvokeEx(() => connections.Add(registration.Login, e.AcceptSocket));
                        }
                    }
                    break;

                    case Packet.Type.Registration:
                    {
                        RegistrationData registrationData = ( RegistrationData )packet.Data;
                        if (database.ContainsLogin(registrationData.Login))
                        {
                            e.AcceptSocket.Send(Packet.RegistrationResultPacket(RegistrationResult.LoginAllreadyExist));
                        }
                        else
                        {
                            e.AcceptSocket.Send(Packet.RegistrationResultPacket(RegistrationResult.OK));
                            database.Add(registrationData);
                        }
                    }
                    break;

                    case Packet.Type.GetConnectedPlayers:
                    {
                        string login = ( string )packet.Data;
                        List <RegistrationData> players = new List <RegistrationData>();
                        foreach (RegistrationData reg in listBoxFreeClients.Items)
                        {
                            if (reg.Login != login)
                            {
                                players.Add(reg);
                            }
                        }
                        e.AcceptSocket.Send(Packet.GiveConnectedPlayersPacket(players));
                    }
                    break;

                    case Packet.Type.StartGame:
                    {
                        StartGameData startGameData = ( StartGameData )packet.Data;
                        Socket        query         = connections[startGameData.LoginReply];
                        query.Send(packet);
                    }
                    break;

                    case Packet.Type.StartGameResult:
                    {
                        StartGameData startGameData = ( StartGameData )packet.Data;

                        if (startGameData.Result == StartGameResult.OK)
                        {
                            this.InvokeEx(() =>
                                {
                                    listBoxFreeClients.Items.Remove(database.Find(startGameData.LoginQuery));
                                    listBoxFreeClients.Items.Remove(database.Find(startGameData.LoginReply));
                                    listBoxInTheGame.Items.Add(startGameData);
                                });
                        }

                        Socket reply = connections[startGameData.LoginQuery];
                        reply.Send(packet);
                    }
                    break;

                    case Packet.Type.MoveChess:
                    {
                        ChessMoveData chessMoveData = ( ChessMoveData )packet.Data;
                        Socket        send          = connections[chessMoveData.LoginСontender];
                        send.Send(packet);
                    }
                    break;

                    case Packet.Type.AbortGame:
                    case Packet.Type.EndGame:
                    {
                        this.InvokeEx(() =>
                            {
                                StartGameData startGameDataRemove = null;

                                string login = FindLoginBySocket(e.AcceptSocket);
                                foreach (StartGameData startGameData in listBoxInTheGame.Items)
                                {
                                    if (startGameData.LoginQuery == login || startGameData.LoginReply == login)
                                    {
                                        if (packet.PacketType == Packet.Type.AbortGame)
                                        {
                                            string sendLogin = (login == startGameData.LoginQuery)
                                                    ?   startGameData.LoginReply
                                                    :   startGameData.LoginQuery;
                                            connections[sendLogin].Send(packet);
                                        }
                                        startGameDataRemove = startGameData;
                                        break;
                                    }
                                }

                                if (startGameDataRemove != null)
                                {
                                    listBoxFreeClients.Items.Add(database.Find(startGameDataRemove.LoginQuery));
                                    listBoxFreeClients.Items.Add(database.Find(startGameDataRemove.LoginReply));
                                    listBoxInTheGame.Items.Remove(startGameDataRemove);
                                }
                            });
                    }
                    break;
                    }
                }
            }
            catch
            {
                e.AcceptSocket.Close();
                this.InvokeEx(() =>
                {
                    string removeItem             = string.Empty;
                    RegistrationData registration = database.Find(FindLoginBySocket(e.AcceptSocket));
                    if (registration != null && listBoxFreeClients.Items.Contains(registration))
                    {
                        listBoxFreeClients.Items.Remove(registration);
                        removeItem = registration.Login;
                    }

                    if (removeItem.Length > 0)
                    {
                        connections.Remove(removeItem);
                    }
                });
            }
        }
예제 #3
0
 public static Packet RegistrationPacket(RegistrationData registrationData)
 {
     return(new Packet(Type.Registration, registrationData));
 }