Ejemplo n.º 1
0
 /// <summary>
 /// Добавление пользователем комнаты
 /// </summary>
 /// <param name="connection"></param>
 /// <param name="client"></param>
 /// <param name="roomName"></param>
 public static void AddRoom(ref MySqlConnection connection, ref TcpClient client, string roomName)
 {
     if (rooms.FirstOrDefault(r => 0 == string.Compare(r.name, roomName, true)) == null)
     {
         DBmanager.CreateNewRoom(ref connection, roomName);
         rooms.Add(new RoomClass(roomName));
         List <string> roomList = DBmanager.GetRoomList(connection);
         unassignedUsers.ForEach(user =>
                                 SendToStream(new MessageClass(codes.SENDING_ROOMLIST, list: roomList), ref user.client));
         rooms.ForEach(room => room.connectedUsers.ForEach(user =>
                                                           SendToStream(new MessageClass(codes.SENDING_ROOMLIST, list: roomList), ref user.client)));
         Console.WriteLine("Room " + roomName + " is created.");
     }
     else
     {
         SendToStream(new MessageClass(codes.EXISTING_ROOM_NAME), ref client);
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Обработка получаемых от клиента пакетов
        /// </summary>
        public void Process()
        {
            try
            {
                MessageClass message;
                while (true)
                {
                    message = GetFromStream(ref client);
                    switch (message.code)
                    {
                    case codes.REQUESTING_ROOMLIST:
                        SendToStream(new MessageClass(codes.SENDING_ROOMLIST, list: DBmanager.GetRoomList(connection)), ref client);
                        break;

                    case codes.SENDING_USERNAME:
                        if (ServerEngine.existingNicknames.FirstOrDefault(n => n == message.info) == null)
                        {
                            name = message.info;
                            ServerEngine.existingNicknames.Add(name);
                            SendToStream(new MessageClass(codes.CONFIRMING_USERNAME, name), ref client);
                            Console.WriteLine("User " + name + " logged in.");
                        }
                        else
                        {
                            SendToStream(new MessageClass(codes.REQUESTING_USERNAME,
                                                          "There is user witn nickname \"" + message.info + "\" already"), ref client);
                        }
                        break;

                    case codes.REQUESTING_CHAT_HIST:
                        SendToStream(new MessageClass(codes.SENDING_CHAT_HIST, list: DBmanager.GetHistory(name, connection)), ref client);
                        break;

                    case codes.SENDING_CHAT_MESSAGE:
                        if (room != null)
                        {
                            room.SendBroadcastMessage(name + ": " + message.info, connection);
                            Console.WriteLine(name + ": " + message.info);
                        }
                        break;

                    case codes.SENDING_SELECTED_ROOM:
                        if (room != null)
                        {
                            ServerEngine.ChangeRoom(this, message.info);
                        }
                        else
                        {
                            ServerEngine.unassignedUsers.Remove(this);
                            ServerEngine.rooms.FirstOrDefault(r => r.name == message.info).AddClient(this);
                        }
                        break;

                    case codes.REQUESTING_NEW_ROOM:
                        ServerEngine.AddRoom(ref connection, ref client, message.info);
                        Console.WriteLine("Room " + message.info + " was added.");
                        break;

                    case codes.REQUESTING_ROOM_DELETING:
                        if (room.connectedUsers.Count == 1 && room != null)
                        {
                            room.RemoveClient(id);
                            ServerEngine.unassignedUsers.Add(this);
                            ServerEngine.RemoveRoom(room);
                            Console.WriteLine("Room " + room.name + " was deleted.");
                            SendToStream(new MessageClass(codes.SENDING_ROOMLIST, list: DBmanager.GetRoomList(connection)), ref client);
                            room = null;
                        }
                        break;

                    case codes.LEAVING_ROOM:
                        room.RemoveClient(id);
                        room = null;
                        ServerEngine.unassignedUsers.Add(this);
                        break;

                    case codes.SENDING_DISCONNECT_MESSAGE:
                        Disconnect();
                        return;

                    default:
                        Console.WriteLine("Wrong message code with package body: " + message.info + ".");
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("In Process(): " + e.Message);
                Disconnect();
            }
        }