Пример #1
0
        public ActiveEmptySharing()
        {
            // Creates a empty share
            Share share = new Share("Testing");

            // Port to listen for incomming connections on
            share.Port = 12345;
            AddFilelistsToShare(share);

            incomingConnectionListener         = new TcpConnectionListener(share.Port);
            incomingConnectionListener.Update += new FmdcEventHandler(Connection_Update);
            incomingConnectionListener.Start();

            HubSetting setting = new HubSetting();

            setting.Address     = "127.0.0.1";
            setting.Port        = 411;
            setting.DisplayName = "FlowLib";
            setting.Protocol    = "Auto";

            Hub hubConnection = new Hub(setting);

            hubConnection.ProtocolChange += new FmdcEventHandler(hubConnection_ProtocolChange);
            hubConnection.Share           = share;
            hubConnection.Me.Mode         = FlowLib.Enums.ConnectionTypes.Direct;
            hubConnection.Connect();
        }
        public void KeepAliveServerTest()
        {
            ManualResetEvent mutex = new ManualResetEvent(false);
            TcpConnection    listenerConnectionToClient = null;

            using (TcpConnectionListener listener = new TcpConnectionListener(new NetworkEndPoint(IPAddress.Any, 4296)))
                using (TcpConnection connection = new TcpConnection(new NetworkEndPoint(IPAddress.Loopback, 4296)))
                {
                    listener.NewConnection += delegate(object sender, NewConnectionEventArgs args)
                    {
                        listenerConnectionToClient = (TcpConnection)args.Connection;
                        listenerConnectionToClient.KeepAliveInterval = 100;

                        Thread.Sleep(1050); //Enough time for ~10 keep alive packets

                        mutex.Set();
                    };

                    listener.Start();

                    connection.Connect();

                    mutex.WaitOne();

                    Assert.IsNotNull(listenerConnectionToClient);
                    Assert.IsTrue(
                        listenerConnectionToClient.Statistics.TotalBytesSent >= 30 &&
                        listenerConnectionToClient.Statistics.TotalBytesSent <= 50,
                        "Sent: " + listenerConnectionToClient.Statistics.TotalBytesSent
                        );
                }
        }
        public void TcpIPv4ConnectionTest()
        {
            using (TcpConnectionListener listener = new TcpConnectionListener(IPAddress.Any, 4296, IPMode.IPv4))
                using (TcpConnection connection = new TcpConnection(new NetworkEndPoint(IPAddress.Loopback, 4296, IPMode.IPv4)))
                {
                    listener.Start();

                    connection.Connect();
                }
        }
Пример #4
0
        protected virtual void DoUPnPExternalRecieveAccess()
        {
            try
            {
                tcpListener         = new TcpConnectionListener(Port);
                tcpListener.Update += new FmdcEventHandler(DoUPnPExternalRecieveAccess_Update);
                tcpListener.Start();

                // Don't do this if we don't have external ip
                if (
                    (Functions.UPnPDevices & Success) == Functions.UPnPDevices &&
                    (Functions.UPnPIGD & Success) == Functions.UPnPIGD &&
                    (Functions.UPnPExternalIp & Success) == Functions.UPnPExternalIp &&
                    (Functions.UPnPAddMapping & Success) == Functions.UPnPAddMapping &&
                    (Functions.UPnPGetMapping & Success) == Functions.UPnPGetMapping &&
                    (Functions.UPnPDeleteMapping & Success) == Functions.UPnPDeleteMapping
                    )
                {
                    bool hasAddPortMapping = false;
                    if (hasAddPortMapping = wanipService.AddPortMapping(this, mapping))
                    {
                        //Transfer transfer = new Transfer(ExternalIPUPnP.ToString(), Port);
                        Transfer transfer = new Transfer(ExternalIP.ToString(), Port);
                        UserInfo me       = new UserInfo();
                        me.DisplayName     = "upnp";
                        me.TagInfo.Version = "FlowLibPowered";
                        transfer.Share     = new Share("temp");
                        transfer.Me        = me;
                        transfer.Source    = new Source(ExternalIP.ToString(), "upnp");
                        transferManager.AddTransferReq(new TransferRequest("upnp", null, new UserInfo()));

                        transfer.Protocol = new TransferNmdcProtocol(transfer);
                        transferManager.StartTransfer(transfer);

                        // Wait 60 seconds before continue
                        int i = 0;
                        do
                        {
                            Thread.Sleep(50);
                        } while (((Functions.UPnPExternalRecieveAccess & Success) != Functions.UPnPExternalRecieveAccess) && i++ > 1200);

                        // clean;
                        wanipService.DeletePortMapping(this, mapping);
                    }
                }

                tcpListener.Update -= DoUPnPExternalRecieveAccess_Update;
                tcpListener.End();
                tcpListener = null;
            }
            finally
            {
                Progress = Functions.End;
            }
        }
