public void Send(byte[] data) { try { TcpState state = new TcpState(this.tcpClient); state.Stream.BeginWrite(data, 0, data.Length, new AsyncCallback(sendCallback), state); } catch (Exception ex) { this.Error?.Invoke(this, new DyEventArgs() { Message = ex.Message + "send" }); } }
private void sendCallback(IAsyncResult ar) { TcpState state = (TcpState)ar.AsyncState; try { state.Stream.EndWrite(ar); } catch (Exception ex) { this.Error?.Invoke(this, new DyEventArgs() { Message = ex.Message + " sendCallback" }); } }
private void receiveCallback(IAsyncResult ar) { TcpState state = (TcpState)ar.AsyncState; int readbytes = 0; try { readbytes = state.Stream.EndRead(ar); } catch { readbytes = 0; } if (readbytes == 0) { this.Error?.Invoke($"Client {state.ID} is already disconnect!"); this.clientDict.Remove(state.ID); return; } byte[] buf = new byte[readbytes]; Array.Copy(state.Buffer, 0, buf, 0, readbytes); state.Stream.BeginRead(state.Buffer, 0, state.Buffer.Length, new AsyncCallback(receiveCallback), state); this.Received?.Invoke(buf); }
private void sendCallback(IAsyncResult ar) { TcpState state = (TcpState)ar.AsyncState; state.Stream.EndWrite(ar); }
public void Send(byte[] data, string remote) { TcpState state = this.clientDict[remote]; state.Stream.BeginWrite(data, 0, data.Length, new AsyncCallback(sendCallback), state); }