Example #1
0
        private Peer()
        {
            this.state = GameState.Connect;
            this.isCreator = false;
            this.peerInfo = new PeerInfo(IPAddress.Loopback.ToString(), -1);
            this.lastMessage = MessageType.Null;
            this.currentRoom = null;
            this.roomRequester = new Queue<PeerInfo>();
            this.lastMessageClient = MessageType.Null;
            this.keepAliveSender = new System.Timers.Timer(GameConstant.keepAliveInterval);
            this.keepAliveSender.Elapsed += new ElapsedEventHandler(sendKeepAlive);
            this.keepAliveSenderCreator = new System.Timers.Timer(GameConstant.keepAliveInterval);
            this.keepAliveSenderCreator.Elapsed += new ElapsedEventHandler(sendKeepAliveCreator);
            this.keepAliveResponse = new System.Timers.Timer(GameConstant.connectionTimeOut);
            this.keepAliveResponse.Elapsed += new ElapsedEventHandler(trackerDown);
            this.keepAliveResponseCreator = new System.Timers.Timer(GameConstant.connectionTimeOut);
            this.keepAliveResponseCreator.Elapsed += new ElapsedEventHandler(creatorDown);

            for (int i = 1; i <= 8; i++ )
            {
                cubenode[i] = new List<int>();
            }
            cubenode[1].Add(2); cubenode[1].Add(4); cubenode[1].Add(5); cubenode[1].Add(7);
            cubenode[2].Add(1); cubenode[2].Add(3); cubenode[2].Add(6); cubenode[2].Add(8);
            cubenode[3].Add(2); cubenode[3].Add(4); cubenode[3].Add(5); cubenode[3].Add(7);
            cubenode[4].Add(1); cubenode[4].Add(3); cubenode[4].Add(6); cubenode[4].Add(8);
            cubenode[5].Add(1); cubenode[5].Add(3); cubenode[5].Add(6); cubenode[5].Add(8);
            cubenode[6].Add(2); cubenode[6].Add(4); cubenode[6].Add(5); cubenode[6].Add(7);
            cubenode[7].Add(1); cubenode[7].Add(3); cubenode[7].Add(6); cubenode[7].Add(8);
            cubenode[8].Add(2); cubenode[8].Add(4); cubenode[8].Add(5); cubenode[8].Add(7);

            // there is a reason that takes long sentences to explain why I passed random object here
            // instead of creating new random object when needed.
        }
Example #2
0
 // Copy constructor
 public Room(Room room)
 {
     this.creator = new PeerInfo(room.creator.getIP(), room.creator.getID(), room.creator.getPort(), room.creator.getUsername());
     this.roomID = room.roomID;
     this.maxPlayer = room.maxPlayer;
     this.connectedPeers = new List<PeerInfo>();
     foreach (PeerInfo peer in room.connectedPeers)
     {
         this.connectedPeers.Add(new PeerInfo(peer.getIP(), peer.getID(), peer.getPort(), peer.getUsername()));
     }
 }
