Exemplo n.º 1
0
        public static HNode ConnectNew(IPEndPoint endpoint)
        {
            var node = new HNode();

            node.Connect(endpoint);

            return(node);
        }
Exemplo n.º 2
0
        public void Intercept(HotelEndPoint endpoint)
        {
            listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            listener.Bind(new IPEndPoint(IPAddress.Any, 38101));
            listener.Listen(1);

            int interceptCount = 0;

            while (!IsConnected)
            {
                try
                {
                    Local = new HNode(listener.Accept());

                    if (++interceptCount == SocketSkip)
                    {
                        interceptCount = 0;
                        continue;
                    }

                    byte[] buffer = Local.Peek(6);
                    Remote = HNode.ConnectNew(endpoint);
                    if (BigEndian.ToUInt16(buffer, 4) == 4000)
                    {
                        IsConnected = true;
                        OnConnected(EventArgs.Empty);

                        _inSteps  = 0;
                        _outSteps = 0;
                        Task.Factory.StartNew(InterceptOutgoing);
                        Task.Factory.StartNew(InterceptIncoming);
                    }
                    else
                    {
                        buffer = Local.Receive(512);
                        Remote.Send(buffer);

                        buffer = Encoding.UTF8.GetBytes("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                                                        "<cross-domain-policy xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://www.adobe.com/xml/schemas/PolicyFileSocket.xsd\">" +
                                                        "<allow-access-from domain=\"localhost\" to-ports=\"38101\"/>" +
                                                        "</cross-domain-policy>\0");
                        Local.Send(buffer);
                        continue;
                    }
                }
                finally
                {
                    if (!IsConnected)
                    {
                        Local?.Dispose();
                        Remote?.Dispose();
                    }
                }
            }
        }