public void OnConnected(Socket tcpSocket) { _ = tcpSocket ?? throw new ArgumentNullException(nameof(tcpSocket)); if (tcpSocket.Connected == false) { throw new ArgumentException(nameof(tcpSocket)); } m_Connection = new AsyncTcpConnection(tcpSocket); m_Connection.Subscribe( onReceived: HandleReceivedData, onError: error => { Log.I.Error($"패킷 송신 중 오류 발생", error); Dispose(); }, onReceiveCompleted: () => Dispose()); m_PacketProcessor = new SCPacketProcessor(this); m_PacketSender = new CSPacketSender(m_Connection); }
public ClientConnection(long id, Socket tcpSocket, SessionManager sessionManager) { _ = tcpSocket ?? throw new ArgumentNullException(nameof(tcpSocket)); if (tcpSocket.Connected == false) { throw new ArgumentException(nameof(tcpSocket)); } Id = id; m_Connection = new AsyncTcpConnection(tcpSocket); m_Connection.Subscribe( onReceived: HandleReceivedData, onError: error => { Log.I.Error($"패킷 송신 중 오류 발생", error); Dispose(); }, onReceiveCompleted: () => Dispose()); var packeHandler = new CSPacketHandler(this, sessionManager); m_PacketProcessor = new CSPacketProcessor(packeHandler); m_PacketSender = new SCPacketSender(m_Connection); }
public void AsyncTcpAcceptor() { var listenIp = IPAddress.Loopback; int listenPort = 12255; var acceptedConnections = new ConcurrentQueue <AsyncTcpConnection>(); var acceptor = new AsyncTcpAcceptor( onNewConnection: accepted => { var newConnection = new AsyncTcpConnection(accepted); newConnection.Subscribe(_ => 0, _ => { }, () => { }); acceptedConnections.Enqueue(newConnection); }); int numberOfBacklogSockets = 8; acceptor.Bind(listenIp, listenPort); acceptor.ListenAndStart(numberOfBacklogSockets); var isDone = new AutoResetEvent(false); Func <Socket, AsyncTcpConnection> tcpStreamCreator = socket => { var newStream = new AsyncTcpConnection(socket); newStream.Subscribe(_ => 0, _ => { }, () => { }); return(newStream); }; AsyncTcpConnector.Connect( ip: listenIp, port: listenPort, leftTimeoutList: new Queue <TimeSpan>(new[] { TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(100) }), onCompleted: (isConnected, socket) => { Assert.IsTrue(isConnected); Assert.IsNotNull(socket); var newStream = tcpStreamCreator(socket); Assert.IsNotNull(newStream); isDone.Set(); }); Assert.IsTrue(isDone.WaitOne(TimeSpan.FromSeconds(5))); isDone.Reset(); BecomeTrue(() => { return(acceptedConnections.Count >= 1); }, TimeSpan.FromSeconds(1)).Wait(); var connectionCount = 5 * numberOfBacklogSockets; for (int i = 0; i < connectionCount; i++) { AsyncTcpConnector.Connect( ip: listenIp, port: listenPort, leftTimeoutList: new Queue <TimeSpan>(new[] { TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(100) }), onCompleted: (isConnected, socket) => { Assert.IsTrue(isConnected); Assert.IsNotNull(socket); var newStream = tcpStreamCreator(socket); Assert.IsNotNull(newStream); isDone.Set(); }); } BecomeTrue(() => { return(acceptedConnections.Count >= connectionCount); }, TimeSpan.FromSeconds(1)).Wait(); foreach (var c in acceptedConnections) { c.Dispose(); } }