protected virtual void OnClientDisconnected(object sender, Socket_EventArgs e) { if (ClientDisconnected != null) { ClientDisconnected(sender, e); } }
protected virtual void OnDataReceived(object sender, Socket_EventArgs e) { if (DataReceived != null) { DataReceived(sender, e); } }
private async Task Read() { while (shouldRead) { if (sock.Connected && sock.GetStream().CanRead) { List <byte[]> byteList = new List <byte[]>(); while (sock.GetStream().DataAvailable) { byte[] buffer = new byte[1024]; int read = await sock.GetStream().ReadAsync(buffer, 0, buffer.Length); byteList.Add(buffer); } if (byteList.Count > 0) { byte[] data = byteList.SelectMany(x => x).ToArray(); Socket_EventArgs args = new Socket_EventArgs(this); args.Data = new Socket_Data(data); OnDataReceived(this, args); byteList.Clear(); } } await Task.Delay(1000); } }
public void Send(byte[] buffer) { if (sock.Connected && sock.GetStream().CanWrite) { sock.GetStream().Write(buffer, 0, buffer.Length); sock.GetStream().Flush(); Socket_EventArgs args = new Socket_EventArgs(this); args.Data = new Socket_Data(buffer); OnDataSent(this, args); } else { if (QueueData) { } else { //throw new Exception("Socket stream not available for writing"); } } }
protected virtual void OnDataReceived(object sender, Socket_EventArgs e) { DataReceived?.Invoke(sender, e); }
protected virtual void OnDataSent(object sender, Socket_EventArgs e) { DataSent?.Invoke(sender, e); }
protected virtual void OnDisconnected(object sender, Socket_EventArgs e) { Disconnected?.Invoke(sender, e); }