Example #1
0
        public async Task ShouldTriggerConnectionSentWhenSentSomeBytes()
        {
            NetworkDirection       direction = NetworkDirection.Outgoing;
            NetworkOutgoingMessage message   = new RandomMessage(113);

            using (NetworkFixture fixture = new NetworkFixture())
                using (TcpSocket host = fixture.Pool.New())
                    using (TcpSocket socket = fixture.Pool.New())
                    {
                        TcpSocketInfo info     = host.BindAndInfo();
                        int           port     = info.Endpoint.Port;
                        IPEndPoint    endpoint = new IPEndPoint(IPAddress.Loopback, port);

                        socket.Bind();
                        host.Listen(10);
                        host.Accept(null);

                        await socket.Connect(endpoint);

                        NetworkConnection connection = fixture.Pool.Create(socket, direction, endpoint);
                        Trigger           handler    = Trigger.Bind(ref fixture.Hooks.OnConnectionSent, data =>
                        {
                            data.Remote.Should().Be(NetworkAddress.Parse(endpoint));
                            data.Connection.Should().Be(connection);
                            data.Bytes.Should().Be(message.Length);
                        });

                        connection.Send(message);
                        handler.Wait().Should().BeTrue();
                    }
        }
            public void IncrementStat(NetworkDirection direction, string entryName, int amount)
            {
                NetworkOperationEntryDetails entry;

                if (m_Entries.ContainsKey(entryName))
                {
                    entry = m_Entries[entryName];
                }
                else
                {
                    entry                = new NetworkOperationEntryDetails();
                    entry.m_EntryName    = entryName;
                    m_Entries[entryName] = entry;
                }
                entry.AddStat(direction, amount);

                switch (direction)
                {
                case NetworkDirection.Incoming:
                {
                    totalIn += amount;
                }
                break;

                case NetworkDirection.Outgoing:
                {
                    totalOut += amount;
                }
                break;
                }
            }
Example #3
0
        /// <summary>
        /// Records transport traffic
        /// </summary>
        /// <param name="direction"></param>
        /// <param name="channelId"></param>
        /// <param name="numBytes"></param>
        public static void RecordTraffic(NetworkDirection direction, int channelId, int numBytes)
        {
            if (NetworkProfiler.IsRecording)
            {
                foreach (NetworkProfileChannel c in NetworkProfiler.CurrentTick.Channels)
                {
                    if (c.Id == channelId)
                    {
                        if (direction == NetworkDirection.Incoming)
                        {
                            c.BytesIncoming += numBytes;
                        }
                        else
                        {
                            c.BytesOutgoing += numBytes;
                        }
                        return;
                    }
                }

                NetworkProfiler.CurrentTick.Channels.Add(new NetworkProfileChannel
                {
                    Id            = channelId,
                    BytesIncoming = direction == NetworkDirection.Incoming ? numBytes : 0,
                    BytesOutgoing = direction == NetworkDirection.Outgoing ? numBytes : 0,
                });
            }
        }
Example #4
0
 /// <summary>
 /// Records a message
 /// </summary>
 /// <param name="direction"></param>
 /// <param name="messageType"></param>
 /// <param name="entryName"></param>
 /// <param name="count"></param>
 public static void RecordMessage(NetworkDirection direction, Type messageType, string entryName, int count)
 {
     if (NetworkProfiler.IsRecording)
     {
         NetworkProfiler.CurrentTick.RecordMessage(direction, messageType, entryName, count);
     }
 }
Example #5
0
        private void OnAccept(TcpSocketAccept data)
        {
            bool canAcceptMore =
                data.Status != SocketStatus.NotSocket &&
                data.Status != SocketStatus.OperationAborted;

            if (canAcceptMore)
            {
                data.Socket.Accept(OnAccept);
            }

            if (data.Status == SocketStatus.OK)
            {
                IPEndPoint     endpoint = data.GetRemote();
                NetworkAddress remote   = NetworkAddress.Parse(endpoint);

                NetworkDirection  direction  = NetworkDirection.Incoming;
                NetworkConnection connection = dependencies.Network.Create(data.Connection, direction, endpoint);

                hooks.CallConnectionArrived(remote, connection);
            }
            else
            {
                data.Connection?.Dispose();
            }
        }
