Beispiel #1
0
 private void OnClientAuthorized(TcpClient client, string userLogin, UserInfo userInfo)
 {
     lock (_connectedUsers)
     {
         foreach (KeyValuePair <string, TcpClient> connectedUser in _connectedUsers)
         {
             new UserConnectPacket(client.GetStream(), connectedUser.Key, UserManager.Instance.GetUserInfo(connectedUser.Key)).Send();
             new AddUsersToRoomPacket(client.GetStream(), Room.MainRoomId, new List <string> {
                 connectedUser.Key
             }).Send();
         }
         _connectedUsers.Add(userLogin, client);
         AddUsersToRoomPacket addUsersToRoomPacket = new AddUsersToRoomPacket(client.GetStream(), Room.MainRoomId, new List <string> {
             userLogin
         });
         UserConnectPacket userConnectPacket = new UserConnectPacket(client.GetStream(), userLogin, userInfo);
         foreach (TcpClient cl in _connectedUsers.Values)
         {
             userConnectPacket.Send(cl.GetStream());
             addUsersToRoomPacket.Send(cl.GetStream());
         }
         lock (_rooms)
         {
             _rooms[Room.MainRoomId].Members.Add(userLogin);
         }
     }
 }
Beispiel #2
0
        /// <summary>
        /// Read and prosess data from stream
        /// </summary>
        private void ClientStreamReadData()
        {
            try
            {
                using (Stream stream = _server.GetStream())
                {
                    BinaryReader streamReader = new BinaryReader(stream);
                    while (true)
                    {
                        PacketType packetType = (PacketType)streamReader.ReadInt32();

                        switch (packetType)
                        {
                        case PacketType.Negotiation:
                            NegotiationPacket negotiationPacket = new NegotiationPacket(stream);
                            negotiationPacket.Receive();
                            Login = negotiationPacket.Login;
                            _loginEvents[0].Set();
                            _registerEvents[0].Set();
                            lock (_rooms)
                            {
                                Room mainRoom = new Room(Room.MainRoomId, Room.MainRoomName, new HashSet <string>());
                                _rooms.Add(Room.MainRoomId, mainRoom);
                            }
                            _context.Post(p => NegotiationCompleted?.Invoke(this, EventArgs.Empty), null);
                            break;

                        case PacketType.SimpleMessage:
                            SimpleMessagePacket simpleMessagePacket = new SimpleMessagePacket(stream);
                            simpleMessagePacket.Receive();
                            OnSimpleMessageReceived(simpleMessagePacket);
                            break;

                        case PacketType.Error:
                            ErrorPacket errorPacket = new ErrorPacket(stream);
                            errorPacket.Receive();
                            _context.Post(p => ErrorOccurred?.Invoke(this, new ErrorOccurredEventHandlerArgs(errorPacket.ErrorMessage, errorPacket.ErrorType)), null);
                            break;

                        case PacketType.UserConnect:
                            UserConnectPacket userConnectPacket = new UserConnectPacket(stream);
                            userConnectPacket.Receive();
                            lock (_users)
                            {
                                _users[userConnectPacket.UserLogin] = userConnectPacket.UserInfo;
                            }
                            _context.Post(p => UserConnected?.Invoke(this, new UserConnectedEventHandlerArgs(userConnectPacket.UserLogin, userConnectPacket.UserInfo)), null);
                            break;

                        case PacketType.UserDisconnect:
                            UserDisconnectPacket userDisconnectPacket = new UserDisconnectPacket(stream);
                            userDisconnectPacket.Receive();
                            lock (_users)
                            {
                                _users.Remove(userDisconnectPacket.UserLogin);
                            }
                            _context.Post(p => UserDisconnected?.Invoke(this, new UserDisconnectedEventHandlerArgs(userDisconnectPacket.UserLogin)), null);
                            break;

                        case PacketType.CreateNewRoom:
                            CreateNewRoomPacket createNewRoomPacket = new CreateNewRoomPacket(stream);
                            createNewRoomPacket.Receive();
                            lock (_rooms)
                            {
                                _rooms.Add(createNewRoomPacket.Room.Id, createNewRoomPacket.Room);
                            }
                            _context.Post(p => NewRoomCreated?.Invoke(this, new NewRoomCreatedEventHandlerArgs(createNewRoomPacket.Room)), null);
                            break;

                        case PacketType.AddUserToRoom:
                            AddUsersToRoomPacket addUsersToRoomPacket = new AddUsersToRoomPacket(stream);
                            addUsersToRoomPacket.Receive();
                            lock (_rooms)
                            {
                                foreach (string user in addUsersToRoomPacket.Users)
                                {
                                    _rooms[addUsersToRoomPacket.RoomId].Members.Add(user);
                                }
                            }
                            _context.Post(p => RoomsUpdated?.Invoke(this, new RoomsUpdatedEventHandlerArgs()), null);
                            break;

                        case PacketType.RemoveUserFromRoom:
                            RemoveUserFromRoomPacket removeUserFromRoomPacket = new RemoveUserFromRoomPacket(stream);
                            removeUserFromRoomPacket.Receive();
                            lock (_rooms)
                            {
                                Room room;
                                if (_rooms.TryGetValue(removeUserFromRoomPacket.RoomId, out room))
                                {
                                    room.Members.Remove(removeUserFromRoomPacket.User);
                                }
                            }
                            _context.Post(p => RoomsUpdated?.Invoke(this, new RoomsUpdatedEventHandlerArgs()), null);
                            break;
                        }
                    }
                }
            }
            catch (Exception)
            {
                _server.Close();
                _server.Client.Dispose();
            }
        }