public void InCompleted(SocketError socketError, int bytesTransferred) { if (socketError != SocketError.Success) { if (socketError == SocketError.ConnectionReset || socketError == SocketError.NoBufferSpaceAvailable || socketError == SocketError.TooManyOpenSockets) { m_socket.EventAcceptFailed(m_endpoint, ErrorHelper.SocketErrorToErrorCode(socketError)); Accept(); } else { m_acceptedSocket.Dispose(); NetMQException exception = NetMQException.Create(socketError); m_socket.EventAcceptFailed(m_endpoint, exception.ErrorCode); throw exception; } } else { // TODO: check TcpFilters m_acceptedSocket.NoDelay = true; // Utils.TuneTcpKeepalives(m_acceptedSocket, m_options.TcpKeepalive, m_options.TcpKeepaliveCnt, m_options.TcpKeepaliveIdle, m_options.TcpKeepaliveIntvl); // Create the engine object for this connection. StreamEngine engine = new StreamEngine(m_acceptedSocket, m_options, m_endpoint); // Choose I/O thread to run connecter 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 unneed 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, m_acceptedSocket); Accept(); } }
// Close the listening socket. private void Close() { if (m_handle == null) { return; } try { m_handle.Dispose(); m_socket.EventClosed(m_endpoint, m_handle); } catch (SocketException ex) { m_socket.EventCloseFailed(m_endpoint, ErrorHelper.SocketErrorToErrorCode(ex.SocketErrorCode)); } if (m_acceptedSocket != null) { try { m_acceptedSocket.Dispose(); } catch (SocketException) { } } m_acceptedSocket = null; m_handle = null; }
public void Destroy() { if (m_handle != null) { try { m_handle.Dispose(); } catch (SocketException) {} m_handle = null; } }
// Close the connecting socket. private void Close() { Debug.Assert(m_s != null); try { m_s.Dispose(); m_socket.EventClosed(m_endpoint, m_s); m_s = null; } catch (SocketException ex) { m_socket.EventCloseFailed(m_endpoint, ErrorHelper.SocketErrorToErrorCode(ex.SocketErrorCode)); } }
public void Destroy() { Debug.Assert(!m_plugged); if (m_handle != null) { try { m_handle.Dispose(); } catch (SocketException) {} m_handle = null; } }
/// <summary> /// Close the listening socket. /// </summary> private void Close() { if (m_handle == null) { return; } try { m_handle.Dispose(); m_socket.EventClosed(m_endpoint, m_handle); } catch (SocketException ex) { m_socket.EventCloseFailed(m_endpoint, ex.SocketErrorCode.ToErrorCode()); } m_handle = null; }
private void Close() { if (m_handle == null) { return; } try { m_handle.Dispose(); m_socket.EventClosed(m_address.ToString(), m_handle); } catch (SocketException ex) { m_socket.EventCloseFailed(m_address.ToString(), ErrorHelper.SocketErrorToErrorCode(ex.SocketErrorCode)); } catch (NetMQException ex) { m_socket.EventCloseFailed(m_address.ToString(), ex.ErrorCode); } m_handle = null; }
public void InCompleted(SocketError socketError, int bytesTransferred) { if (socketError != SocketError.Success) { if (socketError == SocketError.ConnectionReset || socketError == SocketError.NoBufferSpaceAvailable || socketError == SocketError.TooManyOpenSockets) { m_socket.EventAcceptFailed(m_endpoint, ErrorHelper.SocketErrorToErrorCode(socketError)); Accept(); } else { m_acceptedSocket.Dispose(); NetMQException exception = NetMQException.Create(socketError); m_socket.EventAcceptFailed(m_endpoint, exception.ErrorCode); throw exception; } } else { // TODO: check TcpFilters m_acceptedSocket.NoDelay = true; if (m_options.TcpKeepalive != -1) { m_acceptedSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, m_options.TcpKeepalive); if (m_options.TcpKeepaliveIdle != -1 && m_options.TcpKeepaliveIntvl != -1) { ByteArraySegment 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); m_acceptedSocket.IOControl(IOControlCode.KeepAliveValues, (byte[])bytes, null); } } // Create the engine object for this connection. StreamEngine engine = new StreamEngine(m_acceptedSocket, m_options, m_endpoint); // Choose I/O thread to run connecter 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 unneed 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, m_acceptedSocket); Accept(); } }
public static void Main(string[] args) { CompletionPort completionPort = CompletionPort.Create(); AutoResetEvent listenerEvent = new AutoResetEvent(false); AutoResetEvent clientEvent = new AutoResetEvent(false); AutoResetEvent serverEvent = new AutoResetEvent(false); AsyncSocket listener = AsyncSocket.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); completionPort.AssociateSocket(listener, listenerEvent); AsyncSocket server = AsyncSocket.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); completionPort.AssociateSocket(server, serverEvent); AsyncSocket client = AsyncSocket.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); completionPort.AssociateSocket(client, clientEvent); Task.Factory.StartNew(() => { CompletionStatus completionStatus; while (true) { var result = completionPort.GetQueuedCompletionStatus(-1, out completionStatus); if (result) { Console.WriteLine("{0} {1} {2}", completionStatus.SocketError, completionStatus.OperationType, completionStatus.BytesTransferred); if (completionStatus.State != null) { AutoResetEvent resetEvent = (AutoResetEvent)completionStatus.State; resetEvent.Set(); } } } }); listener.Bind(IPAddress.Any, 5555); listener.Listen(1); client.Connect("localhost", 5555); listener.Accept(server); listenerEvent.WaitOne(); clientEvent.WaitOne(); byte[] sendBuffer = new byte[1] { 2 }; byte[] recvBuffer = new byte[1]; client.Send(sendBuffer); server.Receive(recvBuffer); clientEvent.WaitOne(); serverEvent.WaitOne(); server.Dispose(); client.Dispose(); Console.ReadLine(); }
/// <summary> /// Close. /// </summary> public void Dispose() { socket.Dispose(); }
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(); }