예제 #1
0
        public netMessage readMessage(System.Net.Sockets.NetworkStream stream)
        {
            BinaryFormatter bf = new BinaryFormatter();
            netMessage      nm = bf.Deserialize(stream) as netMessage;

            return(nm);
        }
예제 #2
0
        void clientProcess(object tcpClientObject)
        {
            TcpClient client = tcpClientObject as TcpClient;
            NetworkStream netStream = client.GetStream();

            while(true){
                //итак узнаем что же он хочет от нас?
                netMessage nm = new netMessage();
                try {
                    nm = nm.readMessage(netStream);

                } catch (Exception ex) {
                    return;
                }

                switch (nm.code){
                    case netMessageType.getMapsListRequest: //список карт
                        returnGamesList(netStream);
                        netStream.Close();
                        return;
                    case netMessageType.getMapRequest:
                        returnMapData(netStream, nm as getMapRequest);
                        netStream.Close();
                        return;
                    case netMessageType.newUserRequest:
                        var user=newUser(nm as newUserRequest, netStream);
                        if (user != null) {
                            lock (games) {
                                user.ListenClient = client;
                            }
                            userListenLoop(user);
                            lock (games) {
                                user.Game.Users.Remove(user.Name);
                                users.Remove(user.Name);
                            }
                            client.Close();
                        }
                        return;
                    case netMessageType.secondConnectionRequest:
                        string uname = (nm as secondConnectionRequest).userName;
                        lock (games) {
                            users[uname].SendClient = client;
                        }
                        break;
                }
            }
        }
예제 #3
0
        void reciveMessagesLoop()
        {
            reciveClient = new TcpClient();
            try {
                reciveClient.Connect(sendClient.Client.RemoteEndPoint as System.Net.IPEndPoint);
            }catch(Exception ex){
                MessageBox.Show(ex.Message, "Ошибка при попытке открыть второе подключение", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Close();
                return;
            }
            secondConnectionRequest scr = new secondConnectionRequest();
            scr.userName = me.name;
            scr.sendMessage(reciveClient.GetStream());

            netMessage message = new netMessage();
            try {
                while (true) {
                    message = message.readMessage(reciveClient.GetStream());
                    switch (message.code) {
                        case netMessageType.usersFromServer:
                            updateUsersTable(message as messageUsersFromServer);
                            break;
                    }
                }
            }catch(Exception ex){
                MessageBox.Show(ex.Message, "Ошибка чтения данных от сервера", MessageBoxButtons.OK, MessageBoxIcon.Error);
                reciveClient.Close();
                return;
            }
        }
예제 #4
0
        void userListenLoop(clsUser user)
        {
            netMessage message = new netMessage();
            while (true)
            {
                try {
                    message = message.readMessage(user.ListenClient.GetStream());
                } catch (Exception ex) {
                    return;
                }
                switch (message.code)
                {
                    case netMessageType.userToServer:
                        lock (games)
                        {
                            user.X = (message as messageUserToServer).x;
                            user.Y = (message as messageUserToServer).y;
                            sendNewCoordinates(user.Game.Map.name,user);
                        }

                        break;
                }
            }
        }