コード例 #1
0
ファイル: Room.cs プロジェクト: andhikanugraha/tetembakan
 public Room(PeerInfo creator, string roomID, int maxPlayer, List<PeerInfo> connectedPeers)
 {
     this.creator = creator;
     this.roomID = roomID;
     this.maxPlayer = maxPlayer;
     this.connectedPeers = connectedPeers;
 }
コード例 #2
0
ファイル: Room.cs プロジェクト: andhikanugraha/tetembakan
 public Room(PeerInfo creator, string roomID, int maxPlayer)
 {
     this.creator = creator;
     this.roomID = roomID;
     this.maxPlayer = maxPlayer;
     this.connectedPeers = new List<PeerInfo>(maxPlayer);
     addPlayer(creator);
 }
コード例 #3
0
ファイル: PeerInfo.cs プロジェクト: andhikanugraha/tetembakan
 public bool Equals(PeerInfo peer)
 {
     if ((this.peerID == peer.peerID) && (this.peerIP == peer.peerIP) && (this.port == peer.port))
     {
         return true;
     }
     return false;
 }
コード例 #4
0
ファイル: Room.cs プロジェクト: andhikanugraha/tetembakan
 // 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()));
     }
 }
コード例 #5
0
ファイル: Room.cs プロジェクト: andhikanugraha/tetembakan
 // Concstruct Room Object from ByteArray Data
 public Room(byte[] roomByte)
 {
     int offset = 0;
     int roomIDLength = BitConverter.ToInt32(roomByte, offset); offset += 4;
     this.roomID = Encoding.ASCII.GetString(roomByte, offset, roomIDLength); offset += roomIDLength;
     byte[] creator = new byte[GameConstant.peerInfoSize];
     Buffer.BlockCopy(roomByte, offset, creator, 0, GameConstant.peerInfoSize);
     this.creator = new PeerInfo(creator);
     offset += GameConstant.peerInfoSize;
     int maxPlayer = BitConverter.ToInt32(roomByte, offset); offset += GameConstant.maxPlayerSize;
     this.maxPlayer = maxPlayer;
     int connectedPeersCount = BitConverter.ToInt32(roomByte, offset); offset += 4;
     this.connectedPeers = new List<PeerInfo>(connectedPeersCount);
     for (int i = 0; i < connectedPeersCount; i++)
     {
         byte[] peer = new byte[GameConstant.peerInfoSize];
         Buffer.BlockCopy(roomByte, offset, peer, 0, GameConstant.peerInfoSize);
         offset += GameConstant.peerInfoSize;
         this.connectedPeers.Add(new PeerInfo(peer));
     }
 }
コード例 #6
0
ファイル: Room.cs プロジェクト: andhikanugraha/tetembakan
 public void addPlayer(PeerInfo newPlayer)
 {
     connectedPeers.Add(newPlayer);
 }
コード例 #7
0
ファイル: Room.cs プロジェクト: andhikanugraha/tetembakan
 public void setCreator(PeerInfo creator)
 {
     this.creator = creator;
 }
コード例 #8
0
ファイル: Tracker.cs プロジェクト: andhikanugraha/tetembakan
 private void dead(object source, ElapsedEventArgs e)
 {
     tracker.removeDeadPeer(this.info.getID());
     this.isAliveTimer.Stop();
     this.socket = null; this.info = null; this.isAliveTimer = null; this.tracker = null;
 }
コード例 #9
0
ファイル: PeerInfo.cs プロジェクト: andhikanugraha/tetembakan
 public PeerInfo(PeerInfo peer)
 {
     this.peerIP = peer.peerIP; this.peerID = peer.peerID; this.port = peer.port; this.username = peer.username;
 }
コード例 #10
0
ファイル: Tracker.cs プロジェクト: andhikanugraha/tetembakan
 public PeerHandler(Socket socket, PeerInfo info, Tracker tracker)
 {
     this.tracker = tracker;
     this.socket = socket;
     this.info = info;
     this.isAlive = true;
     this.isAliveTimer = new System.Timers.Timer(GameConstant.connectionTimeOut);
     this.isAliveTimer.Elapsed += new ElapsedEventHandler(dead);
     this.isAliveTimer.Start();
 }
コード例 #11
0
ファイル: Tracker.cs プロジェクト: andhikanugraha/tetembakan
 private void setNewCreator(PeerInfo oldCreator, PeerInfo newCreator)
 {
     foreach (Room room in roomList)
     {
         if (room.getCreator().getID() == oldCreator.getID())
         {
             room.setCreator(new PeerInfo(newCreator));
             room.getConnectedPeers().Clear();
             room.addPlayer(room.getCreator());
             roomTable[room.getRoomID()] = getHandler(room.getCreator());
             return;
         }
     }
 }
コード例 #12
0
ファイル: Tracker.cs プロジェクト: andhikanugraha/tetembakan
 private void removeRoom(PeerInfo creator)
 {
     foreach (Room room in roomList)
     {
         if (room.getCreator().getID() == creator.getID())
         {
             roomList.Remove(room);
             roomTable.Remove(room.getRoomID());
             return;
         }
     }
 }
