public static Message InformationHandler(InfomationType type, string name) { if (type == InfomationType.ConnectedUsers) { Message m = new Message("Server", MessageType.Infomation); m.SetColor(ColorToNColor(Color.Aquamarine)); m.SetContent(clients.Count.ToString() + " Connected Clients"); return(m); } else if (type == InfomationType.ConnectTime) { TimeSpan connectedTime = DateTime.UtcNow - clients[name].ConnectTime; Message m = new Message("Server", MessageType.Infomation); m.SetColor(ColorToNColor(Color.Aquamarine)); m.SetContent("You have been connected for " + connectedTime.ToString()); return(m); } else if (type == InfomationType.MessagesSent) { Message m = new Message("Server", MessageType.Infomation); m.SetColor(ColorToNColor(Color.Aquamarine)); m.SetContent(TotalMessagesSent.ToString() + " Messages Sent"); return(m); } else if (type == InfomationType.ServerUptime) { TimeSpan connectedTime = DateTime.UtcNow - startup; Message m = new Message("Server", MessageType.Infomation); m.SetColor(ColorToNColor(Color.Aquamarine)); m.SetContent("The server has been online for " + connectedTime.ToString()); return(m); } else if (type == InfomationType.Rooms) { Message m = new Message("Server", MessageType.Infomation); m.SetColor(ColorToNColor(Color.Aquamarine)); string content = "\n"; foreach (KeyValuePair <string, Room> room in Rooms) { content += $"{room.Value.Name} ({room.Value.ID})\n"; } m.SetContent(content + "use the /changeroom followed by an id or a name to change room"); return(m); } else { Message m = new Message("Server", MessageType.Infomation); m.SetColor(ColorToNColor(Color.Aquamarine)); m.SetContent("Unknown"); return(m); } }
private static void HandleClientDesktopWorker(object token) { ClientInfo c = (ClientInfo)token; NetworkStream stream = c.Stream; // Let bots know there is a new user foreach (dynamic b in Bots) { b.OnUserConnect(c); } // Tell all connected clients that we are connected Message cm = new Message(c.Name, MessageType.Status); cm.SetStatusType(StatusType.Connected); SendToAllClients(cm, null, -1); Room r = new Room(); // Put user in none full room foreach (KeyValuePair <string, Room> room in Rooms) { if (!room.Value.AddUser(c)) { continue; } else { r = room.Value; break; } } if (r.Equals(default(Room))) { JsonMessage m = new JsonMessage("Server", MessageType.Status); m.SetStatusType(StatusType.ErrorDisconnect); m.SetContent("All available rooms are full"); JsonMessageHelpers.SetJsonMessage(stream, m); return; } Message rooms = new Message("Server", MessageType.Request); rooms.SetRequestType(RequestType.Rooms); rooms.SetContent(Rooms.SerializeRooms()); MessageHelpers.SetMessage(stream, rooms); // Send The Server Message Buffer foreach (Message message in r.Buffer) { MessageHelpers.SetMessage(stream, message); } while (true) { try { c = clients[c.GUID]; Message m = MessageHelpers.GetMessage(stream); foreach (dynamic b in Bots) { m = b.OnMessage(c, m); } // Manage Message buffer if (m.MessageType == MessageType.Message) { r.AddMesssage(m); } else if (m.MessageType == MessageType.Infomation) { MessageHelpers.SetMessage(stream, InformationHandler(m.InfomationType, c.GUID)); continue; } else if (m.MessageType == MessageType.Status && m.StatusType == StatusType.ChangeRoom) { try { ClientInfo cInfo = clients[c.GUID]; if (!r.AddUser(cInfo)) { m = new Message("Server", MessageType.Message); m.SetColor(ColorToNColor(Color.Aquamarine)); m.SetContent("The requested room is full"); MessageHelpers.SetMessage(stream, m); continue; } r.RemoveUser(cInfo); int id = int.Parse(m.Content); cInfo.SetRoomID(id); clients[c.GUID] = cInfo; Room oldR = r; r = Rooms.Where(x => x.Value.ID == id).First().Value; r.AddUser(cInfo); m = new Message("Server", MessageType.Message); m.SetContent("Moved to Room " + r.Name + "(" + id + ")"); MessageHelpers.SetMessage(stream, m); foreach (Message message in r.Buffer) { MessageHelpers.SetMessage(stream, message); } foreach (dynamic b in Bots) { b.OnUserChangeRoom(c, oldR, r); } continue; } catch { try { ClientInfo cInfo = clients[c.GUID]; Room oldR = r; r = Rooms.Where(x => x.Value.Name == m.Content).First().Value; cInfo.SetRoomID(r.ID); r.AddUser(cInfo); clients[c.GUID] = cInfo; m = new Message("Server", MessageType.Message); m.SetContent("Moved to Room " + r.Name); MessageHelpers.SetMessage(stream, m); foreach (Message message in r.Buffer) { MessageHelpers.SetMessage(stream, message); } foreach (dynamic b in Bots) { b.OnUserChangeRoom(c, oldR, r); } continue; } catch { continue; } } } if (m.Name != c.Name) { m.SetName(c.Name); } if (m.StatusType == StatusType.Disconnecting && m.MessageType == MessageType.Status) { foreach (dynamic b in Bots) { b.OnUserDisconnect(c); } r.RemoveUser(clients[c.GUID]); MessageHelpers.SetMessage(stream, m); stream.Close(); clients.Remove(c.GUID); Message d = new Message(c.Name, MessageType.Status); d.SetStatusType(StatusType.Disconnected); SendToAllClients(d, null, -1); break; } foreach (KeyValuePair <string, ClientInfo> network in clients) { if (network.Key == c.GUID) { continue; } else { if (m.EndPoint == "") { SendToAllClients(m, null, clients[c.GUID].RoomId, c.GUID); break; } else { if (network.Value.Name == m.EndPoint) { MessageHelpers.SetMessage(network.Value.Stream, m); } } } } } catch (IOException) { if (clients.ContainsKey(c.GUID)) { foreach (dynamic b in Bots) { b.OnUserDisconnect(c); } r.RemoveUser(clients[c.GUID]); clients[c.GUID].Stream.Close(); clients.Remove(c.GUID); } foreach (KeyValuePair <string, ClientInfo> network in clients) { Message d = new Message(network.Key == c.Name ? "You" : c.Name, MessageType.Status); d.SetStatusType(StatusType.Disconnected); MessageHelpers.SetMessage(network.Value.Stream, d); } break; } catch (Exception ex) { if (clients.ContainsKey(c.GUID)) { foreach (dynamic b in Bots) { b.OnUserDisconnect(c); } r.RemoveUser(clients[c.GUID]); clients[c.GUID].Stream.Close(); clients.Remove(c.GUID); } Console.WriteLine(c.Name + " disconnected due to an error. Details: " + ex.Message); foreach (KeyValuePair <string, ClientInfo> network in clients) { Message e = new Message(network.Key == c.Name ? "You" : c.Name, MessageType.Status); e.SetStatusType(StatusType.ErrorDisconnect); MessageHelpers.SetMessage(network.Value.Stream, e); } break; } } }