private void OnAcceptComplete(Exception ex = null, IAsyncTcpConnection result = null) { if (this.AcceptCompleted != null) { this.AcceptCompleted(ex == null ? new AsyncResultEventArgs <IAsyncTcpConnection>(result) : new AsyncResultEventArgs <IAsyncTcpConnection>(ex)); } }
/// <overloads> /// <summary>Sends a packet to a socket.</summary> /// <remarks> /// <para>Generates a length prefix for the packet and writes the length prefix and packet to the socket.</para> /// </remarks> /// </overloads> /// <summary>Sends a packet to a socket.</summary> /// <remarks> /// <para>Generates a length prefix for the packet and writes the length prefix and packet to the socket.</para> /// </remarks> /// <param name="socket">The socket used for communication.</param> /// <param name="packet">The packet to send.</param> /// <param name="state">The user-defined state that is passed to WriteCompleted. May be null.</param> public static void WritePacketAsync(IAsyncTcpConnection socket, byte[] packet, object state) { // Get the length prefix for the message byte[] lengthPrefix = BitConverter.GetBytes(packet.Length); // We use the special CallbackOnErrorsOnly object to tell the socket we don't want // WriteCompleted to be invoked (it would confuse socket users if they see WriteCompleted // events for writes they never started). socket.WriteAsync(lengthPrefix, new CallbackOnErrorsOnly()); // Send the actual message, this time enabling the normal callback. socket.WriteAsync(packet, state); }
/// <inheritdoc cref="WritePacketAsync(IAsyncTcpConnection, byte[], object)" /> /// <param name="socket">The socket used for communication.</param> /// <param name="packet">The packet to send.</param> public static void WritePacketAsync(IAsyncTcpConnection socket, byte[] packet) { WritePacketAsync(socket, packet, null); }
/// <summary> /// Sends a keepalive (0-length) packet to the socket. /// </summary> /// <param name="socket">The socket used for communication.</param> public static void WriteKeepaliveAsync(IAsyncTcpConnection socket) { // We use CallbackOnErrorsOnly to indicate that the WriteCompleted callback should only be // called if there was an error. socket.WriteAsync(BitConverter.GetBytes((int)0), new CallbackOnErrorsOnly()); }
/// <summary> /// Initializes a new instance of the <see cref="SocketPacketProtocol"/> class bound to a given socket connection. /// </summary> /// <param name="socket">The socket used for communication.</param> public SocketPacketProtocol(IAsyncTcpConnection socket) { this.Socket = socket; this.lengthBuffer = new byte[sizeof(int)]; }
/// <summary> /// Initiates a write operation. /// </summary> /// <param name="connection">The connection to which to write.</param> /// <param name="buffers">The buffers containing the data to write to the socket.</param> /// <remarks> /// <para>Multiple write operations may be active at the same time.</para> /// <para>The write operation will complete by invoking <see cref="IAsyncTcpConnection.WriteCompleted"/>, unless the socket is shut down (<see cref="IAsyncTcpConnection.ShutdownAsync"/>), closed (<see cref="InterfaceExtensions.Close(IAsyncTcpConnection)"/>), or abortively closed (<see cref="InterfaceExtensions.AbortiveClose"/>).</para> /// <para>Write operations are never cancelled.</para> /// </remarks> public static void WriteAsync(this IAsyncTcpConnection connection, params ArraySegment <byte>[] buffers) { connection.WriteAsync(buffers, null); }
/// <summary> /// Initiates a write operation. /// </summary> /// <remarks> /// <para>Multiple write operations may be active at the same time.</para> /// <para>The write operation will complete by invoking <see cref="IAsyncTcpConnection.WriteCompleted"/>, unless the socket is shut down (<see cref="IAsyncTcpConnection.ShutdownAsync"/>), closed (<see cref="Close(IAsyncTcpConnection)"/>), or abortively closed (<see cref="AbortiveClose"/>).</para> /// <para>Write operations are never cancelled.</para> /// <para>If <paramref name="state"/> is an instance of <see cref="CallbackOnErrorsOnly"/>, then <see cref="IAsyncTcpConnection.WriteCompleted"/> is only invoked in an error situation; it is not invoked if the write completes successfully.</para> /// </remarks> /// <param name="connection">The connection to which to write.</param> /// <param name="buffer">The data to write to the socket.</param> /// <param name="state">The context, which is passed to <see cref="IAsyncTcpConnection.WriteCompleted"/> as <c>e.UserState</c>.</param> public static void WriteAsync(this IAsyncTcpConnection connection, byte[] buffer, object state) { connection.WriteAsync(buffer, 0, buffer.Length, state); }
/// <summary> /// Initiates a write operation. /// </summary> /// <remarks> /// <para>Multiple write operations may be active at the same time.</para> /// <para>The write operation will complete by invoking <see cref="IAsyncTcpConnection.WriteCompleted"/>, unless the socket is shut down (<see cref="IAsyncTcpConnection.ShutdownAsync"/>), closed (<see cref="InterfaceExtensions.Close(IAsyncTcpConnection)"/>), or abortively closed (<see cref="InterfaceExtensions.AbortiveClose"/>).</para> /// <para>Write operations are never cancelled.</para> /// </remarks> /// <param name="connection">The connection to which to write.</param> /// <param name="buffer">The buffer containing the data to write to the socket.</param> /// <param name="offset">The offset of the data within <paramref name="buffer"/>.</param> /// <param name="size">The number of bytes of data, at <paramref name="offset"/> within <paramref name="buffer"/>.</param> public static void WriteAsync(this IAsyncTcpConnection connection, byte[] buffer, int offset, int size) { connection.WriteAsync(buffer, offset, size, null); }
/// <summary> /// Initiates a write operation. /// </summary> /// <remarks> /// <para>Multiple write operations may be active at the same time.</para> /// <para>The write operation will complete by invoking <see cref="IAsyncTcpConnection.WriteCompleted"/>, unless the socket is shut down (<see cref="IAsyncTcpConnection.ShutdownAsync"/>), closed (<see cref="Close(IAsyncTcpConnection)"/>), or abortively closed (<see cref="AbortiveClose"/>).</para> /// <para>Write operations are never cancelled.</para> /// </remarks> /// <param name="connection">The connection to which to write.</param> /// <param name="buffer">The data to write to the socket.</param> public static void WriteAsync(this IAsyncTcpConnection connection, byte[] buffer) { connection.WriteAsync(buffer, 0, buffer.Length, null); }
/// <summary> /// Abortively closes the socket. Once this method is called, no operations will complete. /// </summary> /// <remarks> /// <para>This method provides the fastest way to reclaim socket resources; however, its use is not generally recommended; <see cref="Close(IAsyncTcpConnection)"/> should usually be used instead of this method.</para> /// </remarks> public static void AbortiveClose(this IAsyncTcpConnection connection) { connection.LingerState = new LingerOption(true, 0); connection.Dispose(); }
/// <summary> /// Gracefully or abortively closes the socket. Once this method is called, no operations will complete. /// </summary> /// <remarks> /// <para>This method performs a graceful shutdown of the underlying socket; however, this is performed in the background, so the application never receives notification of its completion. <see cref="IAsyncTcpConnection.ShutdownAsync"/> performs a graceful shutdown with completion.</para> /// <para>Note that exiting the process after calling this method but before the background shutdown completes will result in an abortive close.</para> /// <para><see cref="IAsyncTcpConnection.LingerState"/> will determine whether this method will perform a graceful or abortive close.</para> /// </remarks> public static void Close(this IAsyncTcpConnection connection) { connection.Dispose(); }
/// <summary> /// Initializes a new instance of the <see cref="SocketPacketProtocol"/> class bound to a given socket connection. /// </summary> /// <param name="socket">The socket used for communication.</param> public SocketPacketProtocol(IAsyncTcpConnection socket) { Socket = socket; lengthBuffer = new byte[sizeof(int)]; }
/// <summary> /// Initiates a write operation. /// </summary> /// <param name="connection">The connection to which to write.</param> /// <param name="buffers">The buffers containing the data to write to the socket.</param> /// <remarks> /// <para>Multiple write operations may be active at the same time.</para> /// <para>The write operation will complete by invoking <see cref="IAsyncTcpConnection.WriteCompleted"/>, unless the socket is shut down (<see cref="IAsyncTcpConnection.ShutdownAsync"/>), closed (<see cref="InterfaceExtensions.Close(IAsyncTcpConnection)"/>), or abortively closed (<see cref="InterfaceExtensions.AbortiveClose"/>).</para> /// <para>Write operations are never cancelled.</para> /// </remarks> public static void WriteAsync(this IAsyncTcpConnection connection, IList <ArraySegment <byte> > buffers) { connection.WriteAsync(buffers, null); }
/// <summary> /// Initializes a new instance of the <see cref="ModbusPacketProtocols"/> class bound to a given socket connection. /// </summary> /// <param name="socket">The socket used for communication.</param> public ModbusPacketProtocols(IAsyncTcpConnection socket) { Socket = socket; // this.lengthBuffer = new byte[sizeof(int)]; }