private void AsyncReadCallback(IAsyncResult asyncResult) { SendAndReceiveContext context = (SendAndReceiveContext)asyncResult.AsyncState; try { NetworkStream stream = context.stream; int n = stream.EndRead(asyncResult); if (n > 0) { context.offset += n; context.length -= n; if (context.offset < context.buf.Length) { stream.BeginRead(context.buf, context.offset, context.length, new AsyncCallback(AsyncReadCallback), context); return; } } if (context.offset < context.buf.Length) { context.e = new HproseException("Unexpected EOF"); context.callback(asyncResult); return; } context.readCallback(asyncResult); } catch (Exception e) { context.e = e; context.callback(asyncResult); } }
private IAsyncResult AsyncSend(TcpConnEntry entry, MemoryStream data, AsyncCallback callback) { int n = (int)data.Length; int len = n > 1020 ? 2048 : n > 508 ? 1024 : 512; byte[] buf = new byte[len]; buf[0] = (byte)((n >> 24) & 0xff); buf[1] = (byte)((n >> 16) & 0xff); buf[2] = (byte)((n >> 8) & 0xff); buf[3] = (byte)(n & 0xff); int p = len - 4; SendAndReceiveContext context = new SendAndReceiveContext(); context.callback = callback; context.entry = entry; NetworkStream stream = entry.conn.stream; if (n <= p) { data.Read(buf, 4, n); return(stream.BeginWrite(buf, 0, n + 4, new AsyncCallback(WriteFullCallback), context)); } else { data.Read(buf, 4, p); context.buf = data.ToArray(); context.offset = p; context.length = n - p; return(stream.BeginWrite(buf, 0, len, new AsyncCallback(WritePartCallback), context)); } }
private void AcceptTcp() { while (true) { TcpClient client = null; SendAndReceiveContext context = new SendAndReceiveContext(); try { client = Listener.AcceptTcpClient(); client.LingerState = m_lingerState; client.NoDelay = m_noDelay; client.ReceiveBufferSize = m_receiveBufferSize; client.SendBufferSize = m_sendBufferSize; #if (dotNET10 || dotNET11) client.ReceiveTimeout = m_receiveTimeout; client.SendTimeout = m_sendTimeout; #endif NetworkStream stream = client.GetStream(); context.callback = new AsyncCallback(ErrorCallback); context.context = new HproseTcpListenerContext(client); context.stream = stream; NonBlockingHandle(context); } catch (Exception e) { if (context.context == null) { context.context = new HproseTcpListenerContext(null); } FireErrorEvent(e, context.context); CloseConnection(context); } } }
private void AcceptTcpCallback(IAsyncResult asyncResult) { TcpListener listener = asyncResult.AsyncState as TcpListener; TcpClient client = null; NetworkStream stream = null; SendAndReceiveContext context = new SendAndReceiveContext(); try { client = listener.EndAcceptTcpClient(asyncResult); listener.BeginAcceptTcpClient(new AsyncCallback(AcceptTcpCallback), listener); client.LingerState = m_lingerState; client.NoDelay = m_noDelay; client.ReceiveBufferSize = m_receiveBufferSize; client.SendBufferSize = m_sendBufferSize; client.ReceiveTimeout = m_receiveTimeout; client.SendTimeout = m_sendTimeout; stream = client.GetStream(); context.callback = new AsyncCallback(ErrorCallback); context.context = new HproseTcpListenerContext(client); context.stream = stream; NonBlockingHandle(context); } catch (Exception e) { if (context.context == null) { context.context = new HproseTcpListenerContext(null); } FireErrorEvent(e, context.context); CloseConnection(context); } }
private void NonBlockingHandle(SendAndReceiveContext context) { context.readCallback = new AsyncCallback(ReadHeadCallback); context.buf = new byte[4]; context.offset = 0; context.length = 4; context.stream.BeginRead(context.buf, context.offset, context.length, new AsyncCallback(AsyncReadCallback), context); }
private void ReadHeadCallback(IAsyncResult asyncResult) { SendAndReceiveContext context = (SendAndReceiveContext)asyncResult.AsyncState; NetworkStream stream = context.stream; byte[] buf = context.buf; int len = (int)buf[0] << 24 | (int)buf[1] << 16 | (int)buf[2] << 8 | (int)buf[3]; context.readCallback = new AsyncCallback(ReadBodyCallback); context.buf = new byte[len]; context.offset = 0; context.length = len; stream.BeginRead(context.buf, context.offset, context.length, new AsyncCallback(AsyncReadCallback), context); }
private void WriteFullCallback(IAsyncResult asyncResult) { SendAndReceiveContext context = (SendAndReceiveContext)asyncResult.AsyncState; try { NetworkStream stream = context.entry.conn.stream; stream.EndWrite(asyncResult); context.readCallback = new AsyncCallback(ReadHeadCallback); context.buf = new byte[4]; context.offset = 0; context.length = 4; stream.BeginRead(context.buf, context.offset, context.length, new AsyncCallback(AsyncReadCallback), context); } catch (Exception e) { context.e = e; context.callback(asyncResult); } }
protected override MemoryStream EndSendAndReceive(IAsyncResult asyncResult) { SendAndReceiveContext context = (SendAndReceiveContext)asyncResult.AsyncState; try { if (context.e == null) { byte[] buf = context.buf; return(new MemoryStream(buf, 0, buf.Length)); } else { context.entry.Close(); throw context.e; } } finally { pool.Free(context.entry); } }
private void CloseConnection(SendAndReceiveContext context) { try { if (context.stream != null) { context.stream.Close(); } if (context.context.Client != null) { context.context.Client.Close(); } } catch (Exception) { } }
private IAsyncResult AsyncWrite(SendAndReceiveContext context, MemoryStream data) { int n = (int)data.Length; int len = n > 1020 ? 2048 : n > 508 ? 1024 : 512; byte[] buf = new byte[len]; buf[0] = (byte)((n >> 24) & 0xff); buf[1] = (byte)((n >> 16) & 0xff); buf[2] = (byte)((n >> 8) & 0xff); buf[3] = (byte)(n & 0xff); int p = len - 4; NetworkStream stream = context.stream; if (n <= p) { data.Read(buf, 4, n); return stream.BeginWrite(buf, 0, n + 4, new AsyncCallback(WriteFullCallback), context); } else { data.Read(buf, 4, p); context.buf = data.ToArray(); context.offset = p; context.length = n - p; return stream.BeginWrite(buf, 0, len, new AsyncCallback(WritePartCallback), context); } }