Пример #5
0
        protected virtual void DoInternalListenRecieveAccess()
        {
            try
            {
                tcpListener         = new TcpConnectionListener(Port);
                tcpListener.Update += new FmdcEventHandler(DoInternalListenAccess_Update);
                tcpListener.Start();
                try
                {
                    // TODO: We shouldn't limit ourself to IPv4.
                    Transfer transfer = new Transfer("127.0.0.1", Port);
                    UserInfo me       = new UserInfo();
                    me.DisplayName     = "loopback";
                    me.TagInfo.Version = "FlowLibPowered";
                    transfer.Share     = new Share("temp");
                    transfer.Me        = me;
                    // TODO: We shouldn't limit ourself to IPv4.
                    transfer.Source = new Source("127.0.0.1", "loopback");
                    //transferManager.AddTransferReq(new TransferRequest("loopback", null, new UserInfo()));
                    transferManager.AddTransferReq(new TransferRequest(transfer.Source));

                    transfer.Protocol = new TransferNmdcProtocol(transfer);
                    transferManager.StartTransfer(transfer);
                    Success |= Functions.InternalListenAccess;
                }
                catch
                {
                    // We do not have InternalListenAccess.
                    Success = Success & ~Functions.InternalListenAccess;
                }

                // Wait 10 seconds before continue
                int i = 0;
                do
                {
                    Thread.Sleep(50);
                } while (((Functions.InternalRecieveAccess & Success) != Functions.InternalRecieveAccess) && i++ > 200);


                // clean;
                tcpListener.Update -= DoInternalListenAccess_Update;
                tcpListener.End();
                tcpListener = null;
            }
            finally
            {
                Progress = Functions.ExternalIp;
            }
        }
Пример #6
0
        public void TcpHandshakeTest()
        {
            using (TcpConnectionListener listener = new TcpConnectionListener(new NetworkEndPoint(IPAddress.Any, 4296, IPMode.IPv4)))
                using (TcpConnection connection = new TcpConnection(new NetworkEndPoint(IPAddress.Loopback, 4296, IPMode.IPv4)))
                {
                    listener.Start();

                    listener.NewConnection += delegate(object sender, NewConnectionEventArgs e)
                    {
                        Assert.IsTrue(Enumerable.SequenceEqual(e.HandshakeData, new byte[] { 1, 2, 3, 4, 5, 6 }));
                    };

                    connection.Connect(new byte[] { 1, 2, 3, 4, 5, 6 });
                }
        }
