Example #1
0
 public void SendToAll(PacketInfo packetInfo)
 {
     foreach (Client client in Clients)
     {
         client.SendPacket(packetInfo);
     }
 }
Example #2
0
        private bool ClientConnect(Client client, string password = "")
        {
            if (!string.IsNullOrEmpty(Password) && !password.Equals(Password, StringComparison.InvariantCultureIgnoreCase))
            {
                KickClient(client, "Wrong password.");
                return(false);
            }

            string nickname = client.UserInfo.Nickname;

            if (string.IsNullOrEmpty(nickname) || nickname.Equals(Nickname, StringComparison.InvariantCultureIgnoreCase))
            {
                KickClient(client, "Invalid nickname.");
                return(false);
            }

            if (Clients.Any(x => nickname.Equals(x.UserInfo.Nickname, StringComparison.InvariantCultureIgnoreCase)))
            {
                KickClient(client, "Nickname is already in use: " + nickname);
                return(false);
            }

            Clients.Add(client);
            OnUserConnected(client.UserInfo);
            client.Authorized = true;

            SendUserList(client);

            PacketInfo packetInfoConnected = new PacketInfo("Connected");

            packetInfoConnected.Data = client.UserInfo;
            SendToAll(packetInfoConnected);

            return(true);
        }
Example #3
0
        private void ClientMessageReceived(Client client, string text)
        {
            OnConsoleOutput(string.Format("{0} {1}: {2}", client.UserInfo.Nickname, client.IP, text));

            PacketInfo packetInfo;

            try
            {
                packetInfo = JsonConvert.DeserializeObject <PacketInfo>(text);
            }
            catch
            {
                return;
            }

            if (packetInfo != null && !string.IsNullOrEmpty(packetInfo.Command))
            {
                if (packetInfo.Command == "Connect")
                {
                    client.UserInfo.Nickname = packetInfo.GetParameter("Nickname");
                    string password = packetInfo.GetParameter("Password");
                    ClientConnect(client, password);
                }
                else if (client.Authorized)
                {
                    switch (packetInfo.Command)
                    {
                    case "Disconnect":
                        string reason = packetInfo.GetParameter("Reason");
                        ClientDisconnected(client, reason);
                        break;

                    case "Message":
                        if (client.Authorized)
                        {
                            PacketInfo  packetInfoMessage = new PacketInfo("Message");
                            MessageInfo messageInfo       = packetInfo.GetData <MessageInfo>();
                            messageInfo.FromUser   = client.UserInfo;
                            packetInfoMessage.Data = messageInfo;

                            if (messageInfo.ToUser != null && !string.IsNullOrEmpty(messageInfo.ToUser.Nickname))
                            {
                                SendTo(messageInfo.ToUser, packetInfoMessage);
                                SendTo(messageInfo.FromUser, packetInfoMessage);
                            }
                            else
                            {
                                SendToAll(packetInfoMessage);
                            }
                        }
                        break;

                    case "Ping":
                        SendTo(client.UserInfo, new PacketInfo("Pong"));
                        break;
                    }
                }
            }
        }
Example #4
0
        public void Disconnect()
        {
            PacketInfo packetInfo = new PacketInfo("Disconnect");

            packetInfo.AddParameter("Reason", "User disconnect");
            client.SendPacket(packetInfo);
            client.Disconnect();
        }
Example #5
0
 public void SendUserList(Client client)
 {
     if (Clients.Count > 0)
     {
         PacketInfo packetInfo = new PacketInfo("UserList");
         packetInfo.Data = Clients.Select(x => x.UserInfo).ToArray();
         client.SendPacket(packetInfo);
     }
 }
Example #6
0
 public void SendMessageToAll(string nickname, string message)
 {
     if (!string.IsNullOrEmpty(nickname) && !string.IsNullOrEmpty(message))
     {
         PacketInfo  packetInfoMessage = new PacketInfo("Message");
         MessageInfo messageInfo       = new MessageInfo(message);
         messageInfo.FromUser   = new UserInfo(nickname);
         packetInfoMessage.Data = messageInfo;
         SendToAll(packetInfoMessage);
     }
 }
Example #7
0
        public void KickClient(Client client, string reason = "")
        {
            PacketInfo packetInfo = new PacketInfo("Kick");

            if (!string.IsNullOrEmpty(reason))
            {
                packetInfo.AddParameter("Reason", reason);
            }
            client.SendPacket(packetInfo);
            client.Disconnect();
        }
