예제 #1
0
        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();
            }
        }
예제 #2
0
        public static void AddGameSession(netPongInvitation invitation, out int nSessionID)
        {
            netPongUdpGameSession gameSession = new netPongUdpGameSession(m_nNextSessionID, invitation.infoPlayer1.strClientName, invitation.infoPlayer2.strClientName, null, null);

            nSessionID = m_nNextSessionID;
            m_nNextSessionID++;

            lock (m_runningSessionsLock)
            {
                m_runningSessions.Add(gameSession);
            }
        }
예제 #3
0
        private static void SendStartGameCommand(netPongInvitation inv, int nSessionID)
        {
            byte[] byteArgLen     = BitConverter.GetBytes((Int16)10);
            byte[] byteSessionID  = BitConverter.GetBytes((Int32)nSessionID);
            byte[] byteServerPort = BitConverter.GetBytes((Int16)2411);

            byte[] bytePacket = new byte[10];
            bytePacket[0] = (byte)netPongTcpOpCodes.NP_TCP_CMD_STARTGAME;
            bytePacket[1] = byteArgLen[0];
            bytePacket[2] = byteArgLen[1];

            for (int i = 0; i < byteServerPort.Length; i++)
            {
                bytePacket[i + 4] = byteServerPort[i];
            }

            for (int i = 0; i < byteSessionID.Length; i++)
            {
                bytePacket[i + 4 + byteServerPort.Length] = byteSessionID[i];
            }

            bytePacket[3] = 0x01; //player id
            try
            {
                inv.infoPlayer1.hClientTcpSocket.BeginSend(bytePacket, 0, bytePacket.Length, 0, new AsyncCallback(SendStartGameCallback), inv.infoPlayer1.hClientTcpSocket);
            }
            catch (Exception)
            {
            }

            bytePacket[3] = 0x02; //player id
            try
            {
                inv.infoPlayer2.hClientTcpSocket.BeginSend(bytePacket, 0, bytePacket.Length, 0, new AsyncCallback(SendStartGameCallback), inv.infoPlayer2.hClientTcpSocket);
            }
            catch (Exception)
            {
            }
        }
예제 #4
0
        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("|========================");
            }
        }
예제 #5
0
        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("|========================");
            }
        }