public void Initialize(bool isServer = false) { NetCommonIO netIO = new NetCommonIO(); netIO.Initialize(SelectorType.IOCP, 500); INetSelector selector = netIO.Selector(); if (null != selector) { selector.RegAcceptedDelegate(PrintAcceptLog); selector.RegConnectedDelegate(PrintConnectedLog); selector.RegSentDelegate(PrintSentLog); selector.RegReceivedDelegate(PrintReceiveLog); selector.RegDisconnectedDelegate(PrintDisconnectedLog); selector.Start(); if (!isServer) { //client List <ClientThreadT> client_threads = new List <ClientThreadT>(); Random rand = new Random(Guid.NewGuid().GetHashCode()); for (int i = 0; i < 100; ++i) { SelItem clientItem = new SelItem(); AsyncSocket clientSocket = AsyncSocket.CreateIPv4Tcp(); selector.AssociateSocket(clientSocket, clientItem); clientSocket.Bind(IPAddress.Any, 0); Console.WriteLine($"Client ip:{NetUtil.ToIportString(clientSocket.LocalEndPoint)}"); int name = rand.Next(); ClientThreadT thread = new ClientThreadT(clientSocket, clientItem, name.ToString()); client_threads.Add(thread); } Console.WriteLine("Wait client threads to be end ..."); bool finished = false; while (!finished) { finished = true; int count = 0; foreach (var thread in client_threads) { ++count; if (!thread.finished()) { finished = false; Thread.Sleep(50); break; } } if (finished) { break; } } Console.WriteLine("Client threads all finished."); selector.Stop(); } else { //server try { SelItem accept_item = new SelItem(); AsyncSocket listener = AsyncSocket.CreateIPv4Tcp(); selector.AssociateSocket(listener, accept_item); listener.Bind(IPAddress.Any, 56665); listener.Listen(int.MaxValue); Console.WriteLine("Server started :"); //begin to accept while (true) { listener.Accept(); accept_item.Handle.WaitOne(); var serverSocket = listener.GetAcceptedSocket(); SelItem server_item = new SelItem(); selector.AssociateSocket(serverSocket, server_item); NetData.ReceiveData(serverSocket); } } catch (Exception e) { Console.WriteLine(e.Message); } } } }
/// <summary> /// This is called when socket input has been completed. /// </summary> /// <param name="socketError">This indicates the status of the input operation - whether Success or some error.</param> /// <param name="bytesTransferred">the number of bytes that were transferred</param> /// <exception cref="NetMQException">A non-recoverable socket-error occurred.</exception> public void InCompleted(SocketError socketError, int bytesTransferred) { switch (socketError) { case SocketError.Success: { // TODO: check TcpFilters var acceptedSocket = m_handle.GetAcceptedSocket(); acceptedSocket.NoDelay = true; if (m_options.TcpKeepalive != -1) { acceptedSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, m_options.TcpKeepalive); if (m_options.TcpKeepaliveIdle != -1 && m_options.TcpKeepaliveIntvl != -1) { var bytes = new ByteArraySegment(new byte[12]); Endianness endian = BitConverter.IsLittleEndian ? Endianness.Little : Endianness.Big; bytes.PutInteger(endian, m_options.TcpKeepalive, 0); bytes.PutInteger(endian, m_options.TcpKeepaliveIdle, 4); bytes.PutInteger(endian, m_options.TcpKeepaliveIntvl, 8); acceptedSocket.IOControl(IOControlCode.KeepAliveValues, (byte[])bytes, null); } } // Create the engine object for this connection. var engine = new StreamEngine(acceptedSocket, m_options, m_endpoint); // Choose I/O thread to run connector in. Given that we are already // running in an I/O thread, there must be at least one available. IOThread ioThread = ChooseIOThread(m_options.Affinity); // Create and launch a session object. // TODO: send null in address parameter, is unneeded in this case SessionBase session = SessionBase.Create(ioThread, false, m_socket, m_options, new Address(m_handle.LocalEndPoint)); session.IncSeqnum(); LaunchChild(session); SendAttach(session, engine, false); m_socket.EventAccepted(m_endpoint, acceptedSocket); Accept(); break; } case SocketError.ConnectionReset: case SocketError.NoBufferSpaceAvailable: case SocketError.TooManyOpenSockets: { m_socket.EventAcceptFailed(m_endpoint, socketError.ToErrorCode()); Accept(); break; } default: { NetMQException exception = NetMQException.Create(socketError); m_socket.EventAcceptFailed(m_endpoint, exception.ErrorCode); throw exception; } } }
public void SendReceive() { CompletionPort completionPort = CompletionPort.Create(); bool exception = false; var task = Task.Factory.StartNew(() => { bool cancel = false; while (!cancel) { CompletionStatus [] completionStatuses = new CompletionStatus[10]; int removed; completionPort.GetMultipleQueuedCompletionStatus(-1, completionStatuses, out removed); for (int i = 0; i < removed; i++) { if (completionStatuses[i].OperationType == OperationType.Signal) { cancel = true; } else if (completionStatuses[i].SocketError == SocketError.Success) { EventWaitHandle manualResetEvent = (EventWaitHandle)completionStatuses[i].State; manualResetEvent.Set(); } else { exception = true; } } } }); AutoResetEvent clientEvent = new AutoResetEvent(false); AutoResetEvent acceptedEvent = new AutoResetEvent(false); AsyncSocket listener = AsyncSocket.CreateIPv4Tcp(); completionPort.AssociateSocket(listener, acceptedEvent); listener.Bind(IPAddress.Any, 0); int port = listener.LocalEndPoint.Port; listener.Listen(1); listener.Accept(); AsyncSocket clientSocket = AsyncSocket.CreateIPv4Tcp(); completionPort.AssociateSocket(clientSocket, clientEvent); clientSocket.Bind(IPAddress.Any, 0); clientSocket.Connect("localhost", port); clientEvent.WaitOne(); acceptedEvent.WaitOne(); var serverSocket = listener.GetAcceptedSocket(); AutoResetEvent serverEvent = new AutoResetEvent(false); completionPort.AssociateSocket(serverSocket, serverEvent); byte[] recv = new byte[1]; serverSocket.Receive(recv); byte[] data = new[] { (byte)1 }; clientSocket.Send(data); clientEvent.WaitOne(); // wait for data to be send serverEvent.WaitOne(); // wait for data to be received Assert.AreEqual(1, recv[0]); completionPort.Signal(null); task.Wait(); Assert.IsFalse(exception); completionPort.Dispose(); listener.Dispose(); serverSocket.Dispose(); clientSocket.Dispose(); }