public void Start() { bRunning = true; StatusMsgQueue.Enqueue("Preparing Server Lobby"); Lobby = new LobbyListener(LobbyInputQueue, LobbyOutputQueue, sIPAddress, iLobbyPort, Clients.Count); tProcessClientsTimer.Start(); }
private void ProcessClientPackets() { if (LobbyInputQueue.Count > 0) { Packet p = LobbyInputQueue.Dequeue(); //string[] word = p.sMsg.Split("|".ToCharArray()); string sClientSender = p.sClientSource; string s = ""; switch(p.info) { #region NEW CLIENT case Packet.pInfo.NEW_CLIENT: StatusMsgQueue.Enqueue("Receiving New Client"); int iNewPort = (int)(iBasePort + Clients.Count); //Debug.WriteLine("Server: Creating Special Listener"); string sKey = System.Guid.NewGuid().ToString(); Packet p1 = new Packet(Packet.pType.TO_CLIENT, Packet.pInfo.CONNECTION_INFO, Packet.pDelivery.TARGETED, "Server", sKey); p1.AddFieldValue("PORT",((int)(iBasePort + Clients.Count)).ToString()); p1.AddFieldValue("CLIENT KEY",sKey); LobbyOutputQueue.Enqueue(p1); //Debug.WriteLine("Server: Sent Connection Info"); StatusMsgQueue.Enqueue("Adding New Client"); Clients.Add(new SHClient(LobbyInputQueue, sIPAddress, iNewPort, sKey)); //Debug.WriteLine("Server: Done creating client object"); Lobby.StopListening(); StatusMsgQueue.Enqueue("Preparing Server Lobby"); Lobby = new LobbyListener(LobbyInputQueue,LobbyOutputQueue,sIPAddress,iLobbyPort,Clients.Count); //Debug.WriteLine("Server: Done creating new LobbyListener"); break; #endregion #region ADD TO CLIENT LIST case Packet.pInfo.CLIENT_LIST_ADD: StatusMsgQueue.Enqueue("Updating Client List"); ((SHClient)Clients[Clients.Count - 1]).sAlias = p.GetFieldValue("ALIAS"); ; Packet p2 = new Packet( Packet.pType.TO_CLIENT_GUI, Packet.pInfo.CLIENT_LIST_REFRESH, Packet.pDelivery.BROADCAST_ALL, "SERVER", ""); p2.AddFieldValue("CLIENT COUNT",Clients.Count.ToString()); for (int i = 0; i < Clients.Count; i++) p2.AddFieldValue("CLIENT ALIAS "+i.ToString(), ((SHClient)Clients[i]).sAlias); StatusMsgQueue.Enqueue("Sending Client List"); SendPacket(p2); Packet serverP2 = p2.Clone(); serverP2.type = Packet.pType.TO_SERVER_GUI; SendPacket(serverP2); break; #endregion #region ALIAS CHANGE case Packet.pInfo.ALIAS_CHANGE: StatusMsgQueue.Enqueue("Updating Alias"); SHClient t = getClientByKey(sClientSender); if(t!=null) t.sAlias = p.GetFieldValue("ALIAS"); Packet p3 = new Packet( Packet.pType.TO_CLIENT_GUI, Packet.pInfo.CLIENT_LIST_REFRESH, Packet.pDelivery.BROADCAST_ALL, "SERVER", ""); p3.AddFieldValue("CLIENT COUNT",Clients.Count.ToString()); AddClientAliases(p3); SendPacket(p3); //send this to the server gui also p3.type = Packet.pType.TO_SERVER_GUI; SendPacket(p3); break; #endregion #region CHAT MESSAGE case Packet.pInfo.CHAT_MESSAGE: StatusMsgQueue.Enqueue("Forwarding Chat Message"); if (Clients.Count > 1) { Packet p4 = new Packet(Packet.pType.TO_CLIENT_GUI, Packet.pInfo.CHAT_MESSAGE, Packet.pDelivery.BROADCAST_OTHERS, "SERVER", sClientSender); p4.AddFieldValue("CHAT MESSAGE",p.GetFieldValue("CHAT MESSAGE")); SendPacket(p4); //TODO: Add a correction for if the user types in a | in his chat message // piece it back together with hasMoreWords } else { //broadcastToAllClients("CHAT MESSAGE|Server: There are no other clients connected, so stop talking to yourself. kthxbye."); Packet p5 = new Packet( Packet.pType.TO_CLIENT_GUI, Packet.pInfo.CHAT_MESSAGE, Packet.pDelivery.BROADCAST_ALL, "SERVER", ""); p5.AddFieldValue("CHAT MESSAGE","Server: There are no other clients connected, so stop talking to yourself. kthxbye."); SendPacket(p5); } break; #endregion #region CLIENT DISCONNECT case Packet.pInfo.CLIENT_DISCONNECT: Clients.Remove(getClientByKey(p.sClientSource)); Packet p6 = new Packet( Packet.pType.TO_CLIENT_GUI, Packet.pInfo.CHAT_MESSAGE, Packet.pDelivery.BROADCAST_ALL, "SERVER", ""); p6.AddFieldValue("CHAT MESSAGE","Server: " + p.GetFieldValue("ALIAS") + " is no longer connected."); SendPacket(p6); p6 = new Packet( Packet.pType.TO_SERVER_GUI, Packet.pInfo.CLIENT_LIST_REFRESH, Packet.pDelivery.TARGETED, "SERVER", ""); AddClientAliases(p6); SendPacket(p6); break; #endregion #region NEW GAME: case Packet.pInfo.NEW_GAME: Packet p7 = new Packet(Packet.pType.TO_SERVER_GUI, Packet.pInfo.NEW_GAME, Packet.pDelivery.TARGETED, "SERVER", "SERVER_GUI"); p7.AddFieldValue("GAME NAME", p.GetFieldValue("GAME NAME")); p7.AddFieldValue("GAME KEY", p.GetFieldValue("GAME KEY")); SendPacket(p7); p7.type = Packet.pType.TO_CLIENT_GUI; p7.delivery = Packet.pDelivery.BROADCAST_ALL; SendPacket(p7); break; #endregion /* case "DISCONNECT": //int index = Convert.ToInt32(word[1]); //Clients.RemoveAt(index); //lstClients.Items.RemoveAt(index); break; * */ case Packet.pInfo.GAME_DATA: //forwardToOtherClients(new ClientPacket(sClientSender,"GAME DATA|" + word[1] + "|" + word[2] + "|" + word[3])); break; } } }
private void ProcessClientPackets() { if (LobbyInputQueue.Count > 0) { Packet p = LobbyInputQueue.Dequeue(); //string[] word = p.sMsg.Split("|".ToCharArray()); string sClientSender = p.sClientSource; string s = ""; switch (p.info) { #region NEW CLIENT case Packet.pInfo.NEW_CLIENT: StatusMsgQueue.Enqueue("Receiving New Client"); int iNewPort = (int)(iBasePort + Clients.Count); //Debug.WriteLine("Server: Creating Special Listener"); string sKey = System.Guid.NewGuid().ToString(); Packet p1 = new Packet(Packet.pType.TO_CLIENT, Packet.pInfo.CONNECTION_INFO, Packet.pDelivery.TARGETED, "Server", sKey); p1.AddFieldValue("PORT", ((int)(iBasePort + Clients.Count)).ToString()); p1.AddFieldValue("CLIENT KEY", sKey); LobbyOutputQueue.Enqueue(p1); //Debug.WriteLine("Server: Sent Connection Info"); StatusMsgQueue.Enqueue("Adding New Client"); Clients.Add(new SHClient(LobbyInputQueue, sIPAddress, iNewPort, sKey)); //Debug.WriteLine("Server: Done creating client object"); Lobby.StopListening(); StatusMsgQueue.Enqueue("Preparing Server Lobby"); Lobby = new LobbyListener(LobbyInputQueue, LobbyOutputQueue, sIPAddress, iLobbyPort, Clients.Count); //Debug.WriteLine("Server: Done creating new LobbyListener"); break; #endregion #region ADD TO CLIENT LIST case Packet.pInfo.CLIENT_LIST_ADD: StatusMsgQueue.Enqueue("Updating Client List"); ((SHClient)Clients[Clients.Count - 1]).sAlias = p.GetFieldValue("ALIAS");; Packet p2 = new Packet( Packet.pType.TO_CLIENT_GUI, Packet.pInfo.CLIENT_LIST_REFRESH, Packet.pDelivery.BROADCAST_ALL, "SERVER", ""); p2.AddFieldValue("CLIENT COUNT", Clients.Count.ToString()); for (int i = 0; i < Clients.Count; i++) { p2.AddFieldValue("CLIENT ALIAS " + i.ToString(), ((SHClient)Clients[i]).sAlias); } StatusMsgQueue.Enqueue("Sending Client List"); SendPacket(p2); Packet serverP2 = p2.Clone(); serverP2.type = Packet.pType.TO_SERVER_GUI; SendPacket(serverP2); break; #endregion #region ALIAS CHANGE case Packet.pInfo.ALIAS_CHANGE: StatusMsgQueue.Enqueue("Updating Alias"); SHClient t = getClientByKey(sClientSender); if (t != null) { t.sAlias = p.GetFieldValue("ALIAS"); } Packet p3 = new Packet( Packet.pType.TO_CLIENT_GUI, Packet.pInfo.CLIENT_LIST_REFRESH, Packet.pDelivery.BROADCAST_ALL, "SERVER", ""); p3.AddFieldValue("CLIENT COUNT", Clients.Count.ToString()); AddClientAliases(p3); SendPacket(p3); //send this to the server gui also p3.type = Packet.pType.TO_SERVER_GUI; SendPacket(p3); break; #endregion #region CHAT MESSAGE case Packet.pInfo.CHAT_MESSAGE: StatusMsgQueue.Enqueue("Forwarding Chat Message"); if (Clients.Count > 1) { Packet p4 = new Packet(Packet.pType.TO_CLIENT_GUI, Packet.pInfo.CHAT_MESSAGE, Packet.pDelivery.BROADCAST_OTHERS, "SERVER", sClientSender); p4.AddFieldValue("CHAT MESSAGE", p.GetFieldValue("CHAT MESSAGE")); SendPacket(p4); //TODO: Add a correction for if the user types in a | in his chat message // piece it back together with hasMoreWords } else { //broadcastToAllClients("CHAT MESSAGE|Server: There are no other clients connected, so stop talking to yourself. kthxbye."); Packet p5 = new Packet( Packet.pType.TO_CLIENT_GUI, Packet.pInfo.CHAT_MESSAGE, Packet.pDelivery.BROADCAST_ALL, "SERVER", ""); p5.AddFieldValue("CHAT MESSAGE", "Server: There are no other clients connected, so stop talking to yourself. kthxbye."); SendPacket(p5); } break; #endregion #region CLIENT DISCONNECT case Packet.pInfo.CLIENT_DISCONNECT: Clients.Remove(getClientByKey(p.sClientSource)); Packet p6 = new Packet( Packet.pType.TO_CLIENT_GUI, Packet.pInfo.CHAT_MESSAGE, Packet.pDelivery.BROADCAST_ALL, "SERVER", ""); p6.AddFieldValue("CHAT MESSAGE", "Server: " + p.GetFieldValue("ALIAS") + " is no longer connected."); SendPacket(p6); p6 = new Packet( Packet.pType.TO_SERVER_GUI, Packet.pInfo.CLIENT_LIST_REFRESH, Packet.pDelivery.TARGETED, "SERVER", ""); AddClientAliases(p6); SendPacket(p6); break; #endregion #region NEW GAME: case Packet.pInfo.NEW_GAME: Packet p7 = new Packet(Packet.pType.TO_SERVER_GUI, Packet.pInfo.NEW_GAME, Packet.pDelivery.TARGETED, "SERVER", "SERVER_GUI"); p7.AddFieldValue("GAME NAME", p.GetFieldValue("GAME NAME")); p7.AddFieldValue("GAME KEY", p.GetFieldValue("GAME KEY")); SendPacket(p7); p7.type = Packet.pType.TO_CLIENT_GUI; p7.delivery = Packet.pDelivery.BROADCAST_ALL; SendPacket(p7); break; #endregion /* * case "DISCONNECT": * //int index = Convert.ToInt32(word[1]); * //Clients.RemoveAt(index); * //lstClients.Items.RemoveAt(index); * break; * */ case Packet.pInfo.GAME_DATA: //forwardToOtherClients(new ClientPacket(sClientSender,"GAME DATA|" + word[1] + "|" + word[2] + "|" + word[3])); break; } } }