コード例 #13
0
ファイル: Tracker.cs プロジェクト: andhikanugraha/tetembakan
        private void onReceive(IAsyncResult ar)
        {
            try
            {
                Socket peerSocket = (Socket)ar.AsyncState;
                peerSocket.EndReceive(ar);

                Message msgReceived = new Message(msgByte);
                Message msgResponse = new Message();
                string logMsg = String.Empty;
                switch (msgReceived.msgType)
                {
                    case MessageType.Handshake:
                        logMsg = MessageType.Handshake.ToString();
                        if (peerList.Count < maxPeer)
                        {
                            string peerIP = ((IPEndPoint)peerSocket.RemoteEndPoint).Address.ToString();
                            int peerID = peerInfoPooler.getAvailableID();
                            int port = peerInfoPooler.getAvailablePort();
                            PeerInfo peerInfo = new PeerInfo(peerIP, peerID, port, msgReceived.username);
                            peerList.Add(new PeerHandler(peerSocket, peerInfo, this));

                            msgResponse = new Message(MessageType.HandshakeResponse, peerInfo);
                        }
                        else
                        {
                            msgResponse = new Message(MessageType.Failed);
                        }
                        break;

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

                    case MessageType.List:
                        logMsg = MessageType.List.ToString();
                        msgResponse = new Message(MessageType.Room, roomList);
                        break;

                    case MessageType.Create:
                        logMsg = MessageType.Create.ToString();
                        if ((roomList.Count < maxRoom) && (!roomTable.ContainsKey(msgReceived.roomID)))
                        {
                            PeerInfo creator = getPeerInfo(peerSocket);
                            this.roomList.Add(new Room(creator, msgReceived.roomID, msgReceived.maxPlayer));
                            this.roomTable.Add(msgReceived.roomID, getHandler(msgReceived.peerID));
                            msgResponse = new Message(MessageType.Success);
                        }
                        else
                        {
                            msgResponse = new Message(MessageType.Failed);
                        }
                        break;

                    case MessageType.Join:
                        logMsg = MessageType.Join.ToString();
                        if (roomTable.ContainsKey(msgReceived.roomID))
                        {
                            if (!isRoomFull(msgReceived.roomID))
                            {
                                msgResponse = new Message(MessageType.JoinSuccess, getRoomByID(msgReceived.roomID));
                            }
                            else
                            {
                                msgResponse = new Message(MessageType.Failed);
                            }
                        }
                        else
                        {
                            msgResponse = new Message(MessageType.Failed);
                        }
                        break;

                    case MessageType.CheckResponse:
                        logMsg = MessageType.CheckResponse.ToString();
                        if (msgReceived.isJoinAccepted)
                        {
                            Room infoSent = getRoomByCreator(getPeerInfo(peerSocket).getID());
                            infoSent.getCreator().setPort(msgReceived.port);
                            msgResponse = new Message(MessageType.JoinSuccess, infoSent);
                        }
                        else
                            msgResponse = new Message(MessageType.Failed);
                        break;

                    case MessageType.CreatorQuit:
                        logMsg = MessageType.CreatorQuit.ToString();
                        msgResponse = new Message(MessageType.Success);
                        PeerInfo oldCreator = getPeerInfo(peerSocket);
                        if (oldCreator.getID() == msgReceived.peer.getID())
                        {
                            removeRoom(oldCreator);
                        }
                        else
                        {
                            setNewCreator(oldCreator, msgReceived.peer);
                        }
                        break;

                    case MessageType.RoomInfo:
                        logMsg = MessageType.RoomInfo.ToString();
                        updateRoomInfo(msgReceived.room);
                        break;

                    case MessageType.SelfPromoteCreator:
                        logMsg = MessageType.SelfPromoteCreator.ToString();
                        setNewCreator(msgReceived.peer, msgReceived.peer2);
                        break;

                }

                byte[] message = msgResponse.toByte();

                if (msgReceived.msgType == MessageType.CheckResponse)
                {
                    Socket joinSocket = getSocket(msgReceived.peerIDJoinRoom);
                    if (joinSocket != null)
                    {
                        joinSocket.BeginSend(message, 0, message.Length, SocketFlags.None,
                                                new AsyncCallback(onSend), joinSocket);
                    }
                }
                else if (msgReceived.msgType != MessageType.RoomInfo)
                {
                    peerSocket.BeginSend(message, 0, message.Length, SocketFlags.None,
                                        new AsyncCallback(onSend), peerSocket);
                }

                Array.Clear(msgByte, 0, msgByte.Length);
                peerSocket.BeginReceive(msgByte, 0, msgByte.Length, SocketFlags.None,
                                            new AsyncCallback(onReceive), peerSocket);

                PeerInfo logInfo = getPeerInfo(peerSocket);

                if (logMsg == MessageType.KeepAlive.ToString() && !logKeepAlive) { }
                else
                if (logInfo != null && log)
                {
                    if (logMsg != MessageType.RoomInfo.ToString())
                        if (logMsg == MessageType.Handshake.ToString())
                            System.Console.WriteLine("{0} has been connected to tracker", logInfo);
                        System.Console.WriteLine("{0} : {1}", logInfo, logMsg);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("onReceive exception : {0}", e.Message);
                Console.WriteLine(e.Source);
                Console.WriteLine(e.InnerException);
                Console.WriteLine(e.StackTrace);
                Console.WriteLine(e.TargetSite);
            }
        }
コード例 #14
0
ファイル: Tracker.cs プロジェクト: andhikanugraha/tetembakan
 private PeerHandler getHandler(PeerInfo peer)
 {
     foreach (PeerHandler handler in peerList)
     {
         if (handler.info.getID() == peer.getID())
         {
             return handler;
         }
     }
     return new PeerHandler();
 }