Ejemplo n.º 1
0
 public Room(string new_room_id, int new_peer_id)
 {
     room_id = new_room_id;
     peer_id = new_peer_id;
     maxplayer = 8;
     neighbor = new List<Peer>();
     Peer peer = new Peer();
     peer.peer_id = new_peer_id;
     neighbor.Add(peer);
     creatorIPAddress = "";
 }
Ejemplo n.º 2
0
        private void HandleClientComm(object client)
        {
            TcpClient tcpClient = (TcpClient)client;
            NetworkStream clientStream = tcpClient.GetStream();
            string clientIPAddress = "" + IPAddress.Parse((((IPEndPoint)tcpClient.Client.RemoteEndPoint).Address.ToString()));

            Debug.WriteLine(clientIPAddress);
            tcpClient.ReceiveTimeout = 10000000;

            byte[] message = new byte[4096];
            int bytesRead;
            //clientStream.ReadTimeout = 1000;

            while (true)
            {
                bytesRead = 0;

                try
                {
                    //blocks until a client sends a message
                    bytesRead = clientStream.Read(message, 0, 4096);
                }
                catch
                {
                    //a socket error has occured
                    break;
                }

                if (bytesRead == 0)
                {
                    //the client has disconnected from the server
                    break;
                }

                //message has successfully been received
                ASCIIEncoding encoder = new ASCIIEncoding();
                switch (Message.getCode(message))
                {
                    case Message.HANDSHAKE_CODE: // 135
                        if (peers.Count == max_peer)
                        {
                            if (log)
                                Console.WriteLine("Handshake failed : max_peer reached");
                            byte[] buff = Message.Failed();
                            clientStream.Write(buff, 0, buff.Length);
                            clientStream.Flush();
                        }
                        else
                        {
                            Peer p = new Peer();
                            p.peer_id = lastPeerID;
                            peers.Add(p);
                            byte[] byte_peer_id = BitConverter.GetBytes(lastPeerID);
                            lastPeerID++;
                            if (log)
                                Console.WriteLine("Handshake success : peer " + p.peer_id);
                            byte[] buff = Message.HandShakeResponse(p.peer_id);
                            clientStream.Write(buff, 0, buff.Length);
                            clientStream.Flush();
                        }
                        break;
                    case Message.CREATE_CODE: // 255
                        int peerid = Message.getPeerId(message);
                        int maxplayer = Message.getMaxPlayer(message);
                        String roomid = Message.getRoomId2(message);
                        if (/*findPeer(peerid) != -1  && */findRoom(roomid) == -1 && rooms.Count < max_room)
                        {
                            Room room = new Room();
                            room.peer_id = peerid;
                            room.room_id = roomid;
                            room.maxplayer = maxplayer;
                            room.neighbor = new List<Peer>();
                            Peer p2 = peers[findPeer(peerid)] as Peer;
                            room.neighbor.Add(p2);
                            room.creatorIPAddress = clientIPAddress;
                            rooms.Add(room);
                            if (log)
                                Console.WriteLine("Room creation success : peer " + peerid + " room " + roomid + " IPAddress: " + clientIPAddress);
                            byte[] buff = Message.Success();
                            clientStream.Write(buff, 0, buff.Length);
                            clientStream.Flush();
                        }
                        else
                        {
                            if (log)
                                Console.WriteLine("Room creation failed");
                            byte[] buff = Message.Failed();
                            clientStream.Write(buff, 0, buff.Length);
                            clientStream.Flush();
                        }
                        break;
                    case Message.LIST_CODE:// 254
                        int peerid2 = Message.getPeerId(message);
                        if (log)
                            Console.WriteLine("Room listed for : peer " + peerid2);
                        byte[] buff2 = Message.Room(rooms);
                        clientStream.Write(buff2, 0, buff2.Length);
                        clientStream.Flush();
                        break;
                    case Message.JOIN_CODE:// 253
                        int peerid3 = Message.getPeerId(message);
                        String roomid3 = Message.getRoomId(message);
                        if (joinRoom(roomid3, peerid3))
                        {
                            if (log)
                                Console.WriteLine("Room join success : peer " + peerid3 + " room " + roomid3);
                            byte[] buff = Message.Success();

                            clientStream.Write(buff, 0, buff.Length);
                            clientStream.Flush();
                        }
                        else
                        {
                            if (log)
                                Console.WriteLine("Room join failed : peer " + peerid3 + " room " + roomid3);
                            byte[] buff = Message.Failed();
                            clientStream.Write(buff, 0, buff.Length);
                            clientStream.Flush();
                        }
                        break;
                    case Message.START_CODE:// 252
                        int peerid4 = Message.getPeerId(message);
                        String roomid4 = Message.getRoomId(message);
                        int roomidx = findRoom(roomid4);
                        if (roomidx == -1)
                        {
                            if (log)
                                Console.WriteLine("Room start failed : room not found. peer " + peerid4 + " room " + roomid4);
                            byte[] buff = Message.Failed();
                            clientStream.Write(buff, 0, buff.Length);
                            clientStream.Flush();
                        }
                        else
                        {
                            Room r4 = rooms[roomidx] as Room;
                            if (r4.peer_id != peerid4)
                            {
                                if (log)
                                    Console.WriteLine("Room start failed : peer is not master. peer " + peerid4 + " room " + roomid4);
                                byte[] buff = Message.Failed();
                                clientStream.Write(buff, 0, buff.Length);
                                clientStream.Flush();
                            }
                            else
                            {
                                rooms.RemoveAt(roomidx);
                                if (log)
                                    Console.WriteLine("Room start success peer " + peerid4 + " room " + roomid4);
                                byte[] buff = Message.Success();
                                clientStream.Write(buff, 0, buff.Length);
                                clientStream.Flush();
                            }
                        }
                        break;
                    case Message.QUIT_CODE:// 235
                        int peerid5 = Message.getPeerId(message);
                        int roomidx5 = findRoom(peerid5);
                        if (roomidx5 == -1)
                        {
                            if (log)
                                Console.WriteLine("Room quit failed : room not found. peer " + peerid5);// + " room " + roomid5);
                            byte[] buff = Message.Failed();
                            clientStream.Write(buff, 0, buff.Length);
                            clientStream.Flush();
                        }
                        else
                        {
                            Room r5 = rooms[roomidx5] as Room;
                            if (r5.findNeighbor(peerid5) == -1)
                            {
                                if (log)
                                    Console.WriteLine("Room quit failed : peer hasn't joined yet. peer " + peerid5);// + " room " + roomid5);
                                byte[] buff = Message.Failed();
                                clientStream.Write(buff, 0, buff.Length);
                                clientStream.Flush();
                            }
                            else
                            {
                                r5.neighbor.RemoveAt(r5.findNeighbor(peerid5));
                                if (log)
                                    Console.WriteLine("Room quit success. peer " + peerid5);// + " room " + roomid5);
                                byte[] buff = Message.Success();
                                clientStream.Write(buff, 0, buff.Length);
                                clientStream.Flush();
                            }
                        }
                        break;
                    case Message.INSIDEROOM_CODE:// 253
                        int peerid6 = Message.getPeerId(message);
                        int roomidx6 = findRoom(peerid6);
                        if (roomidx6 != -1)
                        {
                            Room room = rooms[roomidx6] as Room;
                            if (log)
                                Console.WriteLine("Room finding success : peer " + peerid6 + " room " + room.room_id);
                            byte[] buff = Message.InsideRoom(room);

                            clientStream.Write(buff, 0, buff.Length);
                            clientStream.Flush();
                        }
                        else
                        {
                            if (log)
                                Console.WriteLine("Room join failed : peer " + peerid6 + " room " + roomidx6);
                            byte[] buff = Message.Failed();
                            clientStream.Write(buff, 0, buff.Length);
                            clientStream.Flush();
                        }
                        break;
                }
                if (log)
                    Console.WriteLine("Message = " + encoder.GetString(message, 0, bytesRead));
                byte[] buff7 = Message.KeepAlive(Message.getPeerId(message));
                clientStream.Write(buff7, 0, buff7.Length);
                clientStream.Flush();
            }

            tcpClient.Close();
        }
