/// <summary>
 /// Удаление комнаты из коллекции и закрытие подключения к БД
 /// </summary>
 /// <param name="room"></param>
 public static void RemoveRoom(RoomClass room)
 {
     rooms.Remove(room);
     DBmanager.RemoveRoom(ref room.connection, room.name);
     room.connection.Close();
 }
Beispiel #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();
            }
        }