protected override void SocketEventArgsCompleted(object sender, SocketAsyncEventArgs e) { if (e.LastOperation == SocketAsyncOperation.Connect) { ProcessConnect(sender as Socket, null, e); return; } ProcessReceive(e); }
protected override void OnGetSocket(SocketAsyncEventArgs e) { if (Buffer.Array == null) Buffer = new ArraySegment<byte>(new byte[ReceiveBufferSize], 0, ReceiveBufferSize); e.SetBuffer(Buffer.Array, Buffer.Offset, Buffer.Count); if (m_SocketEventArgs != null) { m_SocketEventArgs.Dispose(); } m_SocketEventArgs = e; OnConnected(); StartReceive(); }
private void ProcessReceive(SocketAsyncEventArgs e) { if (e.SocketError != SocketError.Success) { if (EnsureSocketClosed()) OnClosed(); if (!IsIgnorableSocketError((int)e.SocketError)) OnError(new SocketException((int)e.SocketError)); return; } if (e.BytesTransferred == 0) { if (EnsureSocketClosed()) OnClosed(); return; } OnDataReceived(e.Buffer, e.Offset, e.BytesTransferred); StartReceive(); }
void Sending_Completed(object sender, SocketAsyncEventArgs e) { if (e.SocketError != SocketError.Success || e.BytesTransferred == 0) { if (EnsureSocketClosed()) OnClosed(); if (e.SocketError != SocketError.Success && !IsIgnorableSocketError((int)e.SocketError)) OnError(new SocketException((int)e.SocketError)); return; } OnSendingCompleted(); }
protected override void SendInternal(PosList<ArraySegment<byte>> items) { if (m_SocketEventArgsSend == null) { m_SocketEventArgsSend = new SocketAsyncEventArgs(); m_SocketEventArgsSend.Completed += new EventHandler<SocketAsyncEventArgs>(Sending_Completed); } bool raiseEvent; try { if (items.Count > 1) { if (m_SocketEventArgsSend.Buffer != null) m_SocketEventArgsSend.SetBuffer(null, 0, 0); m_SocketEventArgsSend.BufferList = items; } else { var currentItem = items[0]; try { if (m_SocketEventArgsSend.BufferList != null) m_SocketEventArgsSend.BufferList = null; } catch//a strange NullReference exception { } m_SocketEventArgsSend.SetBuffer(currentItem.Array, 0, currentItem.Count); } raiseEvent = Client.SendAsync(m_SocketEventArgsSend); } catch (SocketException exc) { if (EnsureSocketClosed() && !IsIgnorableSocketError(exc.ErrorCode)) OnError(exc); return; } catch (Exception e) { if (EnsureSocketClosed() && IsIgnorableException(e)) OnError(e); return; } if (!raiseEvent) Sending_Completed(Client, m_SocketEventArgsSend); }