Example #3
0
        private void onReceivePeerClient(IAsyncResult ar)
        {
            try
            {
                peerToPeerSocketClient.EndReceive(ar);

                msgReceived = new Message(msgByteP2PClient);
                switch (msgReceived.msgType)
                {
                    case MessageType.RoomInfo:
                        this.currentRoom = new Room(msgReceived.room);
                        drawRoom();
                        this.state = GameState.Room;
                        break;

                    case MessageType.KeepAlive:
                        this.keepAliveResponseCreator.Stop();
                        this.keepAliveResponseCreator.Start();
                        break;

                    case MessageType.Success:
                        if (this.lastMessageClient == MessageType.Quit)
                        {
                            gotoLobby();
                            this.currentRoom = null;
                            this.state = GameState.Lobby;
                        }
                        break;

                    case MessageType.CreatorQuit:
                        if (this.peerInfo.getID() == msgReceived.peer.getID())
                        {
                            this.currentRoom.setCreator(this.peerInfo);
                            bool hitoriBochi = false;
                            if (this.currentRoom.getConnectedPeers().Count == 2) hitoriBochi = true;
                            this.currentRoom.getConnectedPeers().Clear();
                            this.currentRoom.getConnectedPeers().Add(this.peerInfo);
                            this.isCreator = true;
                            this.peerList = new List<PeerHandler>();
                            if (hitoriBochi) drawRoom();
                        }
                        else
                        {
                            this.currentRoom.setCreator(new PeerInfo(msgReceived.peer));
                            connectToCreator(msgReceived.peer.getIP(), msgReceived.peer.getPort());
                        }
                        break;

                    case MessageType.Start:
                        Console.WriteLine("Starting game...\nMembers : \n");
                        foreach(PeerInfo peer in this.currentRoom.getConnectedPeers()) {
                            Console.WriteLine(peer.getID());
                            Console.WriteLine(peer.getIP());
                            Console.WriteLine(peer.getUsername());
                        }
                    /*
                        Thread t = new Thread( () => {
                        Thread.CurrentThread.IsBackground = true;
                        game = new Game1 (this.currentRoom.getIDOnRoom(this.peerInfo.getID ()),
                                          this.currentRoom.getConnectedPeers().Count, msgReceived.turn);
                        game.Run ();
                        });
                        t.Start();
                     */
                        break;
                    case MessageType.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;
                }
                if (msgReceived.msgType != MessageType.CreatorQuit)
                    peerToPeerSocketClient.BeginReceive(msgByteP2PClient, 0, msgByteP2PClient.Length, SocketFlags.None,
                                                            new AsyncCallback(onReceivePeerClient), null);
            }
            catch (Exception e)
            {
                Console.WriteLine("onreceivepeerclient exception : {0}", e.Message);
                Console.WriteLine(e.Source);
                Console.WriteLine(e.InnerException);
                Console.WriteLine(e.StackTrace);
                Console.WriteLine(e.TargetSite);
            }
        }
Example #4
0
        public void createRoom(string roomID, int maxPlayer)
        {
            try
            {
                initializeP2PConnection();
                this.lastMessage = MessageType.Create;
                this.peerList = new List<PeerHandler>();
                byte[] msg = new Message(MessageType.Create, this.peerInfo.getID(), maxPlayer, roomID).toByte();

                this.currentRoom = new Room(this.peerInfo, roomID, maxPlayer);

                peerToTrackerSocket.BeginSend(msg, 0, msg.Length, SocketFlags.None,
                                                new AsyncCallback(onSendTracker), null);
            }
            catch (Exception e)
            {
                Console.WriteLine("createRoom exception : {0}", e.Message);
                Console.WriteLine(e.Source);
                Console.WriteLine(e.InnerException);
                Console.WriteLine(e.StackTrace);
                Console.WriteLine(e.TargetSite);
            }
        }
Example #5
0
        // Construct message for JoinSuccess, RoomInfo message
        private byte[] constructMessage(MessageType msgType, Room room)
        {
            if (msgType == MessageType.JoinSuccess || msgType == MessageType.RoomInfo)
            {
                List<byte> msg = new List<byte>();
                msg.AddRange(GameConstant.pstrByte);
                msg.AddRange(GameConstant.reservedByte);
                if (msgType == MessageType.JoinSuccess)
                    msg.Add((byte)GameConstant.joinSuccessCode);
                else
                    msg.Add((byte)GameConstant.RoomInfoCode);
                msg.Add((byte)GameConstant.beginRoomCode);
                msg.AddRange(room.toByte());
                msg.Add((byte)GameConstant.endRoomCode);

                return msg.ToArray();
            }
            else return null;
        }
