Exemplo n.º 1
0
        private Messages.MESSAGE DeSerialize(byte[] received)
        {
            IntPtr ip = Marshal.AllocHGlobal(received.Length);                                                   //allocate unmanaged memory

            Marshal.Copy(received, 0, ip, received.Length);                                                      //copy the byte[] to the unmanaged memory
            Messages.MESSAGE structure = (Messages.MESSAGE)Marshal.PtrToStructure(ip, typeof(Messages.MESSAGE)); //Marshal the unmanaged memory contents to the Message_PDU structure
            Marshal.FreeHGlobal(ip);                                                                             //free unmanaged memory
            return(structure);
        }
Exemplo n.º 2
0
        private void periodicActivityCheck(object myObject, EventArgs myEventArgs)
        {
            //ACCEPT CONNECTIONS
            Socket tempSocket = tcp.tcp_acceptConnection();

            if (tempSocket != null)
            {
                Client client = new Client();
                client.ConnectionSocket = tempSocket;
                ConnectedClients.Add(client);
                txt_clients.Text = ConnectedClients.Count.ToString();
            }

            //RECIEVE MESSAGES
            for (int i = 0; i < ConnectedClients.Count; i++)
            {
                Messages.MESSAGE msg = tcp.receiveMessage(ConnectedClients[i].ConnectionSocket);
                #region MESSAGE DECODER
                switch (msg.msgID)
                {
                //Handle Error Message (Closed connection)
                case -1:
                    ConnectedClients.RemoveAt(i);
                    txt_clients.Text = ConnectedClients.Count.ToString();
                    txt_output.AppendText("Player Disconnected \n");
                    updatePlayersList();
                    break;

                case 0:
                    break;

                //Initialise player
                case 1:
                    decoder.initialiseClient(msg, ConnectedClients[i]);
                    updatePlayersList();
                    decoder.updateAllPlayersInfo();
                    break;

                //Request to invite player recieved
                case 4:
                    txt_output.AppendText("recieved request \n");
                    decoder.inviteClient(ConnectedClients[i].ClientID, msg);
                    break;

                //invitation response recieved
                case 6:
                    decoder.invitationResponse(msg);
                    break;

                default:     //pass message to appriopriate game sessession
                    getGameSession(msg.gameSession).getMessage(msg, ConnectedClients[i].ClientID);
                    break;
                }
                #endregion MESSAGE DECODER
            }
        }
Exemplo n.º 3
0
        public Messages.MESSAGE receiveMessage(Socket clientConnectionSocket)
        {
            Messages.MESSAGE msg = new Messages.MESSAGE();

            try
            {
                EndPoint remoteEndPoint = (EndPoint)localEndPoint;     //case IPEndPoint to EndPoint
                byte[]   recBuffer      = new byte[1024];              //Initialise Buffer for recived messages
                int      recByteCount;
                recByteCount = clientConnectionSocket.ReceiveFrom(recBuffer, ref remoteEndPoint);
                byte[] data = new byte[recByteCount];
                Array.Copy(recBuffer, data, recByteCount);

                if (recByteCount > 0)
                {
                    msg = DeSerialize(data);
                }
                else
                {
                    msg.msgID = 0;
                }
            }
            catch (SocketException se)
            {
                // If an exception occurs, display an error message
                if (10053 == se.ErrorCode || 10054 == se.ErrorCode) // Remote end closed the connection
                {
                    msg.msgID = -1;
                    closeConnection(clientConnectionSocket);
                }
                else if (10035 != se.ErrorCode)
                {
                }
            }
            catch
            {
                //Silently catch other exceptions
            }

            return(msg);
        }
Exemplo n.º 4
0
 public void sendMessage(Messages.MESSAGE msg, Socket clientConnectionSocket)
 {
     byte[] byteData = Serialize(msg);
     clientConnectionSocket.Send(byteData, SocketFlags.None);
 }
Exemplo n.º 5
0
 public void getMessage(Messages.MESSAGE msg, int clientID)
 {
     sessionDecoder.messageReader(msg, clientID);
 }