Ejemplo n.º 1
0
        private void CheckActivity(object sender, EventArgs e)
        {
            // Check buffer for TCP messages
            try
            {
                EndPoint localEndPoint     = remoteEndPoint;
                byte[]   ReceiveBuffer     = new byte[1024];
                int      iReceiveByteCount = tcpSocket.connectionSocket.ReceiveFrom(ReceiveBuffer, ref localEndPoint);
                byte[]   data = new byte[iReceiveByteCount];
                Array.Copy(ReceiveBuffer, data, iReceiveByteCount); //make sure byte[] is the same length as the recieved byte[]

                // If there is a message deserialize it and pass it into the TCP Message Manager
                if (0 < iReceiveByteCount)
                {
                    try
                    {
                        GameMessageStruct gameMessage = PDU.DeserializeStruct(data); //convert byte[] to structure so it can be read

                        TCPMessageManager(gameMessage);
                    }
                    catch (Exception error)
                    {
                        Console.WriteLine(error.Message);
                    }
                }
            }
            catch
            {
            }
        }
Ejemplo n.º 2
0
        private void CheckTCPActivity(object sender, EventArgs myEventArgs)
        {
            // Check TCP Connections Backlog for connection requests
            try
            {
                if (playerNumber < 2)
                {
                    try
                    {
                        // Add each player as a new socket
                        currentGame[playerNumber]          = tcpSocket.connectionSocket.Accept();
                        currentGame[playerNumber].Blocking = false;
                        playerNumber++;

                        if (playerNumber == 2)
                        {
                            gameStarted = true;
                        }
                    }
                    catch (SocketException se)                              // Handle socket-related exception
                    {                                                       // If an exception occurs, display an error message
                        if (10053 == se.ErrorCode || 10054 == se.ErrorCode) // Remote end closed the connection
                        {
                            tcpSocket.CloseSocket();
                        }
                        else if (10035 != se.ErrorCode)
                        {   // Ignore error messages relating to normal behaviour of non-blocking sockets
                            MessageBox.Show(se.Message);
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                }
                else if (gameStarted == true)
                {
                    // Diagnostics message
                    listBoxStatus.Items.Add("Game Has Started");
                    gameStarted = false;

                    // Send same list of characters to each client
                    GameMessageStruct gameMessage = new GameMessageStruct
                    {
                        messageType = 0,
                        nameslist   = gameManagement.DisplayPeople()
                    };

                    byte[] data = PDU.SerializeStruct(gameMessage);

                    foreach (Socket client in currentGame)
                    {
                        client.Send(data, SocketFlags.None);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            // Then check for game activity
            for (int i = 0; i < 2; i++)
            {
                try
                {
                    EndPoint localEndPoint     = tcpSocket.endPoint;
                    byte[]   ReceiveBuffer     = new byte[1024];
                    int      iReceiveByteCount = currentGame[i].ReceiveFrom(ReceiveBuffer, ref localEndPoint);
                    byte[]   data = new byte[iReceiveByteCount];
                    Array.Copy(ReceiveBuffer, data, iReceiveByteCount); //make sure byte[] is the same length as the recieved byte[]

                    if (0 < iReceiveByteCount)
                    {
                        try
                        {
                            GameMessageStruct gameMessage = PDU.DeserializeStruct(data); //convert byte[] to structure so it can be read

                            TCPMessageManager(gameMessage);
                        }
                        catch (Exception error)
                        {
                            Console.WriteLine(error.Message);
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
        }