コード例 #1
0
        private ClientServerCommunication obtainFromServer()
        {
            byte[] serializedObject = new byte[clientSocket.ReceiveBufferSize];
            int    bytesRead        = stream.Read(serializedObject, 0, serializedObject.Length);

            return(ClientServerCommunication.Deserialize <ClientServerCommunication>(serializedObject));
        }
コード例 #2
0
        private void sendToServer(object content, ClientServerCommunication.CommunicationType type)
        {
            ClientServerCommunication csc = new ClientServerCommunication(type);

            csc.Content = content;

            byte[] bytesToSend = ClientServerCommunication.SerializeToByteArray(csc);
            stream.Write(bytesToSend, 0, bytesToSend.Length);
        }
コード例 #3
0
        private void listenFromServer()
        {
            try
            {
                stream = clientSocket.GetStream();

                while (true)
                {
                    ClientServerCommunication csc = obtainFromServer();

                    switch (csc.Type)
                    {
                    case ClientServerCommunication.CommunicationType.Login:
                        UserViewModel user = csc.Content as UserViewModel;

                        if (user == null)
                        {
                            MessageBox.Show("Failed to login");
                        }
                        else
                        {
                            CurrentUser = user;
                            BeginInvoke((Action) delegate
                            {
                                cf = new ClientForm(this);
                                cf.Show();
                            });
                            Hide();
                        }
                        break;

                    case ClientServerCommunication.CommunicationType.InSuspence:
                        MessageBox.Show("Your account is in suspense.");
                        break;

                    case ClientServerCommunication.CommunicationType.FriendGotOnline:
                        UserViewModel onFriend = csc.Content as UserViewModel;
                        cf.BeginInvoke((Action) delegate
                        {
                            cf.friendGotOnline(onFriend);
                        });
                        break;

                    case ClientServerCommunication.CommunicationType.FriendGotOffline:
                        UserViewModel ofFriend = csc.Content as UserViewModel;
                        cf.BeginInvoke((Action) delegate
                        {
                            cf.friendGotOffline(ofFriend);
                        });
                        break;

                    case ClientServerCommunication.CommunicationType.SendMessage:
                        MessageViewModel msg = csc.Content as MessageViewModel;

                        cf.BeginInvoke((Action) delegate
                        {
                            cf.getMessage(msg);
                        });

                        break;

                    case ClientServerCommunication.CommunicationType.CreateARoom:
                        if (csc.Content == null)
                        {
                            cf.BeginInvoke((Action) delegate
                            {
                                cf.FailedToCreate();
                            });
                        }
                        else
                        {
                            currentSession = (GameSession)csc.Content;
                            cf.BeginInvoke((Action) delegate
                            {
                                cf.roomCreated(currentSession);
                            });
                        }
                        break;

                    case ClientServerCommunication.CommunicationType.GetRooms:
                        List <GameSession> sessions = (List <GameSession>)csc.Content;

                        cf.BeginInvoke((Action) delegate
                        {
                            cf.ListRooms(sessions);
                        });
                        break;

                    case ClientServerCommunication.CommunicationType.JoinRoom:
                        currentSession = csc.Content as GameSession;
                        if (currentSession == null)
                        {
                            MessageBox.Show("You can't join the session. You are in a game or the lobby is full.");
                        }
                        else
                        {
                            gf = null;
                            BeginInvoke((Action) delegate
                            {
                                gf = new GameForm(currentSession, this);
                                gf.Show();
                            });
                        }

                        break;

                    case ClientServerCommunication.CommunicationType.Move:
                        ChessPieceServer piece = (ChessPieceServer)csc.Content;
                        gf.BeginInvoke((Action) delegate
                        {
                            gf.opponentsMovement(piece);
                        });

                        break;

                    case ClientServerCommunication.CommunicationType.GameStart:
                        currentSession = (GameSession)csc.Content;
                        BeginInvoke((Action) delegate
                        {
                            gf = new GameForm(currentSession, this);
                            gf.Show();
                        });
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }