Exemple #1
0
        private static void ParseChangePaddlePos(netPongUDPParsePacketThreadParam threadParam)
        {
            netPongUdpGameSession gameSession;

            if (threadParam.gameSessionIndex >= m_runningSessions.Count)
            {
                return;
            }

            lock (m_runningSessionsLock)
            {
                gameSession = m_runningSessions[threadParam.gameSessionIndex];
            }

            if (threadParam.packetData[3] == 0x1)
            {
                gameSession.fPlayer1PaddleXPos = (float)BitConverter.ToSingle(threadParam.packetData, 4);
                lock (m_runningSessionsLock)
                {
                    m_runningSessions[threadParam.gameSessionIndex] = gameSession;
                }
                SendSetPaddlePosCommand(gameSession.fPlayer1PaddleXPos, gameSession.epPlayer2);
            }
            else if (threadParam.packetData[3] == 0x2)
            {
                gameSession.fPlayer2PaddleXPos = (-1.0f) * (float)BitConverter.ToSingle(threadParam.packetData, 4);
                lock (m_runningSessionsLock)
                {
                    m_runningSessions[threadParam.gameSessionIndex] = gameSession;
                }
                SendSetPaddlePosCommand(gameSession.fPlayer2PaddleXPos, gameSession.epPlayer1);
            }
        }
Exemple #2
0
        private static void ParsePacket(object param)
        {
            netPongUDPParsePacketThreadParam threadParam = (netPongUDPParsePacketThreadParam)param;

            if (threadParam.packetData == null)
            {
                return;
            }

            if (threadParam.packetData.Length < 3)
            {
                return;
            }

            int nOpCode = (int)threadParam.packetData[2];

            switch (nOpCode)
            {
            case (int)netPongUdpOpCodes.NP_UDP_CMD_READY:
                ParseReadyCommand(threadParam);
                break;

            case (int)netPongUdpOpCodes.NP_UDP_CMD_CHANGE_PADDLE_POS:
                ParseChangePaddlePos(threadParam);
                break;

            default:
                break;
            }
        }
Exemple #3
0
        private static void ParseReadyCommand(netPongUDPParsePacketThreadParam threadParam)
        {
            byte[] packet     = threadParam.packetData;
            byte   nPlayerID  = packet[3];
            Int32  nSessionID = BitConverter.ToInt32(packet, 4);

            lock (m_runningSessionsLock)
            {
                for (int i = 0; i < m_runningSessions.Count; i++)
                {
                    if (m_runningSessions[i].nSessionID == nSessionID)
                    {
                        netPongUdpGameSession session = m_runningSessions[i];
                        if (nPlayerID == 1)
                        {
                            //console-output
                            Console.WriteLine("");
                            Console.WriteLine("|========================");
                            Console.WriteLine("| - netPong UDP Game Server");
                            Console.WriteLine("|  * Player 1 confirmed UDP Connection ");
                            Console.WriteLine("|========================");

                            session.epPlayer1    = threadParam.senderRemoteEP;
                            m_runningSessions[i] = session;
                        }
                        else if (nPlayerID == 2)
                        {
                            //console-output
                            Console.WriteLine("");
                            Console.WriteLine("|========================");
                            Console.WriteLine("| - netPong UDP Game Server");
                            Console.WriteLine("|  * Player 2 confirmed UDP Connection ");
                            Console.WriteLine("|========================");

                            session.epPlayer2    = threadParam.senderRemoteEP;
                            m_runningSessions[i] = session;
                        }
                        m_runningSessions[i] = session;

                        if ((session.epPlayer1 != null) && (session.epPlayer2 != null))
                        {
                            //console-output
                            Console.WriteLine("");
                            Console.WriteLine("|========================");
                            Console.WriteLine("| - netPong UDP Game Server");
                            Console.WriteLine("|  * Starting BallCalculationThread for Session ID " + session.nSessionID);
                            Console.WriteLine("|========================");

                            session.bIsInGame       = true;
                            session.hBallCalcThread = new Thread(new ParameterizedThreadStart(BallCalculationThread));
                            session.hBallCalcThread.IsBackground = true;
                            m_runningSessions[i] = session;
                            session.hBallCalcThread.Start(i);
                        }
                        break;
                    }
                }
            }
        }
Exemple #4
0
        private static void ReadData(IAsyncResult ar)
        {
            IPEndPoint senderRemoteEP = new IPEndPoint(IPAddress.Any, 0);

            byte[] bytePacket = null;

            try
            {
                lock (m_udpClientLock)
                {
                    bytePacket = m_udpClient.EndReceive(ar, ref senderRemoteEP);
                }
            }
            catch (Exception err)
            {
                //console-output
                Console.WriteLine("");
                Console.WriteLine("|========================");
                Console.WriteLine("| - netPong UDP Game Server");
                Console.WriteLine("|  * Error while recieving: ");
                Console.WriteLine("|    " + err.ToString());
                Console.WriteLine("|========================");
            }

            int gameSessionIndexc;

            IsValidSender(senderRemoteEP, out gameSessionIndexc);
            Thread parseThread = new Thread(new ParameterizedThreadStart(ParsePacket));
            netPongUDPParsePacketThreadParam parseThreadParam = new netPongUDPParsePacketThreadParam(senderRemoteEP, gameSessionIndexc, bytePacket);

            parseThread.Start(parseThreadParam);

            lock (m_udpClientLock)
            {
                try
                {
                    m_udpClient.BeginReceive(new AsyncCallback(ReadData), null);
                }
                catch (Exception)
                {
                }
            }
        }