Example #6
0
        /// <summary>
        /// Creates a new instance of the network connection from the existing
        /// instance. The inner socket and direction will copied, but the caller
        /// can decide how the encryption and decryption will work.
        /// </summary>
        /// <param name="connection">The existing instance of the connection.</param>
        /// <param name="configurer">The new configuration.</param>
        public NetworkPoolConnection(NetworkPoolConnection connection, NetworkConfiguration configurer)
        {
            listener   = connection.listener;
            socket     = connection.socket;
            remote     = connection.remote;
            direction  = connection.direction;
            identifier = connection.identifier;

            incoming = new NetworkIncomingBuffer(connection.incoming, configurer.Decryptor);
            outgoing = new NetworkOutgoingBuffer(connection.outgoing, configurer.Encryptor);
        }
Example #7
0
        /// <summary>
        /// Creates a new instance of the network connection relying on the
        /// already connected socket instance and direction value indicating
        /// who is the initiator of the connection.
        /// </summary>
        /// <param name="listener"></param>
        /// <param name="socket">The already connected socket.</param>
        /// <param name="direction">The direction indicating who initiated the connection.</param>
        /// <param name="identifier"></param>
        public NetworkPoolConnection(NetworkPoolListener listener, TcpSocket socket, NetworkDirection direction, long identifier, IPEndPoint remote)
        {
            this.listener   = listener;
            this.socket     = socket;
            this.direction  = direction;
            this.identifier = identifier;

            this.remote = NetworkAddress.Parse(remote);

            incoming = new NetworkIncomingBuffer(listener, socket, identifier);
            outgoing = new NetworkOutgoingBuffer(listener, socket, identifier);
        }
 public static void SetStat(NetworkDirection direction, short msgId, string entryName, int amount)
 {
     NetworkOperationDetails details;
     if (m_NetworkOperations.ContainsKey(msgId))
     {
         details = m_NetworkOperations[msgId];
     }
     else
     {
         details = new NetworkOperationDetails {
             MsgId = msgId
         };
         m_NetworkOperations[msgId] = details;
     }
     details.SetStat(direction, entryName, amount);
 }
Example #9
0
        public void ShouldCreateNewConnection()
        {
            IPAddress        address   = IPAddress.Parse("127.0.0.1");
            IPEndPoint       remote    = new IPEndPoint(address, 8080);
            NetworkDirection direction = NetworkDirection.Outgoing;

            using (NetworkFixture fixture = new NetworkFixture())
                using (TcpSocket socket = fixture.Pool.New())
                {
                    NetworkConnection connection = fixture.Pool.Create(socket, direction, remote);

                    connection.Should().NotBeNull();
                    connection.Direction.Should().Be(direction);
                    connection.Remote.Should().Be(NetworkAddress.Parse(remote));
                    connection.Identifier.Should().BeGreaterThan(0);
                }
        }
Example #10
0
        public void ShouldCreateChangedConnection()
        {
            IPAddress        address   = IPAddress.Parse("127.0.0.1");
            IPEndPoint       remote    = new IPEndPoint(address, 8080);
            NetworkDirection direction = NetworkDirection.Outgoing;

            using (NetworkFixture fixture = new NetworkFixture())
                using (TcpSocket socket = fixture.Pool.New())
                {
                    NetworkConnection origin  = fixture.Pool.Create(socket, direction, remote);
                    NetworkConnection changed = fixture.Pool.Change(origin, new NetworkConfiguration());

                    changed.Should().NotBeNull();
                    changed.Should().NotBeSameAs(origin);
                    changed.Identifier.Should().Be(origin.Identifier);
                }
        }
        static public void IncrementStat(NetworkDirection direction, short msgId, string entryName, int amount)
        {
            NetworkOperationDetails op;

            if (m_NetworkOperations.ContainsKey(msgId))
            {
                op = m_NetworkOperations[msgId];
            }
            else
            {
                op       = new NetworkOperationDetails();
                op.MsgId = msgId;
                m_NetworkOperations[msgId] = op;
            }

            op.IncrementStat(direction, entryName, amount);
        }
