public void SendToAll(PacketInfo packetInfo) { foreach (Client client in Clients) { client.SendPacket(packetInfo); } }
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); }
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; } } } }
public void Disconnect() { PacketInfo packetInfo = new PacketInfo("Disconnect"); packetInfo.AddParameter("Reason", "User disconnect"); client.SendPacket(packetInfo); client.Disconnect(); }
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); } }
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); } }
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(); }
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); }
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); } }
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); } } }
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; } } }
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()); } }
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); } }
public void SendPacket(PacketInfo packetInfo) { new Thread(() => SendPacketThread(packetInfo)).Start(); }