Пример #7
0
        protected virtual void DoExternalRecieveAccess()
        {
            try
            {
                tcpListener         = new TcpConnectionListener(Port);
                tcpListener.Update += new FmdcEventHandler(DoExternalRecieveAccess_Update);
                tcpListener.Start();

                // Don't do this if we don't have external ip
                if ((Functions.ExternalIp & Success) == Functions.ExternalIp)
                {
                    Transfer transfer = new Transfer(ExternalIP.ToString(), Port);
                    UserInfo me       = new UserInfo();
                    me.DisplayName     = "external";
                    me.TagInfo.Version = "FlowLibPowered";
                    transfer.Share     = new Share("temp");
                    transfer.Me        = me;
                    transfer.Source    = new Source(ExternalIP.ToString(), "external");
                    transferManager.AddTransferReq(new TransferRequest("external", null, new UserInfo()));

                    transfer.Protocol = new TransferNmdcProtocol(transfer);
                    transferManager.StartTransfer(transfer);
                }

                // Wait 30 seconds before continue
                int i = 0;
                do
                {
                    Thread.Sleep(50);
                } while (((Functions.ExternalRecieveAccess & Success) != Functions.ExternalRecieveAccess) && i++ > 600);

                // clean;
                tcpListener.Update -= DoExternalRecieveAccess_Update;
                tcpListener.End();
                tcpListener = null;
            }
            finally
            {
                if ((Functions.ExternalRecieveAccess & Success) == Functions.ExternalRecieveAccess)
                {
                    Progress = Functions.End;
                }
                else
                {
                    Progress = Functions.UPnPDevices;
                }
            }
        }
        public void KeepAliveClientTest()
        {
            using (TcpConnectionListener listener = new TcpConnectionListener(new NetworkEndPoint(IPAddress.Any, 4296)))
                using (TcpConnection connection = new TcpConnection(new NetworkEndPoint(IPAddress.Loopback, 4296)))
                {
                    listener.Start();

                    connection.Connect();
                    connection.KeepAliveInterval = 100;

                    System.Threading.Thread.Sleep(1050); //Enough time for ~10 keep alive packets

                    Assert.IsTrue(
                        connection.Statistics.TotalBytesSent >= 30 &&
                        connection.Statistics.TotalBytesSent <= 50,
                        "Sent: " + connection.Statistics.TotalBytesSent
                        );
                }
        }
        public void TcpFieldTest()
        {
            NetworkEndPoint ep = new NetworkEndPoint(IPAddress.Loopback, 4296);

            using (TcpConnectionListener listener = new TcpConnectionListener(IPAddress.Any, 4296))
                using (TcpConnection connection = new TcpConnection(ep))
                {
                    listener.Start();

                    connection.Connect();

                    //Connection fields
                    Assert.AreEqual(ep, connection.EndPoint);

                    //TcpConnection fields
                    Assert.AreEqual(new IPEndPoint(IPAddress.Loopback, 4296), connection.RemoteEndPoint);
                    Assert.AreEqual(0, connection.Statistics.DataBytesSent);
                    Assert.AreEqual(0, connection.Statistics.DataBytesReceived);
                }
        }
    static void Main(string[] args)
    {
        //Setup listener
        using (TcpConnectionListener listener = new TcpConnectionListener(new NetworkEndPoint(IPAddress.Any, 4296)))
        {
            //Start listening for new connection events
            listener.NewConnection += delegate(object sender, NewConnectionEventArgs a)
            {
                //Send the client some data
                a.Connection.SendBytes(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 }, SendOption.Reliable);

                //Disconnect from the client
                a.Connection.Close();
            };

            listener.Start();

            Console.ReadKey();
        }
    }
        void SetListener(uint16_t inPort)
        {
            // tcp server
            listener = new TcpConnectionListener(new NetworkEndPoint(IPAddress.Any, inPort));


            //Setup listener
            listener.NewConnection += delegate(object sender, NewConnectionEventArgs args)
            {
                NetIncomingMessage msg1 = GetServer().CreateIncomingMessage(NetIncomingMessageType.TcpStatusChanged, 4 + 1);
                msg1.isTcp            = true;
                msg1.m_senderEndPoint = ((NetworkEndPoint)(args.Connection).EndPoint).EndPoint as IPEndPoint;
                msg1.Write((byte)NetConnectionStatus.Connected);
                msg1.Write(string.Empty);
                GetServer().ReleaseMessage(msg1);

                args.Connection.DataReceived += delegate(object innerSender, DataReceivedEventArgs innerArgs)
                {
                    NetIncomingMessage msg = GetServer().CreateIncomingMessage(NetIncomingMessageType.Data, innerArgs.Bytes.Length);
                    msg.isTcp            = true;
                    msg.m_TcpConnecton   = (TcpConnection)innerSender;
                    msg.m_senderEndPoint = ((NetworkEndPoint)((Connection)innerSender).EndPoint).EndPoint as IPEndPoint;
                    Buffer.BlockCopy(innerArgs.Bytes, 2, msg.m_data, 0, innerArgs.Bytes.Length - 2);
                    msg.m_bitLength = (ushort)((innerArgs.Bytes[0] << 8) | innerArgs.Bytes[1]);
                    //Log.Information($"TCP recv length {msg.LengthBytes}, session_id {msg.m_TcpConnecton.sessionId}");
                    GetServer().ReleaseMessage(msg);
                };

                args.Connection.Disconnected += delegate(object sender2, DisconnectedEventArgs args2)
                {
                    NetIncomingMessage msg = GetServer().CreateIncomingMessage(NetIncomingMessageType.TcpStatusChanged, 4 + 1);
                    msg.isTcp            = true;
                    msg.m_senderEndPoint = ((NetworkEndPoint)((Connection)sender2).EndPoint).EndPoint as IPEndPoint;
                    msg.Write((byte)NetConnectionStatus.Disconnected);
                    msg.Write(string.Empty);
                    GetServer().ReleaseMessage(msg);
                };
            };

            listener.Start();
        }
