private static void ReceiveCompleted(SocketAsyncEventArgs e) { SocketAsyncEventArgsX ex = (SocketAsyncEventArgsX)e; ISession session = ex.Session; AsyncTcpClient tcpclient = (AsyncTcpClient)e.UserToken; try { if (e.SocketError == SocketError.Success && e.BytesTransferred > 0) { System.Threading.Interlocked.Increment(ref tcpclient.mReceiveQuantity); tcpclient.mReceivBytes += e.BytesTransferred; ex.BufferX.Postion = 0; ex.BufferX.SetLength(e.BytesTransferred); tcpclient.OnReceive(ex.BufferX); tcpclient.BeginReceive(); } else { ex.BufferX.Free(); tcpclient.DisConnect(); tcpclient.mLastError = new SocketException((int)e.SocketError); } if (tcpclient.SocketProcessHandler != null) { tcpclient.SocketProcessHandler.ReceiveCompleted(tcpclient, e); } } catch (Exception e_) { tcpclient.ProcessError(e_, "client network receive error!"); } }
private static void SendCompleted(SocketAsyncEventArgs e) { SocketAsyncEventArgsX ex = (SocketAsyncEventArgsX)e; Buffers.Buffer buffer = (Buffers.Buffer)ex.BufferX; AsyncTcpClient tcpclient = (AsyncTcpClient)e.UserToken; try { if (e.SocketError == SocketError.IOPending || e.SocketError == SocketError.Success) { System.Threading.Interlocked.Increment(ref tcpclient.mSendQuantity); tcpclient.mSendBytes += e.BytesTransferred; if (e.BytesTransferred < e.Count) { buffer.Postion = buffer.Postion + e.BytesTransferred; buffer.SetLength(buffer.Length - e.BytesTransferred); buffer.AsyncTo(tcpclient.mSendEventArgs, tcpclient.Socket); } else { IBuffer nextbuf = buffer.Next; try { buffer.Completed?.Invoke(buffer); } catch { } buffer.Free(); if (nextbuf != null) { tcpclient.CommitBuffer(nextbuf); } else { tcpclient.SendCompleted(); } } } else { Buffers.Buffer.Free(ex.BufferX); tcpclient.DisConnect(); tcpclient.ProcessError(new SocketException((int)e.SocketError), $"Send error {e.SocketError}"); } if (tcpclient.SocketProcessHandler != null) { tcpclient.SocketProcessHandler.SendCompleted(tcpclient, e); } } catch (Exception e_) { tcpclient.ProcessError(e_, "client network send error!"); } }
private static void IO_Completed(object sender, SocketAsyncEventArgs e) { Buffers.SocketAsyncEventArgsX ex = (Buffers.SocketAsyncEventArgsX)e; AsyncTcpClient tcpclient = (AsyncTcpClient)e.UserToken; if (ex.IsReceive) { ReceiveCompleted(e); } else { SendCompleted(e); } }
public void Initialize(Benchmark benchmark) { mClient = BeetleX.SocketFactory.CreateClient <BeetleX.Clients.AsyncTcpClient>("192.168.2.19", 9012); }
public void To(Clients.AsyncTcpClient client) { client.Send(this); }