public static JsonMessage ToJsonMessage(this Message message) { JsonMessage js = new JsonMessage(message.Name, message.MessageType, message.EndPoint); js.SetColor(message.Color); js.SetContent(message.Content); js.SetStatusType(message.StatusType); js.SetInformationType(message.InfomationType); js.SetRequestType(message.RequestType); return(js); }
private static void HandleClientWebWorker(object token) { object[] d = (object[])token; TcpClient client = (TcpClient)d[0]; ClientInfo c = (ClientInfo)d[1]; 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 JsonMessage cm = new JsonMessage(c.Name, MessageType.Status); cm.SetStatusType(StatusType.Connected); SendToAllClients(null, cm, -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 avaible rooms are full"); JsonMessageHelpers.SetJsonMessage(stream, m); return; } // Send The Server Message Buffer foreach (Message message in r.Buffer) { JsonMessageHelpers.SetJsonMessage(stream, message.ToJsonMessage()); } JsonMessage rooms = new JsonMessage("Server", MessageType.Request); rooms.SetRequestType(RequestType.Rooms); rooms.SetContent(Rooms.SerializeRooms()); JsonMessageHelpers.SetJsonMessage(stream, rooms); while (true) { try { c = clients[c.GUID]; if (client.Available < 3) { Thread.Sleep(20); continue; } JsonMessage m = JsonMessageHelpers.GetJsonMessage(stream); foreach (dynamic b in Bots) { m = b.OnJsonMessage(c, m); } if (m.MessageType == MessageType.Message) { TotalMessagesSent += 1; r.AddMesssage(m.ToMessage()); } if (m.MessageType == MessageType.Infomation) { JsonMessageHelpers.SetJsonMessage(stream, InformationHandler(m.InfomationType, c.GUID).ToJsonMessage()); continue; } else if (m.MessageType == MessageType.Status && m.StatusType == StatusType.ChangeRoom) { try { ClientInfo cInfo = clients[c.GUID]; if (!r.AddUser(cInfo)) { m = new JsonMessage("Server", MessageType.Message); m.SetColor(ColorToNColor(Color.Aquamarine)); m.SetContent("The requested room is full"); JsonMessageHelpers.SetJsonMessage(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 JsonMessage("Server", MessageType.Message); m.SetContent("Moved to Room " + id); JsonMessageHelpers.SetJsonMessage(stream, m); foreach (Message message in r.Buffer) { JsonMessageHelpers.SetJsonMessage(stream, message.ToJsonMessage()); } 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; r.AddUser(cInfo); cInfo.SetRoomID(r.ID); clients[c.GUID] = cInfo; m = new JsonMessage("Server", MessageType.Message); m.SetContent("Moved to Room " + r.Name); JsonMessageHelpers.SetJsonMessage(stream, m); foreach (Message message in r.Buffer) { JsonMessageHelpers.SetJsonMessage(stream, message.ToJsonMessage()); } 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) { stream.Close(); stream.Dispose(); foreach (dynamic b in Bots) { b.OnUserDisconnect(c); } r.RemoveUser(clients[c.GUID]); clients.Remove(c.GUID); JsonMessage t = new JsonMessage(c.GUID, MessageType.Status); t.SetStatusType(StatusType.Disconnected); SendToAllClients(null, t, 0); break; } foreach (KeyValuePair <string, ClientInfo> network in clients) { if (network.Key == c.GUID) { continue; } else { if (m.EndPoint == "") { SendToAllClients(null, m, clients[c.GUID].RoomId, c.GUID); break; } else { if (network.Value.Name == m.EndPoint) { JsonMessageHelpers.SetJsonMessage(network.Value.Stream, m); } } } } } catch (Exception e) { JsonMessage error = new JsonMessage("error", MessageType.Status); error.SetStatusType(StatusType.ErrorDisconnect); error.SetContent(e.Message); if (clients.ContainsKey(c.GUID)) { foreach (dynamic b in Bots) { b.OnUserDisconnect(c); } r.RemoveUser(clients[c.GUID]); clients.Remove(c.GUID); } if (stream != null) { JsonMessageHelpers.SetJsonMessage(stream, error); stream.Close(); } break; } } }
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; } } }
static void WebListener(ref ListenerState state) { TcpListener server = new TcpListener(iPAddress, WebPort); server.Start(); while (true) { state = ListenerState.Waiting; TcpClient client = server.AcceptTcpClient(); state = ListenerState.Accepting; //SslStream stream = new SslStream(client.GetStream(), true); //stream.AuthenticateAsServer(X509, false, SslProtocols.Default, true); while (client.Available < 3) { if (state == ListenerState.Quiting) { break; } } if (state == ListenerState.Quiting) { server.Stop(); break; } state = ListenerState.Handshaking; //JsonMessageHelpers.HandleHandshake(client.GetStream(), client.Available); NetworkStream stream = client.GetStream(); JsonMessageHelpers.HandleHandshake(stream); state = ListenerState.UsernameSub; while (client.Available < 3) { if (state == ListenerState.Quiting) { break; } } if (state == ListenerState.Quiting) { server.Stop(); break; } JsonMessage json = JsonMessageHelpers.GetJsonMessage(stream); if (json.MessageType == MessageType.Initionalize && clients.Any(x => x.Value.Name == json.Name) == false) { ClientInfo c = new ClientInfo(json.Name, stream, ClientType.Web); clients.Add(c.GUID, c); ThreadPool.QueueUserWorkItem(HandleClientWebWorker, new object[2] { client, c }); state = ListenerState.Connected; } else { JsonMessage h = new JsonMessage(json.Name, MessageType.Status); h.SetStatusType(StatusType.ErrorDisconnect); h.SetContent("User with the name " + json.Name + " already exsists"); JsonMessageHelpers.SetJsonMessage(stream, h); stream.Close(); stream.Dispose(); client.Close(); client.Dispose(); state = ListenerState.Disconnected; } } }