Пример #12
0
        public MultiConnections()
        {
            UpdateBase = new FmdcEventHandler(PassiveConnectToUser_UpdateBase);

            // Creates a empty share
            Share share = new Share("Testing");

            // Port to listen for incomming connections on
            share.Port = 12345;

            incomingConnectionListener         = new TcpConnectionListener(share.Port);
            incomingConnectionListener.Update += new FmdcEventHandler(Connection_Update);
            incomingConnectionListener.Start();

            // Adds common filelist to share
            AddFilelistsToShare(share);

            HubSetting setting = new HubSetting();

            setting.Address     = "127.0.0.1";
            setting.Port        = 411;
            setting.DisplayName = "FlowLib";
            setting.Protocol    = "Auto";

            hub = new Hub(setting, this);
            hub.ProtocolChange += new FmdcEventHandler(hubConnection_ProtocolChange);
            // Adds share to hub
            hub.Share   = share;
            hub.Me.Mode = FlowLib.Enums.ConnectionTypes.Direct;
            hub.Connect();


            last            = System.DateTime.Now.Ticks;
            timer.Elapsed  += new System.Timers.ElapsedEventHandler(timer_Elapsed);
            timer.AutoReset = true;
            timer.Interval  = 1000;
            timer.Start();
        }
Пример #13
0
        public void Start()
        {
            NetworkEndPoint    endPoint = new NetworkEndPoint(IPAddress.Any, portNumber);
            ConnectionListener listener = new TcpConnectionListener(endPoint);

            Running = true;

            Console.WriteLine("Starting server!");
            Console.WriteLine("Server listening on " + (listener as TcpConnectionListener).EndPoint);

            listener.NewConnection += NewConnectionHandler;
            listener.Start();

            while (Running)
            {
                //Do nothing
            }

            //Close all
            listener.Close();
            //Exit 0
            Environment.Exit(0);
        }
        public ActiveEmptySharingUsingTLS()
        {
            // Creates a empty share
            Share share = new Share("Testing");

            // Port to listen for incomming connections on
            share.Port = 12345;
            AddFilelistsToShare(share);

            incomingConnectionListener         = new TcpConnectionListener(share.Port);
            incomingConnectionListener.Update += new FmdcEventHandler(Connection_Update);
            incomingConnectionListener.Start();

            // TLS listener
            incomingConnectionListenerTLS         = new TcpConnectionListener(tlsport);
            incomingConnectionListenerTLS.Update += new FmdcEventHandler(Connection_UpdateTLS);
            incomingConnectionListenerTLS.Start();

            HubSetting setting = new HubSetting();

            setting.Address     = "127.0.0.1";
            setting.Port        = 411;
            setting.DisplayName = "FlowLibActiveTLS";
            setting.Protocol    = "Auto";

            Hub hubConnection = new Hub(setting);

            hubConnection.ProtocolChange += new FmdcEventHandler(hubConnection_ProtocolChange);
            hubConnection.Share           = share;
            hubConnection.Me.Mode         = FlowLib.Enums.ConnectionTypes.Direct;
#if !COMPACT_FRAMEWORK
// Security, Windows Mobile doesnt support SSLStream so we disable this feature for it.
            hubConnection.Me.Set(UserInfo.SECURE, tlsport.ToString());
#endif
            hubConnection.Connect();
        }
Пример #15
0
 internal void OnStart()
 {
     tcpListener.Start();
     gameSession = new OnlineGameSession();
 }