Example #6
0
        // Construct message object from ByteArray
        public Message(byte[] msg)
        {
            int offset = 0;
            string pstr = Encoding.ASCII.GetString(msg, offset, GameConstant.pstr.Length);
            offset += GameConstant.pstr.Length;

            if (pstr.Equals(GameConstant.pstr))
            {
                this.pstr = pstr;
                Buffer.BlockCopy(msg, offset, this.reservedByte, 0, 8);
                offset += reservedByte.Length;
                int messageCode = (int)msg[offset]; offset++;

                switch (messageCode)
                {
                    case GameConstant.handshakeCode:
                        this.msgType = MessageType.Handshake;
                        this.username = Encoding.ASCII.GetString(msg, offset, GameConstant.usernameSize).Trim();
                        break;

                    case GameConstant.handshakeResponseCode:
                        this.msgType = MessageType.HandshakeResponse;
                        byte[] peerHR = new byte[GameConstant.peerInfoSize];
                        Buffer.BlockCopy(msg, offset, peerHR, 0, GameConstant.peerInfoSize);
                        this.peer = new PeerInfo(peerHR);
                        //this.peerID = BitConverter.ToInt32(msg, offset); offset += 4;
                        //this.port = BitConverter.ToInt32(msg, offset);
                        break;

                    case GameConstant.keepAliveCode:
                        this.msgType = MessageType.KeepAlive;
                        this.peerID = BitConverter.ToInt32(msg, offset);
                        break;

                    case GameConstant.createCode:
                        this.msgType = MessageType.Create;
                        this.peerID = BitConverter.ToInt32(msg, offset); offset += GameConstant.peerIdSize;
                        this.maxPlayer = BitConverter.ToInt32(msg, offset); offset += GameConstant.maxPlayerSize;
                        this.roomID = Encoding.ASCII.GetString(msg, offset, GameConstant.roomIDSize);
                        this.roomID = this.roomID.Replace("\0", String.Empty).Trim();
                        break;

                    case GameConstant.listCode:
                        this.msgType = MessageType.List;
                        this.peerID = BitConverter.ToInt32(msg, offset);
                        break;

                    case GameConstant.roomCode:
                        this.msgType = MessageType.Room;
                        this.roomCount = BitConverter.ToInt32(msg, offset);
                        offset += GameConstant.roomCountSize;
                        for (int i = 0; i < this.roomCount; i++)
                        {
                            if ((int)msg[offset] == GameConstant.beginRoomCode)
                            {
                                offset++;
                                int beginRoom = offset;
                                int roomByteLength = 0;
                                while ((int)msg[offset] != GameConstant.endRoomCode)
                                {
                                    roomByteLength++;
                                    offset++;
                                }
                                offset++;

                                byte[] roomByte = new byte[roomByteLength];
                                Buffer.BlockCopy(msg, beginRoom, roomByte, 0, roomByteLength);
                                this.roomList.Add(new Room(roomByte));
                            }
                        }
                        break;

                    case GameConstant.successCode:
                        this.msgType = MessageType.Success;
                        break;

                    case GameConstant.failedCode:
                        this.msgType = MessageType.Failed;
                        break;

                    case GameConstant.joinCode:
                        this.msgType = MessageType.Join;
                        this.peerID = BitConverter.ToInt32(msg, offset);
                        offset += GameConstant.peerIdSize;
                        this.roomID = Encoding.ASCII.GetString(msg, offset, GameConstant.roomIDSize);
                        this.roomID = this.roomID.Replace("\0", String.Empty).Trim();
                        break;

                    case GameConstant.startCode:
                        this.msgType = MessageType.Start;
                        this.peerID = BitConverter.ToInt32(msg, offset);
                        offset += GameConstant.peerIdSize;
                        this.turn = BitConverter.ToInt32(msg, offset);
                        offset += 4;
                        this.roomID = Encoding.ASCII.GetString(msg, offset, GameConstant.startSize);
                        this.roomID = this.roomID.Replace("\0", String.Empty).Trim();
                        break;

                    case GameConstant.quitCode:
                        this.msgType = MessageType.Quit;
                        this.peerID = BitConverter.ToInt32(msg, offset);
                        break;

                    case GameConstant.checkCode:
                        this.msgType = MessageType.Check;
                        this.peerIDJoinRoom = BitConverter.ToInt32(msg, offset);
                        break;

                    case GameConstant.checkResponseCode:
                        this.msgType = MessageType.CheckResponse;
                        this.peerIDJoinRoom = BitConverter.ToInt32(msg, offset);
                        offset += GameConstant.peerIdSize;
                        this.isJoinAccepted = BitConverter.ToBoolean(msg, offset); offset += 1;
                        this.port = BitConverter.ToInt32(msg, offset);
                        break;

                    case GameConstant.handshakeCreatorCode:
                        this.msgType = MessageType.HandshakeCreator;
                        byte[] peer = new byte[GameConstant.peerInfoSize];
                        Buffer.BlockCopy(msg, offset, peer, 0, GameConstant.peerInfoSize);
                        this.peer = new PeerInfo(peer);
                        break;

                    case GameConstant.playerCode:
                        this.msgType = MessageType.Player;
                        int players = BitConverter.ToInt32(msg, offset); offset += 4;
                        for (int i = 0; i < players; i++)
                        {
                            byte[] peerByte = new byte[GameConstant.peerInfoSize];
                            Buffer.BlockCopy(msg, offset, peerByte, 0, GameConstant.peerInfoSize);
                            offset += GameConstant.peerInfoSize;

                            this.player.Add(new PeerInfo(peerByte));
                        }
                        break;

                    case GameConstant.joinSuccessCode:
                        this.msgType = MessageType.JoinSuccess;
                        if ((int)msg[offset] == GameConstant.beginRoomCode)
                        {
                            offset++;
                            int beginRoom = offset;
                            int roomByteLength = 0;
                            while ((int)msg[offset] != GameConstant.endRoomCode)
                            {
                                roomByteLength++; offset++;
                            }
                            offset++;

                            byte[] roomByte = new byte[roomByteLength];
                            Buffer.BlockCopy(msg, beginRoom, roomByte, 0, roomByteLength);

                            this.room = new Room(roomByte);
                        }
                        break;

                    case GameConstant.creatorQuitCode:
                        this.msgType = MessageType.CreatorQuit;
                        byte[] creatorPeer = new byte[GameConstant.peerInfoSize];
                        Buffer.BlockCopy(msg, offset, creatorPeer, 0, GameConstant.peerInfoSize);
                        this.peer = new PeerInfo(creatorPeer);
                        break;

                    case GameConstant.RoomInfoCode:
                        this.msgType = MessageType.RoomInfo;
                        if ((int)msg[offset] == GameConstant.beginRoomCode)
                        {
                            offset++;
                            int beginRoom = offset;
                            int roomByteLength = 0;
                            while ((int)msg[offset] != GameConstant.endRoomCode)
                            {
                                roomByteLength++; offset++;
                            }
                            offset++;

                            byte[] roomByte = new byte[roomByteLength];
                            Buffer.BlockCopy(msg, beginRoom, roomByte, 0, roomByteLength);

                            this.room = new Room(roomByte);
                        }
                        break;

                    case GameConstant.SelfPromoteCreatorCode:
                        this.msgType = MessageType.SelfPromoteCreator;
                        byte[] promote = new byte[GameConstant.peerInfoSize];
                        Buffer.BlockCopy(msg, offset, promote, 0, GameConstant.peerInfoSize);
                        offset += GameConstant.peerInfoSize;
                        this.peer = new PeerInfo(promote);
                        Buffer.BlockCopy(msg, offset, promote, 0, GameConstant.peerInfoSize);
                        this.peer2 = new PeerInfo(promote);
                        break;

                    case GameConstant.GameUpdate:
                        this.msgType = MessageType.GameUpdate;
                        this.timestamp = BitConverter.ToInt64(msg, offset);
                        offset += GameConstant.timestampSize;
                        byte[] gameUpdate = new byte[GameConstant.gameUpdateSize];
                        Buffer.BlockCopy(msg, offset, gameUpdate, 0, GameConstant.gameUpdateSize);
                        this.gameUpdate = new GameUpdate(gameUpdate);
                        break;

                }
            }
        }
Example #7
0
 // Constructor for JoinSuccess, RoomInfo message
 public Message(MessageType msgType, Room room)
 {
     this.msgType = msgType; this.room = room;
 }