Ejemplo n.º 3
0
        private bool joinRoom(String room_id, int peer_id)
        {
            int pindex = findPeer(peer_id);
            int rindex = findRoom(room_id);
            Console.WriteLine("peerindex: " + pindex + "\nroomindex: " + rindex);
            if (pindex == -1 || rindex == -1)
                return false;
            else
            {
                Room r = rooms[rindex] as Room;
                Peer p = new Peer();//peers[pindex] as Peer;
                p.peer_id = peer_id;
                if (r.neighbor.Count == r.maxplayer)
                    return false;
                if (r.findNeighbor(peer_id) != -1)
                    return false;
                r.neighbor.Add(p);

                foreach (Peer x in r.neighbor)
                {
                    Debug.WriteLine("peer id yang ada: " + x.peer_id);
                }
                return true;
            }
        }
Ejemplo n.º 4
0
        public static ArrayList getRooms(byte[] message)
        {
            byte[] result = new byte[1];

            ArrayList rooms = new ArrayList();

            int room_count = getRoomCount(message);
            int iterator = 0;
            for (int i = 0; i < room_count; ++i)
            {
                result = new byte[4];
                Array.Copy(message, 24 + iterator, result, 0, 4);
                int peer_id = BitConverter.ToInt32(result, 0);
                Debug.WriteLine("peer_id: " + peer_id);
                iterator += 4;

                result = new byte[4];
                Array.Copy(message, 24 + iterator, result, 0, 4);
                int maxplayer = BitConverter.ToInt32(result, 0);
                Debug.WriteLine("maxplayer: " + maxplayer);
                iterator += 4;

                result = new byte[4];
                Array.Copy(message, 24 + iterator, result, 0, 4);
                int neighbor_count = BitConverter.ToInt32(result, 0);
                iterator += 4;
                Debug.WriteLine("neighbor_count: " + neighbor_count);

                Room room = new Room();
                room.peer_id = peer_id;
                room.maxplayer = maxplayer;
                for (int j = 0; j < neighbor_count; j++)
                {
                    result = new byte[4];
                    Array.Copy(message, 24 + iterator, result, 0, 4);
                    int peerID = BitConverter.ToInt32(result, 0);

                    Debug.WriteLine("peerID[{0}]: " + peerID, j);

                    Peer p = new Peer();
                    p.peer_id = peerID;
                    iterator += 4;
                    room.neighbor.Add(p);
                }

                room.room_id = "";
                for(int x = 0; x < 50; ++x)
                {
                    //Console.WriteLine("message ke-{0}: " + (char)message[24 + iterator + x], x);
                    //room_id = (byte)((message[24 + iterator + (5 * x)] << 4 & 0xFF) | (message[24 + iterator + ((5 * x) + 2)]));
                    room.room_id += (char)message[24 + iterator + x];
                    room.creatorIPAddress += (char)message[24 + (iterator + 50) + x];
                }
                room.room_id.Normalize();
                room.creatorIPAddress.Normalize();
                iterator += 100;
                Debug.WriteLine("roomID[{0}]: " + room.room_id + "\ncreatorIPAddress[{1}]: " + room.creatorIPAddress, i, i);

                rooms.Add(room);
            }

            return rooms;
        }