Пример #16
0
        public DcBot(HubSetting settings)
        {
            UpdateBase = new FlowLib.Events.FmdcEventHandler(DcBot_UpdateBase);

            downloadManager.DownloadCompleted += new FmdcEventHandler(downloadManager_DownloadCompleted);

            // Creates a empty share
            Share share = new Share("Testing");

            // Do we want bot to be active?
            if (Program.USE_ACTIVE_MODE)
            {
                if (Program.PORT_TLS > 0)
                {
                    share.Port = Program.PORT_TLS;
                }
                else
                {
                    share.Port = Program.PORT_ACTIVE;
                }

                incomingConnectionListener         = new TcpConnectionListener(Program.PORT_ACTIVE);
                incomingConnectionListener.Update += new FmdcEventHandler(Connection_Update);
                incomingConnectionListener.Start();

                // TLS listener
                incomingConnectionListenerTLS         = new TcpConnectionListener(Program.PORT_TLS);
                incomingConnectionListenerTLS.Update += new FmdcEventHandler(Connection_UpdateTLS);
                incomingConnectionListenerTLS.Start();
            }
            // Adds common filelist to share
            AddFilelistsToShare(share);

            hubConnection = new Hub(settings, this);

            hubConnection.Me.TagInfo.Version = "Serie V:20101125";
            hubConnection.Me.TagInfo.Slots   = 2;
            // DO NOT CHANGE THIS LINE!
            hubConnection.Me.Set(UserInfo.PID, "7OP7K374IKV7YMEYUI5F5R4YICFT36M7FL64AWY");


            // Adds share to hub
            hubConnection.Share = share;


            // Do we want bot to be active?
            if (Program.USE_ACTIVE_MODE)
            {
                hubConnection.Me.TagInfo.Mode = FlowLib.Enums.ConnectionTypes.Direct;
                hubConnection.Me.Set(UserInfo.SECURE, Program.PORT_TLS.ToString());
            }
            else
            {
                hubConnection.Me.TagInfo.Mode = FlowLib.Enums.ConnectionTypes.Passive;
                hubConnection.Me.Set(UserInfo.SECURE, "");
            }

            hubConnection.ConnectionStatusChange += new FmdcEventHandler(hubConnection_ConnectionStatusChange);
            hubConnection.ProtocolChange         += new FmdcEventHandler(hubConnection_ProtocolChange);
            hubConnection.SecureUpdate           += new FmdcEventHandler(hubConnection_SecureUpdate);
        }
Пример #17
0
        public CollectTransferedInformationFromFilelistDownload()
        {
            UpdateBase = new FmdcEventHandler(PassiveConnectToUser_UpdateBase);

            // Creates a empty share
            Share share = new Share("Testing");

            // Port to listen for incomming connections on
            share.Port = 12345;

            incomingConnectionListener         = new TcpConnectionListener(share.Port);
            incomingConnectionListener.Update += new FmdcEventHandler(Connection_Update);
            incomingConnectionListener.Start();

            // Adds common filelist to share
            AddFilelistsToShare(share);

            HubSetting setting = new HubSetting();

            setting.Address     = "127.0.0.1";
            setting.Port        = 411;
            setting.DisplayName = "FlowLib";
            setting.Protocol    = "Auto";

            Hub hubConnection = new Hub(setting, this);

            hubConnection.ProtocolChange += new FmdcEventHandler(hubConnection_ProtocolChange);
            // Adds share to hub
            hubConnection.Share = share;
            //hubConnection.Me.Mode = FlowLib.Enums.ConnectionTypes.Direct;
            hubConnection.Me.TagInfo.Slots = 2;
            hubConnection.Connect();


            FlowLib.Utils.Convert.General.BinaryPrefixes bp;
            Console.WriteLine("Press any key to update information");
            do
            {
                Console.ReadKey(true);
                Console.Clear();
                Console.WriteLine("Press any key to update information");
                Console.WriteLine("==================================");
                if (stats != null)
                {
                    Console.WriteLine("Total data sent: " + FlowLib.Utils.Convert.General.FormatBytes(stats.TotalBytesSent, out bp) + bp);
                    Console.WriteLine("Total data received: " + FlowLib.Utils.Convert.General.FormatBytes(stats.TotalBytesReceived, out bp) + bp);
                    Console.WriteLine("current download speed: " + FlowLib.Utils.Convert.General.FormatBytes(stats.CurrentReceiveSpeed, out bp) + bp + "/s");
                    Console.WriteLine("current upload speed: " + FlowLib.Utils.Convert.General.FormatBytes(stats.CurrentSendSpeed, out bp) + bp + "/s");
                    Decimal d = new decimal(stats.MaximumReceiveSpeed);
                    Console.WriteLine("Maximum download speed: " + FlowLib.Utils.Convert.General.FormatBytes(decimal.ToInt64(d), out bp) + bp + "/s");
                    d = new decimal(stats.MaximumSendSpeed);
                    Console.WriteLine("Maximum upload speed: " + FlowLib.Utils.Convert.General.FormatBytes(decimal.ToInt64(d), out bp) + bp + "/s");
                    d = new decimal(stats.MinimumReceiveSpeed);
                    Console.WriteLine("Minimum download speed: " + FlowLib.Utils.Convert.General.FormatBytes(decimal.ToInt64(d), out bp) + bp + "/s");
                    d = new decimal(stats.MinimumSendSpeed);
                    Console.WriteLine("Minimum upload speed: " + FlowLib.Utils.Convert.General.FormatBytes(decimal.ToInt64(d), out bp) + bp + "/s");
                }
                else
                {
                    Console.WriteLine("No transfer has started yet.");
                }
                Console.WriteLine("==================================");
            } while (true);
        }