private static void NotifyAllNewClientConnected() { for (int i = 0; i < connectedClients.Count; i++) { netPongClientInfo cliInfo = (netPongClientInfo)connectedClients[i]; SendNewClientConnectedCommand(cliInfo.hClientTcpSocket); } }
private static void SendWelcomeCommand(Socket handler, byte[] data) { String strPlayerName = ""; byte[] nPlayerName = new byte[data.GetLength(0) - 3]; int nData = 3; for (int i = 0; i < data.GetLength(0) - 3; i++) { nPlayerName[i] = data[nData]; nData++; } if (!MakePlayerName(nPlayerName, ref strPlayerName)) { return; } if (!IsPlayerNameAvailable(strPlayerName)) { return; } byte[] welcomePacket = new byte[3]; welcomePacket[0] = (byte)netPongTcpOpCodes.NP_TCP_CMD_WELCOME; welcomePacket[1] = 0x00; welcomePacket[2] = 0x00; netPongClientInfo cliInfo = new netPongClientInfo(); cliInfo.strClientName = strPlayerName; cliInfo.hClientTcpSocket = handler; //console-output Console.WriteLine(""); Console.WriteLine("|========================"); Console.WriteLine("| - Sending WELCOME command"); Console.WriteLine("| * Info : " + handler.RemoteEndPoint.ToString()); Console.WriteLine("| * Player Name: " + cliInfo.strClientName); Console.WriteLine("|========================"); try { handler.BeginSend(welcomePacket, 0, welcomePacket.Length, 0, new AsyncCallback(SendWelcomeCallback), cliInfo); } catch (Exception) { try { handler.Shutdown(SocketShutdown.Both); handler.Close(); CleanDisconnectedClients(); } catch (Exception) { } } }
private static void CleanDisconnectedClients() { bool bRefresh = false; ArrayList tmpCon = new ArrayList(); for (int i = 0; i < connectedClients.Count; i++) { netPongClientInfo cliInfo = (netPongClientInfo)connectedClients[i]; if (cliInfo.hClientTcpSocket.Connected) { tmpCon.Add(cliInfo); } else { bRefresh = true; try { cliInfo.hClientTcpSocket.Shutdown(SocketShutdown.Both); cliInfo.hClientTcpSocket.Close(); } catch (Exception) { } //console-output Console.WriteLine(""); Console.WriteLine("|========================"); Console.WriteLine("| - Player disconnected"); Console.WriteLine("| * PlayerName : " + cliInfo.strClientName); Console.WriteLine("|========================"); ArrayList tmpInv = new ArrayList(); for (int x = 0; x < openInvitations.Count; x++) { netPongInvitation inv = (netPongInvitation)openInvitations[x]; if (inv.infoPlayer1.strClientName.Equals(cliInfo.strClientName) || inv.infoPlayer2.strClientName.Equals(cliInfo.strClientName)) { continue; } else { tmpInv.Add(inv); } } openInvitations = tmpInv; } } connectedClients = tmpCon; if (bRefresh) { NotifyAllNewClientConnected(); } }
private static bool IsPlayerNameAvailable(string strPlayerName) { for (int i = 0; i < connectedClients.Count; i++) { netPongClientInfo cliInfo = (netPongClientInfo)connectedClients[i]; if (cliInfo.strClientName.Equals(strPlayerName)) { return(false); } } return(true); }
private static bool GetClientInfoFromName(string strClientName, out netPongClientInfo clientInfo) { for (int i = 0; i < connectedClients.Count; i++) { if (((netPongClientInfo)connectedClients[i]).strClientName.Equals(strClientName)) { clientInfo = (netPongClientInfo)connectedClients[i]; return(true); } } clientInfo = new netPongClientInfo(); return(false); }
private static void SendWelcomeCallback(IAsyncResult ar) { // Retrieve the socket from the state object. netPongClientInfo cliInfo = (netPongClientInfo)ar.AsyncState; try { // Complete sending the data to the remote device. int bytesSent = cliInfo.hClientTcpSocket.EndSend(ar); //console-output Console.WriteLine(""); Console.WriteLine("|========================"); Console.WriteLine("| - Client connected"); Console.WriteLine("| * Info : " + cliInfo.hClientTcpSocket.RemoteEndPoint.ToString()); Console.WriteLine("| * Player Name: " + cliInfo.strClientName); Console.WriteLine("|========================"); NotifyAllNewClientConnected(); connectedClients.Add(cliInfo); } catch (Exception e) { //console-output Console.WriteLine(""); Console.WriteLine("|========================"); Console.WriteLine("| - Error sending WELCOME command"); Console.WriteLine("| * Exception : " + e.ToString()); Console.WriteLine("| * Info : " + cliInfo.hClientTcpSocket.RemoteEndPoint.ToString()); Console.WriteLine("| * Player Name: " + cliInfo.strClientName); Console.WriteLine("|========================"); cliInfo.hClientTcpSocket.Shutdown(SocketShutdown.Both); cliInfo.hClientTcpSocket.Close(); } // Create the state object. StateObject state = new StateObject(); state.workSocket = cliInfo.hClientTcpSocket; state.neededBytes = 3; state.recvData = new ArrayList(); cliInfo.hClientTcpSocket.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); }
private static void SendPlayerInviteCommand(byte[] nPacket, Socket hSenderSocket) { byte[] arg = new byte[nPacket.Length - 3]; for (int i = 0; i < arg.Length; i++) { arg[i] = nPacket[i + 3]; } string strRecvName = ""; if (!MakePlayerName(arg, ref strRecvName)) { //console-output Console.WriteLine(""); Console.WriteLine("|========================"); Console.WriteLine("| - Reciever not found"); Console.WriteLine("| * PlayerName : " + strRecvName); Console.WriteLine("| * Invitation not sent"); Console.WriteLine("|========================"); } Socket recvSock = null; string strSenderName = ""; for (int i = 0; i < connectedClients.Count; i++) { netPongClientInfo cliInfo = (netPongClientInfo)connectedClients[i]; if (cliInfo.strClientName.Equals(strRecvName)) { recvSock = cliInfo.hClientTcpSocket; } if (cliInfo.hClientTcpSocket.Equals(hSenderSocket)) { strSenderName = cliInfo.strClientName; } } if ((recvSock == null) || strSenderName.Equals("")) { //console-output Console.WriteLine(""); Console.WriteLine("|========================"); Console.WriteLine("| - Client not connected"); Console.WriteLine("| * PlayerName : " + strRecvName); Console.WriteLine("| * Invitation not sent"); Console.WriteLine("|========================"); return; } byte[] byteData = null; try { byteData = Encoding.ASCII.GetBytes(strSenderName); } catch (Exception e) { //console-output Console.WriteLine(""); Console.WriteLine("|========================"); Console.WriteLine("| - Error"); Console.WriteLine("| * Info : " + e.ToString()); Console.WriteLine("|========================"); return; } bool bAddNewSession = false; netPongInvitation newSessionInvitation = new netPongInvitation(); int newSessionID = -1; for (int i = 0; i < openInvitations.Count; i++) { netPongInvitation inv = (netPongInvitation)openInvitations[i]; // invitation already exists, must be a confirm message if ((inv.infoPlayer1.strClientName.Equals(strSenderName) || inv.infoPlayer2.strClientName.Equals(strSenderName)) && (inv.infoPlayer1.strClientName.Equals(strRecvName) || inv.infoPlayer2.strClientName.Equals(strRecvName)) ) { if ((inv.infoPlayer1.strClientName.Equals(strSenderName) && inv.isConfirmedPlayer1) || (inv.infoPlayer2.strClientName.Equals(strSenderName) && inv.isConfirmedPlayer2)) { //console-output Console.WriteLine(""); Console.WriteLine("|========================"); Console.WriteLine("| - An invitation already exits between " + strRecvName + " and " + strSenderName); Console.WriteLine("|========================"); return; } //console-output Console.WriteLine(""); Console.WriteLine("|========================"); Console.WriteLine("| - Invitation confirmed between " + strRecvName + " and " + strSenderName); Console.WriteLine("|========================"); bAddNewSession = true; newSessionInvitation = inv; netPongUdpGameServer.AddGameSession(newSessionInvitation, out newSessionID); break; } //check if one of the player is already in a game if (inv.isConfirmedPlayer1) { if (inv.infoPlayer1.strClientName.Equals(strSenderName) || inv.infoPlayer1.strClientName.Equals(strRecvName)) { //console-output Console.WriteLine(""); Console.WriteLine("|========================"); Console.WriteLine("| - Error: cannot send invitation, player " + inv.infoPlayer1.strClientName + " is already in a game"); Console.WriteLine("|========================"); return; } } if (inv.isConfirmedPlayer2) { if (inv.infoPlayer2.strClientName.Equals(strSenderName) || inv.infoPlayer2.strClientName.Equals(strRecvName)) { //console-output Console.WriteLine(""); Console.WriteLine("|========================"); Console.WriteLine("| - Error: cannot send invitation, player " + inv.infoPlayer2.strClientName + " is already in a game"); Console.WriteLine("|========================"); return; } } } byte[] nSendPacket = new byte[byteData.GetLength(0) + 3]; nSendPacket[0] = (byte)netPongTcpOpCodes.NP_TCP_CMD_PLAYERINVITE; nSendPacket[1] = 0x00; nSendPacket[2] = (byte)byteData.Length; for (int i = 0; i < byteData.Length; i++) { nSendPacket[i + 3] = byteData[i]; } if (!bAddNewSession) { netPongInvitation newInv = new netPongInvitation(); netPongClientInfo player1Info = new netPongClientInfo(); GetClientInfoFromName(strSenderName, out player1Info); newInv.infoPlayer1 = player1Info; newInv.isConfirmedPlayer1 = true; netPongClientInfo player2Info = new netPongClientInfo(); GetClientInfoFromName(strRecvName, out player2Info); newInv.infoPlayer2 = player2Info; newInv.isConfirmedPlayer2 = false; openInvitations.Add(newInv); } playerInviteCallbackParam param = new playerInviteCallbackParam(); param.bAddNewSession = false; param.hSocket = recvSock; if (bAddNewSession) { param.bAddNewSession = true; param.nNewSessionID = newSessionID; param.newInvitation = newSessionInvitation; } try { // Begin sending the data to the remote device. recvSock.BeginSend(nSendPacket, 0, nSendPacket.Length, 0, new AsyncCallback(SendPlayerInviteCallback), param); } catch (Exception e) { //console-output Console.WriteLine(""); Console.WriteLine("|========================"); Console.WriteLine("| - Error"); Console.WriteLine("| * Info : " + e.ToString()); Console.WriteLine("|========================"); } }
private static void SendRejectInvitationCommand(byte[] nPacket, Socket hSenderSocket) { byte[] arg = new byte[nPacket.Length - 3]; for (int i = 0; i < arg.Length; i++) { arg[i] = nPacket[i + 3]; } string strRecvName = ""; if (!MakePlayerName(arg, ref strRecvName)) { //console-output Console.WriteLine(""); Console.WriteLine("|========================"); Console.WriteLine("| - Reciever not found"); Console.WriteLine("| * PlayerName : " + strRecvName); Console.WriteLine("| * Invitation not sent"); Console.WriteLine("|========================"); } Socket recvSock = null; string strSenderName = ""; for (int i = 0; i < connectedClients.Count; i++) { netPongClientInfo cliInfo = (netPongClientInfo)connectedClients[i]; if (cliInfo.strClientName.Equals(strRecvName)) { recvSock = cliInfo.hClientTcpSocket; } if (cliInfo.hClientTcpSocket.Equals(hSenderSocket)) { strSenderName = cliInfo.strClientName; } } if ((recvSock == null) || strSenderName.Equals("")) { //console-output Console.WriteLine(""); Console.WriteLine("|========================"); Console.WriteLine("| - Client not connected"); Console.WriteLine("| * PlayerName : " + strRecvName); Console.WriteLine("| * Reject Invitation not sent"); Console.WriteLine("|========================"); return; } byte[] byteSender = null; try { byteSender = Encoding.ASCII.GetBytes(strSenderName); } catch (Exception e) { //console-output Console.WriteLine(""); Console.WriteLine("|========================"); Console.WriteLine("| - Error"); Console.WriteLine("| * Info : " + e.ToString()); Console.WriteLine("|========================"); return; } for (int i = 0; i < openInvitations.Count; i++) { netPongInvitation inv = (netPongInvitation)openInvitations[i]; // invitation exists if ((inv.infoPlayer1.strClientName.Equals(strSenderName) || inv.infoPlayer2.strClientName.Equals(strSenderName)) && (inv.infoPlayer1.strClientName.Equals(strRecvName) || inv.infoPlayer2.strClientName.Equals(strRecvName)) ) { openInvitations.RemoveAt(i); break; } } byte[] nSendPacket = new byte[byteSender.GetLength(0) + 3]; nSendPacket[0] = (byte)netPongTcpOpCodes.NP_TCP_CMD_REJECTINVITATION; nSendPacket[1] = 0x00; nSendPacket[2] = (byte)byteSender.Length; for (int i = 0; i < byteSender.Length; i++) { nSendPacket[i + 3] = byteSender[i]; } try { // Begin sending the data to the remote device. recvSock.BeginSend(nSendPacket, 0, nSendPacket.Length, 0, new AsyncCallback(SendRejectInviteCallback), recvSock); } catch (Exception e) { //console-output Console.WriteLine(""); Console.WriteLine("|========================"); Console.WriteLine("| - Error"); Console.WriteLine("| * Info : " + e.ToString()); Console.WriteLine("|========================"); } }
private static void ParseTextMessageCommand(byte[] nPacket, Socket sender) { byte nUsrLen = nPacket[1]; byte nMsgLen = nPacket[2]; byte[] bytesUsr = new byte[nUsrLen]; string strRecv = ""; int nPos = 3; for (int i = 0; i < nUsrLen; i++) { bytesUsr[i] = nPacket[nPos]; nPos++; } if (!MakePlayerName(bytesUsr, ref strRecv)) { return; } byte[] bytesMsg = new byte[nMsgLen]; for (int i = 0; i < nMsgLen; i++) { bytesMsg[i] = nPacket[nPos]; nPos++; } string sendername = ""; Socket recvSock = null; for (int i = 0; i < connectedClients.Count; i++) { netPongClientInfo cliInfo = (netPongClientInfo)connectedClients[i]; if (cliInfo.hClientTcpSocket.Equals(sender)) { sendername = cliInfo.strClientName; } if (cliInfo.strClientName.Equals(strRecv)) { recvSock = cliInfo.hClientTcpSocket; } } if (sendername.Equals("") || (recvSock == null)) { return; } byte[] byteSender = null; try { byteSender = Encoding.ASCII.GetBytes(sendername); } catch (Exception e) { //console-output Console.WriteLine(""); Console.WriteLine("|========================"); Console.WriteLine("| - Error"); Console.WriteLine("| * Info : " + e.ToString()); Console.WriteLine("|========================"); return; } SendTextMessageCommand(recvSock, byteSender, bytesMsg); }