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(); }
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}]"; }
internal void CheckParent(TcpPeer parent) { if (this.Parent != parent) { throw new InvalidOperationException($"This connection belongs to the another parent"); } }
public void TestConnect() { TcpPeer tcpPeer = new TcpPeer(); tcpPeer.Connect(IPAddress.Parse("192.168.1.1"), 80); // tcpPeer.Send(new TestPacket()); }
public NetWorkManage() { // 初始化网络 PacketGate = new PacketGate(); TcpPeerAgent = new TcpPeer(); TcpPeerAgent.OnReceivedPacket += new TcpPeer.PacketHandler(PacketGate.ProcessPacket); }
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(); } }
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) { }
private static void OnConnected(TcpPeer peer) { Console.WriteLine("Peer Connected"); _peer = peer; _peer.OnPacketReceived += OnReceived; }
public void CreateTcpPeer() { TcpPeer tcpPeer = new TcpPeer(); }
public PeerMessageHandler(TcpPeer oldpeer) { this.peer = oldpeer; }