Exemplo n.º 1
0
        static void Main()
        {
            TcpPeer peer = new TcpPeer();

            peer.Connect(IPAddress.Loopback, 50050);
            peer.OnConnected += () =>
            {
                Console.WriteLine("Connected callback!");

                Console.Write("Sending message...");
                ConsoleMessage message = new ConsoleMessage {
                    Text = "New Client!"
                };
                peer.Send(message);
                Console.WriteLine("Message Sent!");
            };

            string cmd = string.Empty;

            while (cmd != "q")
            {
                cmd = Console.ReadLine();
                peer.Send(new ConsoleMessage {
                    Text = cmd
                });
            }

            Console.ReadLine();
            peer.Disconnect();
        }
Exemplo n.º 2
0
        public TcpConnection(TcpPeer parent)
        {
            if (parent == null)
            {
                throw new ArgumentNullException(nameof(parent));
            }
            this.latencySimulationRecvQueue = new ConcurrentQueue <DelayedMessage>();
            this.stashed               = true;
            this.Statistics            = new TcpConnectionStatistics();
            this.sendSemaphore         = new SemaphoreSlim(1, 1);
            this.recvBuffer            = new byte[parent.Configuration.BufferSize];
            this.sendBuffer            = new byte[parent.Configuration.BufferSize];
            this.awaitingMessageHeader = new TcpRawMessageHeader();
            this.Parent = parent;
            this.logger = parent.Configuration.LogManager.GetLogger(nameof(TcpConnection));
            this.logger.Meta["kind"] = this.GetType().Name;
            this.logger.Meta["connection_endpoint"] = new RefLogLabel <TcpConnection>(this, v => v.RemoteEndpoint);
            this.logger.Meta["connected"]           = new RefLogLabel <TcpConnection>(this, s => s.Connected);
            this.logger.Meta["closed"]  = new RefLogLabel <TcpConnection>(this, s => s.closed);
            this.logger.Meta["latency"] = new RefLogLabel <TcpConnection>(this, s =>
            {
                var lat = s.Statistics.Latency;
                if (lat.HasValue)
                {
                    return(lat.Value);
                }
                else
                {
                    return("");
                }
            });

            // return $"{nameof(TcpConnection)}[id={Id}, connected={Connected}, endpoint={RemoteEndpoint}]";
        }
Exemplo n.º 3
0
 internal void CheckParent(TcpPeer parent)
 {
     if (this.Parent != parent)
     {
         throw new InvalidOperationException($"This connection belongs to the another parent");
     }
 }
Exemplo n.º 4
0
        public void TestConnect()
        {
            TcpPeer tcpPeer = new TcpPeer();

            tcpPeer.Connect(IPAddress.Parse("192.168.1.1"), 80);
            //  tcpPeer.Send(new TestPacket());
        }
Exemplo n.º 5
0
    public NetWorkManage()
    {
        // 初始化网络
        PacketGate   = new PacketGate();
        TcpPeerAgent = new TcpPeer();

        TcpPeerAgent.OnReceivedPacket += new TcpPeer.PacketHandler(PacketGate.ProcessPacket);
    }
Exemplo n.º 6
0
        public override void ChannelRead(IChannelHandlerContext context, object message)
        {
            var str = message as string;

            if (peer == null)
            {
                peer = connector.AddTcpClient(context);
                peer.OnRecieve(context, str).ConfigureAwait(false).GetAwaiter().GetResult();
            }
            else
            {
                peer.OnRecieve(context, str).ConfigureAwait(false).GetAwaiter().GetResult();
            }
        }
Exemplo n.º 7
0
 internal RpcTcpConnection(TcpPeer parent, IRpcPeer rpcPeer, RpcConfiguration configuration) : base(parent)
 {
     if (parent == null)
     {
         throw new ArgumentNullException(nameof(parent));
     }
     if (rpcPeer == null)
     {
         throw new ArgumentNullException(nameof(rpcPeer));
     }
     if (configuration == null)
     {
         throw new ArgumentNullException(nameof(configuration));
     }
     this.configuration = configuration;
     this.rpcPeer       = rpcPeer;
 }
 internal RpcTcpConnectionEncrypted(TcpPeer parent, IRpcPeer rpcPeer, RpcConfiguration configuration) : base(
         parent, rpcPeer, configuration)
 {
 }
Exemplo n.º 9
0
 private static void OnConnected(TcpPeer peer)
 {
     Console.WriteLine("Peer Connected");
     _peer = peer;
     _peer.OnPacketReceived += OnReceived;
 }
Exemplo n.º 10
0
 public void CreateTcpPeer()
 {
     TcpPeer tcpPeer = new TcpPeer();
 }
Exemplo n.º 11
0
 public PeerMessageHandler(TcpPeer oldpeer)
 {
     this.peer = oldpeer;
 }