Пример #1
0
        private void ResponseConnectionAttemptRequest(string key, IPEndPoint ipe)
        {
            SignalingServerMessage ssm = new SignalingServerMessage(SignalingMethod.ConnectionAttemptResponse);

            byte[] addressByte = ipe.Address.GetAddressBytes();
            byte[] ip          = new byte[3 + addressByte.Length];
            if (addressByte.Length > 4)
            {
                ip[0] = 0x2;
            }
            else
            {
                ip[0] = 0x1;
            }
            ushort port = (ushort)ipe.Port;

            ip[2] = (byte)(port & 0xff);
            ip[1] = (byte)((port >> 8) & 0xff);
            Array.Copy(addressByte, 0, ip, 3, addressByte.Length);
            ssm.WriteBytes(SignalingAttribute.PeerAddress, ip);
            lock (_lock)
            {
                senderTable[key].Send(ssm.WriteRequest());
            }
        }
Пример #2
0
        private void OnCreateRoom(SignalingServerMessage ssm)
        {
            IPEndPoint ipe  = (IPEndPoint)ssm.Get(SignalingAttribute.RoomAddress);
            string     tag  = (string)ssm.Get(SignalingAttribute.RoomTag);
            string     name = (string)ssm.Get(SignalingAttribute.RoomName);
            string     des  = (string)ssm.Get(SignalingAttribute.RoomDescription);
            string     key  = ipe.ToString();

            if (tag.Length <= 0)
            {
                Console.WriteLine("Create Room: '[{0}]{1} Failed!'", tag, key);
                ResponseCreateRoom(false);
            }
            lock (_lock)
            {
                int idx = room.FindIndex(x => x.Key == key);

                if (idx != -1)
                {
                    Console.WriteLine("Create Room: '[{0}]{1} Failed!'", tag, key);
                    //room[idx].Update(tag);
                    ResponseCreateRoom(false);
                }
                else
                {
                    Room r = new Room(tag, ipe, des, name);
                    room.Add(r);
                    senderTable.Add(key, sender);
                    clientKey = key;

                    ResponseCreateRoom(true);
                    Console.WriteLine("Create Room: " + r.ToString());
                }
            }
        }
Пример #3
0
        private void OnJoinRoom(SignalingServerMessage ssm)
        {
            IPEndPoint roomName = (IPEndPoint)ssm.Get(SignalingAttribute.RoomAddress);
            string     tag      = (string)ssm.Get(SignalingAttribute.RoomTag);
            string     key      = roomName.ToString();

            lock (_lock)
            {
                Room target = room.Find(x => x.Key == key && x.Tag == tag);
                if (target != null)
                {
                    if (senderTable.ContainsKey(key))
                    {
                        ResponseJoinRoom(true);
                        ResponseConnectionAttemptRequest(key, RemoteEndPoint);
                    }
                    else
                    {
                        ResponseJoinRoom(false);
                    }
                    Console.WriteLine("Peer '{0}' Join Room: '{1}'", RemoteEndPoint.ToString(), key);
                }
                else
                {
                    Console.WriteLine("Peer '{0}' Join Room: '{1}' Failed", RemoteEndPoint.ToString(), key);
                }
            }
        }
Пример #4
0
        public void OnResponse(byte[] buffer, long offset, long size)
        {
            SignalingServerMessage ssM = new SignalingServerMessage();

            if (!ssM.FromBuffer(buffer, offset, size))
            {
                return;
            }
            Console.WriteLine("\nIncomming Request: {0}", Enum.GetName(typeof(SignalingMethod), ssM.methodType));
            switch (ssM.methodType)
            {
            case SignalingMethod.CreateRoomRequest:
                OnCreateRoom(ssM);
                break;

            case SignalingMethod.CloseRoomRequest:
                OnCloseRoom(ssM);
                break;

            case SignalingMethod.JoinRoomRequest:
                OnJoinRoom(ssM);
                break;

            case SignalingMethod.GetRoomListRequest:
                OnGetRoomList(ssM);
                break;
            }
        }
Пример #5
0
        private void ResponseCreateRoom(bool success)
        {
            SignalingServerMessage ssm = new SignalingServerMessage(SignalingMethod.CreateRoomResponse);

            if (success)
            {
                ssm.WriteEmpty(SignalingAttribute.Success);
            }
            else
            {
                ssm.WriteEmpty(SignalingAttribute.Failed);
            }
            sender.Send(ssm.WriteRequest());
        }
Пример #6
0
        private void ResponseGetRoomList(string tag)
        {
            SignalingServerMessage ssm = new SignalingServerMessage(SignalingMethod.GetRoomListResponse);
            //
            List <Room> sameTag;

            lock (_lock)
            {
                sameTag = room.FindAll(x => x.Tag == tag);
            }
            //Console.WriteLine("Result Room Tag: {0}, total: {1}", tag, sameTag.Count);
            for (int i = 0; i < sameTag.Count; i++)
            {
                Console.WriteLine("Find: " + sameTag[i].ToString());
                byte[] addressByte = sameTag[i].IP.Address.GetAddressBytes();
                byte[] ip          = new byte[3 + addressByte.Length];
                if (addressByte.Length > 4)
                {
                    ip[0] = 0x2;
                }
                else
                {
                    ip[0] = 0x1;
                }
                ushort port = (ushort)sameTag[i].IP.Port;
                ip[2] = (byte)(port & 0xff);
                ip[1] = (byte)((port >> 8) & 0xff);
                Array.Copy(addressByte, 0, ip, 3, addressByte.Length);
                ssm.WriteString(SignalingAttribute.RoomName, sameTag[i].Name);
                ssm.WriteString(SignalingAttribute.RoomDescription, sameTag[i].Description);
                ssm.WriteBytes(SignalingAttribute.RoomAddress, ip);
                Console.WriteLine("Key: " + sameTag[i].Key);
            }
            Console.WriteLine("Result Room Tag: {0}, total: {1}", tag, sameTag.Count);
            sender.Send(ssm.WriteRequest());
        }
Пример #7
0
 private void OnLeaveRoom(SignalingServerMessage ssm)
 {
 }
Пример #8
0
 private void OnGetRoomList(SignalingServerMessage ssm)
 {
     ResponseGetRoomList((string)ssm.Get(SignalingAttribute.RoomTag));
 }
Пример #9
0
        private void OnCloseRoom(SignalingServerMessage ssm)
        {
            IPEndPoint ipe = (IPEndPoint)ssm.Get(SignalingAttribute.RoomAddress);

            CloseRoom(ipe.ToString());
        }