Ejemplo n.º 5
0
        public static Room getRoom(byte[] message)
        {
            int iterator = 0;
            byte[] result = new byte[4];
            Array.Copy(message, 20 + iterator, result, 0, 4);
            int peer_id = BitConverter.ToInt32(result, 0);
            Debug.WriteLine("peer_id: " + peer_id);
            iterator += 4;

            result = new byte[4];
            Array.Copy(message, 20 + iterator, result, 0, 4);
            int maxplayer = BitConverter.ToInt32(result, 0);
            Debug.WriteLine("maxplayer: " + maxplayer);
            iterator += 4;

            result = new byte[4];
            Array.Copy(message, 20 + iterator, result, 0, 4);
            int neighbor_count = BitConverter.ToInt32(result, 0);
            iterator += 4;
            Debug.WriteLine("neighbor_count: " + neighbor_count);

            Room room = new Room();
            room.peer_id = peer_id;
            room.maxplayer = maxplayer;
            for (int j = 0; j < neighbor_count; j++)
            {
                result = new byte[4];
                Array.Copy(message, 20 + iterator, result, 0, 4);
                int peerID = BitConverter.ToInt32(result, 0);

                Debug.WriteLine("peerID[{0}]: " + peerID, j);

                Peer p = new Peer();
                p.peer_id = peerID;
                iterator += 4;
                room.neighbor.Add(p);
            }

            room.room_id = "";
            for (int x = 0; x < 50; ++x)
            {
                //Console.WriteLine("message ke-{0}: " + (char)message[24 + iterator + x], x);
                //room_id = (byte)((message[24 + iterator + (5 * x)] << 4 & 0xFF) | (message[24 + iterator + ((5 * x) + 2)]));
                room.room_id += (char)message[20 + iterator + x];
                room.creatorIPAddress += (char)message[20 + (iterator + 50) + x];
            }
            room.room_id = normalizeString(room.room_id);
            room.creatorIPAddress = normalizeString(room.creatorIPAddress);
            Debug.WriteLine("roomID: " + room.room_id + "\ncreatorIPAddress: " + room.creatorIPAddress);

            return room;
        }