/// <summary>
        /// Создание задачи и объекта для пользователя после его подключения
        /// </summary>
        /// <param name="client"></param>
        private static void CreateUser(ref TcpClient client)
        {
            MySqlConnection connection = DBmanager.Connect();
            ClientClass     clientObj  = new ClientClass(ref connection, ref client, null, idForNextUser++);

            unassignedUsers.Add(clientObj);
            Task.Run(() => clientObj.Process());
        }
Example #2
0
 protected internal void AddClient(ClientClass client)
 {
     client.room = this;
     connectedUsers.Add(client);
     Console.WriteLine("Successfully added client " + client.name + " to " + name +
                       " room. There are " + connectedUsers.Count + " connected users.");
     connectedUsers.ForEach(user => SendToStream(new MessageClass(codes.SENDING_USERLIST,
                                                                  list: connectedUsers.Select(u => u.name).ToList()), ref user.client));
     SendToStream(new MessageClass(codes.SENDING_CHAT_HIST, list: DBmanager.GetHistory(name, client.connection)), ref client.client);
     Task.Run(() => SendBroadcastMessage(client.name + " joined the room.", connection));
 }
Example #3
0
 /// <summary>
 /// Отправление всем пользователям комнаты сообщения
 /// </summary>
 /// <param name="message"></param>
 /// <param name="connection"></param>
 public void SendBroadcastMessage(string message, MySqlConnection connection)
 {
     DBmanager.SaveMessage(message, name, ref connection);
     Console.Write("Broadcasting for: ");
     for (int i = 0; i < connectedUsers.Count; i++)
     {
         Console.Write(connectedUsers[i].name + (i + 1 == connectedUsers.Count ? "" : ", "));
         SendToStream(new MessageClass(codes.SENDING_BROADCAST_MESSAGE, DateTime.Now.ToString("HH:mm:ss") +
                                       "  ||  " + message), ref connectedUsers[i].client);
     }
     Console.WriteLine(".");
 }
 /// <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);
     }
 }
 /// <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();
 }
Example #6
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();
            }
        }