Example #12
0
        public static void SetStat(NetworkDirection direction, short msgId, string entryName, int amount)
        {
            NetworkOperationDetails details;

            if (m_NetworkOperations.ContainsKey(msgId))
            {
                details = m_NetworkOperations[msgId];
            }
            else
            {
                details = new NetworkOperationDetails {
                    MsgId = msgId
                };
                m_NetworkOperations[msgId] = details;
            }
            details.SetStat(direction, entryName, amount);
        }
Example #13
0
        public NetworkConnection Create(TcpSocket socket, NetworkDirection direction, IPEndPoint remote)
        {
            long identifier = Interlocked.Increment(ref sequence);
            NetworkPoolListener   listener   = new NetworkPoolListener(items, queue, hooks, configuration, dependencies);
            NetworkPoolConnection connection = new NetworkPoolConnection(listener, socket, direction, identifier, remote);

            lock (items)
            {
                items.Add(identifier, new NetworkPoolEntry
                {
                    Connection  = connection,
                    IsAvailable = true
                });
            }

            hooks.CallConnectionAttached(connection);
            return(connection);
        }
Example #14
0
        public async Task ShouldTriggerConnectionReceivedWhenReceivedSomeBytes()
        {
            NetworkDirection       direction = NetworkDirection.Outgoing;
            NetworkOutgoingMessage message   = new RandomMessage(113);

            using (NetworkFixture fixture = new NetworkFixture())
                using (TcpSocket host = fixture.Pool.New())
                    using (TcpSocket socket = fixture.Pool.New())
                    {
                        TcpSocketInfo info     = host.BindAndInfo();
                        int           port     = info.Endpoint.Port;
                        IPEndPoint    endpoint = new IPEndPoint(IPAddress.Loopback, port);

                        socket.Bind();
                        host.Listen(10);

                        Task <TcpSocketAccept> task = host.Accept();
                        await socket.Connect(endpoint);

                        TcpSocketAccept accept = await task;

                        NetworkConnection connection = fixture.Pool.Create(socket, direction, endpoint);
                        NetworkBlock      block      = new NetworkBlock(new byte[1024], 0, message.Length);

                        Trigger handler = Trigger.Bind(ref fixture.Hooks.OnConnectionReceived, data =>
                        {
                            data.Remote.Should().Be(NetworkAddress.Parse(endpoint));
                            data.Connection.Should().NotBeNull();
                            data.Bytes.Should().Be(message.Length);
                        });

                        connection.Receive(new NullReceiver());
                        message.ToBytes(block);

                        block.With((buffer, offset, count) =>
                        {
                            accept.Connection.Send(new SocketBuffer(buffer, offset, count), null);
                        });

                        handler.Wait().Should().BeTrue();
                    }
        }
        public static object NetworkDirectionToAbbr(NetworkDirection direction)
        {
            switch (direction)
            {
            case NetworkDirection.Na:
                return(Keywords.NA);

            case NetworkDirection.Send:
                return(Keywords.SEND_ABBR);

            case NetworkDirection.SendBlocking:
                return(Keywords.SEND_BLOCKING_ABBR);

            case NetworkDirection.Receive:
                return(Keywords.RECEIVE_ABBR);

            default:
                throw new ArgumentOutOfRangeException("network");
            }
        }
