Exemplo n.º 1
0
        private void onConnectPeerClient(IAsyncResult ar)
        {
            try
            {
                peerToPeerSocketClient.EndConnect(ar);
                Message msg = new Message(MessageType.HandshakeCreator, this.peerInfo);
                byte[] message = msg.toByte();

                peerToPeerSocketClient.BeginSend(message, 0, message.Length, SocketFlags.None,
                                                    new AsyncCallback(onSendPeerClient), null);

                peerToPeerSocketClient.BeginReceive(msgByteP2PClient, 0, msgByteP2PClient.Length, SocketFlags.None,
                                                        new AsyncCallback(onReceivePeerClient), null);
                this.keepAliveSenderCreator.Start();
                this.keepAliveResponseCreator.Start();
            }
            catch (Exception e)
            {
                Console.WriteLine("onConnectPeerClient exception : {0}", e.Message);
                Console.WriteLine(e.Source);
                Console.WriteLine(e.InnerException);
                Console.WriteLine(e.StackTrace);
                Console.WriteLine(e.TargetSite);
            }
        }
Exemplo n.º 2
0
        private void onReceivePeerServer(IAsyncResult ar)
        {
            try
            {
                Socket peerSocket = (Socket)ar.AsyncState;
                peerSocket.EndReceive(ar);

                msgReceived = new Message(msgByteP2PServer);
                msgResponse = new Message();

                switch (msgReceived.msgType)
                {
                    case MessageType.HandshakeCreator:
                        PeerInfo newPlayer = new PeerInfo(msgReceived.peer);
                        this.peerList.Add(new PeerHandler(peerSocket, newPlayer, this));
                        this.currentRoom.addPlayer(newPlayer);

                        msgResponse = new Message(MessageType.RoomInfo, this.currentRoom);
                        break;

                    case MessageType.KeepAlive:
                        msgResponse = new Message(MessageType.KeepAlive, msgReceived.peerID);
                        PeerHandler handler = getHandler(msgReceived.peerID);
                        handler.isAlive = true;
                        handler.isAliveTimer.Stop();
                        handler.isAliveTimer.Start();
                        break;

                    case MessageType.GameUpdate:
                        msgResponse = new Message(MessageType.GameUpdate, msgReceived.timestamp, msgReceived.gameUpdate);
                        foreach (PeerInfo peer in this.currentRoom.getConnectedPeers())
                        {
                            if (cubenode[this.currentRoom.getIDOnRoom(this.peerInfo.getID())].Contains(this.currentRoom.getIDOnRoom(peer.getID())))
                                System.Console.WriteLine(peer.ToString() + " -> " + this.peerInfo.ToString() + " : " + msgReceived.timestamp + " " + msgReceived.gameUpdate.ToString());
                        }
                        addQueue(msgReceived);
                        break;

                    case MessageType.Quit:
                        msgResponse = new Message(MessageType.Success);
                        this.currentRoom.removePlayer(msgReceived.peerID);
                        removePeerHandler(msgReceived.peerID);
                        break;
                }

                byte[] message = msgResponse.toByte();
                peerSocket.BeginSend(message, 0, message.Length, SocketFlags.None,
                                        new AsyncCallback(onSendPeerServer), peerSocket);

                if (msgReceived.msgType != MessageType.Quit) {
                    peerSocket.BeginReceive(msgByteP2PServer, 0, msgByteP2PServer.Length, SocketFlags.None,
                                                new AsyncCallback(onReceivePeerServer), peerSocket);
                }

                if (msgReceived.msgType == MessageType.Quit || msgReceived.msgType == MessageType.HandshakeCreator)
                {
                    byte[] m = new Message(MessageType.RoomInfo, this.currentRoom).toByte();
                    foreach (PeerHandler peer in peerList)
                    {
                        if (peer.socket != peerSocket)
                        {
                            peer.socket.BeginSend(m, 0, m.Length, SocketFlags.None, new AsyncCallback(onSendPeerServer), peer.socket);
                        }
                    }

                    byte[] msgTracker = new Message(MessageType.RoomInfo, this.currentRoom).toByte();
                    peerToTrackerSocket.BeginSend(msgTracker, 0, msgTracker.Length, SocketFlags.None,
                                                    new AsyncCallback(onSendTracker), null);
                    drawRoom();
                }
                if (msgReceived.msgType == MessageType.GameUpdate)
                {
                    byte[] msg = new Message(MessageType.GameUpdate, msgReceived.timestamp, msgReceived.gameUpdate).toByte();
                    foreach (PeerHandler peer in peerList)
                    {
                        if (peer.socket != peerSocket)
                        {
                            peer.socket.BeginSend(msg, 0, msg.Length, SocketFlags.None, new AsyncCallback(onSendPeerServer), peer.socket);
                        }
                    }

                }
            }
            catch (Exception e)
            {
                Console.WriteLine("onReceivePeerServer exception : {0}", e.Message);
                Console.WriteLine(e.Source);
                Console.WriteLine(e.InnerException);
                Console.WriteLine(e.StackTrace);
                Console.WriteLine(e.TargetSite);
            }
        }
Exemplo n.º 3
0
        public void updateRoom(int param1, double param2)
        {
            try
            {
                if (isCreator)
                {
                    this.lastMessage = MessageType.GameUpdate;
                    Message m = new Message(MessageType.GameUpdate, getCurrentTime(), new GameUpdate(param1,param2));
                    byte[] msg = m.toByte();
                    foreach (PeerHandler peer in peerList)
                    {
                        peer.socket.BeginSend(msg, 0, msg.Length, SocketFlags.None, new AsyncCallback(onSendPeerServer), peer.socket);
                    }
                    foreach (PeerInfo peer in this.currentRoom.getConnectedPeers())
                    {
                        if (cubenode[this.currentRoom.getIDOnRoom(this.peerInfo.getID())].Contains(this.currentRoom.getIDOnRoom(peer.getID())))
                            System.Console.WriteLine(peer.ToString() + " -> " + this.peerInfo.ToString() + " : " + m.timestamp +" " + m.gameUpdate.ToString());
                    }
                }
                else
                {
                    this.lastMessageClient = MessageType.GameUpdate;
                    byte[] msg = new Message(MessageType.GameUpdate, getCurrentTime(), new GameUpdate(param1, param2)).toByte();

                    peerToPeerSocketClient.BeginSend(msg, 0, msg.Length, SocketFlags.None,
                                                    new AsyncCallback(onSendPeerClient), null);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("updateRoom exception : {0}", e.Message);
                Console.WriteLine(e.Source);
                Console.WriteLine(e.InnerException);
                Console.WriteLine(e.StackTrace);
                Console.WriteLine(e.TargetSite);
            }
        }