private Packet AddClientAliases(Packet p) { for (int i = 0; i < Clients.Count; i++) { p.AddFieldValue("CLIENT ALIAS " + i.ToString(), ((SHClient)Clients[i]).sAlias); } return(p); }
private void ProcessServerPackets(object sender, ElapsedEventArgs e) { if (InputQueue.Count > 0) { //string data = InputQueue.Dequeue().sMsg; //string[] word = data.Split("|".ToCharArray()); Packet p = new Packet(); if (InputQueue.Peek().type == Packet.pType.TO_CLIENT) { p.FromString(InputQueue.Dequeue().ToString()); switch (p.info) { #region CONNECTION INFO case Packet.pInfo.CONNECTION_INFO: this.iPort = Convert.ToInt32(p.GetFieldValue("PORT")); this.sKey = p.GetFieldValue("CLIENT KEY"); stm.Close(); tcpclnt.Close(); stm.Dispose(); ConnectToServer(false, sAlias); p.type = Packet.pType.TO_CLIENT_GUI; OutputQueue.Enqueue(p); Packet p1 = new Packet(Packet.pType.TO_SERVER, Packet.pInfo.CLIENT_LIST_ADD, Packet.pDelivery.TARGETED, this.sKey, ""); p1.AddFieldValue("ALIAS", sAlias); OutputQueue.Enqueue(p1); bConnected = true; break; #endregion case Packet.pInfo.CLIENT_LIST_REFRESH: break; /* #region NEW GAME * case Packet.pInfo.NEW_GAME: * * SendPacket(new Packet(Packet.pType.TO_CLIENT_GUI, * Packet.pInfo.NEW_GAME, * Packet.pDelivery.TARGETED, * "", * "", * p.sMessage); * break; #endregion */ } } } }
public void Disconnect() { Packet p = new Packet( Packet.pType.TO_SERVER, Packet.pInfo.CLIENT_DISCONNECT, Packet.pDelivery.BROADCAST_OTHERS, this.sKey, "SERVER"); p.AddFieldValue("ALIAS", sAlias); SendPacket(p); CloseConnection(); }
public void SendChatMessage(string sMsg) { StatusMsgQueue.Enqueue("Sending Message"); Packet p = new Packet( Packet.pType.TO_SERVER, Packet.pInfo.CHAT_MESSAGE, Packet.pDelivery.BROADCAST_OTHERS, this.sKey, "SERVER"); p.AddFieldValue("CHAT MESSAGE", sMsg); OutputQueue.Enqueue(p); }
public void Stop() { Lobby.StopListening(); for (int i = 0; i < Clients.Count; i++) { ((SHClient)Clients[i]).StopClient(); } Packet p = new Packet( Packet.pType.TO_CLIENT, Packet.pInfo.CONNECTION_INFO, Packet.pDelivery.BROADCAST_ALL, "SERVER", ""); p.AddFieldValue("ACTION", "STOPPING"); SendPacket(p); }
private void ConnectToServer(bool bToLobby, string alias) { ProcessServerDataTimer.Stop(); while ((System.Diagnostics.ThreadState)ServerWriter.ThreadState == System.Diagnostics.ThreadState.Running || (System.Diagnostics.ThreadState)ServerReader.ThreadState == System.Diagnostics.ThreadState.Running) { bStopThreads = true; } ServerReader = new Thread(new ThreadStart(ReadServer)); ServerWriter = new Thread(new ThreadStart(WriteServer)); if (Setup()) { ProcessServerDataTimer.Start(); ServerWriter.Start(); ServerReader.Start(); if (bToLobby) { Packet p = new Packet(Packet.pType.TO_SERVER, Packet.pInfo.NEW_CLIENT, Packet.pDelivery.TARGETED, "nullKey", ""); p.AddFieldValue("ALIAS", "nullAlias"); OutputQueue.Enqueue(p); } else { StatusMsgQueue.Enqueue("Reconnecting On Designated Port:" + iPort.ToString()); } } else { /* * if (bToLobby) * MessageBox.Show("Unable to connect to Server. Verify server has been started"); * else * MessageBox.Show("Unable to connect on Designated Client Port: "+ iPort.ToString()+"\nCheck Firewall and Router settings and verify that this \nport is not currently being used by other programs."); * */ } }
private void ProcessServerPackets(object sender, ElapsedEventArgs e) { if (InputQueue.Count > 0) { //string data = InputQueue.Dequeue().sMsg; //string[] word = data.Split("|".ToCharArray()); Packet p = new Packet(); if (InputQueue.Peek().type == Packet.pType.TO_CLIENT) { p.FromString(InputQueue.Dequeue().ToString()); switch (p.info) { #region CONNECTION INFO case Packet.pInfo.CONNECTION_INFO: this.iPort = Convert.ToInt32(p.GetFieldValue("PORT")); this.sKey = p.GetFieldValue("CLIENT KEY"); stm.Close(); tcpclnt.Close(); stm.Dispose(); ConnectToServer(false, sAlias); p.type = Packet.pType.TO_CLIENT_GUI; OutputQueue.Enqueue(p); Packet p1 = new Packet(Packet.pType.TO_SERVER, Packet.pInfo.CLIENT_LIST_ADD, Packet.pDelivery.TARGETED, this.sKey, ""); p1.AddFieldValue("ALIAS",sAlias); OutputQueue.Enqueue(p1); bConnected = true; break; #endregion case Packet.pInfo.CLIENT_LIST_REFRESH: break; /* #region NEW GAME case Packet.pInfo.NEW_GAME: SendPacket(new Packet(Packet.pType.TO_CLIENT_GUI, Packet.pInfo.NEW_GAME, Packet.pDelivery.TARGETED, "", "", p.sMessage); break; #endregion */ } } } }
private void ConnectToServer(bool bToLobby, string alias) { ProcessServerDataTimer.Stop(); while ((System.Diagnostics.ThreadState)ServerWriter.ThreadState == System.Diagnostics.ThreadState.Running || (System.Diagnostics.ThreadState)ServerReader.ThreadState == System.Diagnostics.ThreadState.Running) { bStopThreads = true; } ServerReader = new Thread(new ThreadStart(ReadServer)); ServerWriter = new Thread(new ThreadStart(WriteServer)); if (Setup()) { ProcessServerDataTimer.Start(); ServerWriter.Start(); ServerReader.Start(); if (bToLobby) { Packet p = new Packet(Packet.pType.TO_SERVER, Packet.pInfo.NEW_CLIENT, Packet.pDelivery.TARGETED, "nullKey", ""); p.AddFieldValue("ALIAS","nullAlias"); OutputQueue.Enqueue(p); } else StatusMsgQueue.Enqueue("Reconnecting On Designated Port:"+ iPort.ToString()); } else { /* if (bToLobby) MessageBox.Show("Unable to connect to Server. Verify server has been started"); else MessageBox.Show("Unable to connect on Designated Client Port: "+ iPort.ToString()+"\nCheck Firewall and Router settings and verify that this \nport is not currently being used by other programs."); * */ } }
public void SendChatMessage(string sMsg) { StatusMsgQueue.Enqueue("Sending Message"); Packet p = new Packet( Packet.pType.TO_SERVER, Packet.pInfo.CHAT_MESSAGE, Packet.pDelivery.BROADCAST_OTHERS, this.sKey, "SERVER"); p.AddFieldValue("CHAT MESSAGE", sMsg); OutputQueue.Enqueue(p); }
public void Disconnect() { Packet p = new Packet( Packet.pType.TO_SERVER, Packet.pInfo.CLIENT_DISCONNECT, Packet.pDelivery.BROADCAST_OTHERS, this.sKey, "SERVER"); p.AddFieldValue("ALIAS", sAlias); SendPacket(p); CloseConnection(); }
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; } } }
private Packet AddClientAliases(Packet p) { for (int i = 0; i < Clients.Count; i++) p.AddFieldValue("CLIENT ALIAS " + i.ToString(), ((SHClient)Clients[i]).sAlias); return p; }
public void Stop() { Lobby.StopListening(); for (int i = 0; i < Clients.Count; i++) { ((SHClient)Clients[i]).StopClient(); } Packet p = new Packet( Packet.pType.TO_CLIENT, Packet.pInfo.CONNECTION_INFO, Packet.pDelivery.BROADCAST_ALL, "SERVER", ""); p.AddFieldValue("ACTION","STOPPING"); SendPacket(p); }