Example #8
0
        public bool SendTo(UserInfo userInfo, PacketInfo packetInfo)
        {
            if (userInfo != null && !string.IsNullOrEmpty(userInfo.Nickname))
            {
                Client client = Clients.FirstOrDefault(x => x.UserInfo.Nickname == userInfo.Nickname);

                if (client != null)
                {
                    client.SendPacket(packetInfo);
                    return(true);
                }
            }

            return(false);
        }
Example #9
0
 public void SendMessage(string message, string toUser, Color color)
 {
     if (!string.IsNullOrEmpty(message))
     {
         PacketInfo  packetInfo  = new PacketInfo("Message");
         MessageInfo messageInfo = new MessageInfo(message);
         if (!string.IsNullOrEmpty(toUser))
         {
             messageInfo.ToUser = new UserInfo(toUser);
         }
         messageInfo.TextColor = color;
         packetInfo.Data       = messageInfo;
         client.SendPacket(packetInfo);
     }
 }
Example #10
0
        private void ClientDisconnected(Client client, string reason)
        {
            if (client.Authorized)
            {
                Clients.Remove(client);
                OnUserDisconnected(client.UserInfo);
                OnConsoleOutput(string.Format("Client disconnected: {0} {1} {2}", client.UserInfo.Nickname, client.IP, reason));

                if (IsWorking)
                {
                    PacketInfo packetInfoDisconnected = new PacketInfo("Disconnected");
                    packetInfoDisconnected.Data = client.UserInfo;
                    SendToAll(packetInfoDisconnected);
                }
            }
        }
Example #11
0
        private void client_MessageReceived(Client client, string text)
        {
            PacketInfo packetInfo = JsonConvert.DeserializeObject <PacketInfo>(text);

            if (packetInfo != null && !string.IsNullOrEmpty(packetInfo.Command))
            {
                switch (packetInfo.Command)
                {
                case "Connected":
                    UserInfo connectedUserInfo = packetInfo.GetData <UserInfo>();
                    OnNotification(connectedUserInfo.Nickname + " connected.");
                    OnUserConnected(connectedUserInfo);
                    break;

                case "Disconnected":
                    UserInfo disconnectedUserInfo = packetInfo.GetData <UserInfo>();
                    OnNotification(disconnectedUserInfo.Nickname + " disconnected.");
                    OnUserDisconnected(disconnectedUserInfo);
                    break;

                case "Message":
                    MessageInfo messageInfo = packetInfo.GetData <MessageInfo>();
                    OnMessageReceived(messageInfo);
                    break;

                case "UserList":
                    UserInfo[] userList = packetInfo.GetData <UserInfo[]>();
                    OnUserListReceived(userList);
                    break;

                case "Kick":
                    string reason = packetInfo.GetParameter("Reason");
                    OnKicked(reason);
                    break;

                case "Pong":
                    if (pingTimer != null)
                    {
                        OnPong(pingTimer.ElapsedMilliseconds);
                    }
                    break;
                }
            }
        }
Example #12
0
 private void SendPacketThread(PacketInfo packetInfo)
 {
     try
     {
         StreamWriter           sw           = new StreamWriter(client.GetStream());
         JsonSerializerSettings jsonSettings = new JsonSerializerSettings
         {
             NullValueHandling = NullValueHandling.Ignore
         };
         string data = JsonConvert.SerializeObject(packetInfo, jsonSettings);
         Debug.WriteLine("SendPacket: " + data);
         sw.Write(data);
         sw.Flush();
     }
     catch (Exception e)
     {
         Debug.WriteLine("Send packet failed: " + e.ToString());
     }
 }
Example #13
0
 public void Connect(string password = "")
 {
     try
     {
         client = new Client(IPAddress, Port);
         client.UserInfo.Nickname = Nickname;
         client.MessageReceived  += client_MessageReceived;
         client.Disconnected     += client_Disconnected;
         PacketInfo packetInfo = new PacketInfo("Connect");
         packetInfo.AddParameter("Nickname", Nickname);
         if (!string.IsNullOrEmpty(password))
         {
             packetInfo.AddParameter("Password", password);
         }
         client.SendPacket(packetInfo);
         IsConnected = true;
     }
     catch (Exception e)
     {
         MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
Example #14
0
 public void SendPacket(PacketInfo packetInfo)
 {
     new Thread(() => SendPacketThread(packetInfo)).Start();
 }