Exemple #1
0
 protected virtual void OnClientDisconnected(object sender, Socket_EventArgs e)
 {
     if (ClientDisconnected != null)
     {
         ClientDisconnected(sender, e);
     }
 }
Exemple #2
0
 protected virtual void OnDataReceived(object sender, Socket_EventArgs e)
 {
     if (DataReceived != null)
     {
         DataReceived(sender, e);
     }
 }
Exemple #3
0
        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);
            }
        }
Exemple #4
0
        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");
                }
            }
        }
Exemple #5
0
 protected virtual void OnDataReceived(object sender, Socket_EventArgs e)
 {
     DataReceived?.Invoke(sender, e);
 }
Exemple #6
0
 protected virtual void OnDataSent(object sender, Socket_EventArgs e)
 {
     DataSent?.Invoke(sender, e);
 }
Exemple #7
0
 protected virtual void OnDisconnected(object sender, Socket_EventArgs e)
 {
     Disconnected?.Invoke(sender, e);
 }