Example #16
0
        /// <summary>
        /// Records the message to the current tick
        /// </summary>
        /// <param name="direction">The direction of the message</param>
        /// <param name="messageType">The message type</param>
        /// <param name="entryName">The name of the entry, generally represents the target of the message</param>
        /// <param name="count">The number of these messages sent</param>
        public void RecordMessage(NetworkDirection direction, Type messageType, string entryName, int count)
        {
            this.TotalMessages += count;

            foreach (NetworkProfileMessage m in this.Messages)
            {
                if (m.Direction == direction && messageType == m.Type && entryName == m.Name)
                {
                    m.Count += count;
                    return;
                }
            }

            this.Messages.Add(new NetworkProfileMessage()
            {
                Type      = messageType,
                Direction = direction,
                Name      = entryName,
                Count     = count
            });
        }
            public void AddStat(NetworkDirection direction, int amount)
            {
                int tickId = ((int)s_LastTickTime) % kPacketHistoryTicks;

                switch (direction)
                {
                case NetworkDirection.Incoming:
                {
                    m_IncomingTotal += amount;
                    m_IncomingSequence.Add(tickId, amount);
                }
                break;

                case NetworkDirection.Outgoing:
                {
                    m_OutgoingTotal += amount;
                    m_OutgoingSequence.Add(tickId, amount);
                }
                break;
                }
            }
Example #18
0
        public async Task ShouldTriggerConnectionTerminatedWhenSending()
        {
            NetworkDirection direction = NetworkDirection.Outgoing;

            using (NetworkFixture fixture = new NetworkFixture())
                using (TcpSocket host = fixture.Pool.New())
                    using (TcpSocket socket = fixture.Pool.New())
                    {
                        TcpSocketInfo info     = host.BindAndInfo();
                        int           port     = info.Endpoint.Port;
                        IPEndPoint    endpoint = new IPEndPoint(IPAddress.Loopback, port);

                        socket.Bind();
                        host.Listen(10);

                        Task <TcpSocketAccept> task    = host.Accept();
                        TcpSocketConnect       connect = await socket.Connect(endpoint);

                        TcpSocketAccept accept = await task;

                        connect.Status.Should().Be(SocketStatus.OK);
                        accept.Status.Should().Be(SocketStatus.OK);
                        accept.Connection.Dispose();

                        NetworkConnection connection = fixture.Pool.Create(socket, direction, endpoint);
                        Trigger           handler    = Trigger.Bind(ref fixture.Hooks.OnConnectionTerminated, data =>
                        {
                            data.Remote.Should().Be(NetworkAddress.Parse(endpoint));
                            data.Connection.Should().NotBeNull();
                        });

                        for (int i = 0; i < 10; i++)
                        {
                            connection.Send(new OneByteMessage());
                        }

                        handler.Wait().Should().BeTrue();
                    }
        }
Example #19
0
        public void ShouldTriggerConnectionTerminatedWhenTerminated()
        {
            IPAddress        address   = IPAddress.Parse("127.0.0.1");
            IPEndPoint       remote    = new IPEndPoint(address, 8080);
            NetworkDirection direction = NetworkDirection.Outgoing;

            using (NetworkFixture fixture = new NetworkFixture())
            {
                Trigger handler = Trigger.Bind(ref fixture.Hooks.OnConnectionTerminated, data =>
                {
                    data.Remote.Should().Be(NetworkAddress.Parse(remote));
                    data.Connection.Should().NotBeNull();
                });

                using (TcpSocket socket = fixture.Pool.New())
                {
                    fixture.Pool.Create(socket, direction, remote).Terminate();
                }

                handler.Wait().Should().BeTrue();
            }
        }
 public SyncReceiveNetLatency(NetworkDirection dir)
 {
     networkDir = dir;
 }
Example #21
0
 public NetworkLatency(NetworkDirection dir)
 {
     networkDir = dir;
 }