/// <inheritdoc/>
 public void Dispose()
 {
     if (!_closed)
     {
         Close();
     }
     if (!ReferenceEquals(SocketStream, OriginalStream))
     {
         SocketStream.Dispose();
         SocketStream = OriginalStream;
     }
     _socket.Dispose();
     _cancellationTokenSource.Dispose();
     Data.Dispose();
 }
Beispiel #2
0
        public async Task Stop()
        {
            if (IsOpen && _tcpSocketClient != null)
            {
                await _tcpSocketClient.DisconnectAsync().ConfigureAwait(false);

                _tcpSocketClient.Dispose();
            }
            IsOpen = false;
        }
Beispiel #3
0
    /// <summary>
    /// Continuously read strings from the socketclient and yield them as <code>Message</code> instances.
    /// Stops when <code>eof</code> is hit. Messages are delimited by <code>eom</code>.
    /// </summary>
    /// <param name="client"></param>
    /// <param name="cancellationToken"></param>
    /// <param name="eom"></param>
    /// <param name="eof"></param>
    /// <returns></returns>
    public static IEnumerable <Message> ReadStrings(this ITcpSocketClient client, CancellationToken cancellationToken, string eom = "<EOM>", string eof = "<EOF>")
    {
        var from = String.Format("{0}:{1}", client.RemoteAddress, client.RemotePort);

        var  currData = "";
        int  bytesRec = 0;
        bool gotEof   = false;

        while (bytesRec != -1 && !cancellationToken.IsCancellationRequested && !gotEof)
        {
            var buffer = new byte[1024];
            bytesRec  = client.ReadStream.Read(buffer, 0, buffer.Length);
            currData += Encoding.UTF8.GetString(buffer, 0, bytesRec);

            // Hit an EOM - we have a full message in currData;
            if (currData.IndexOf(eom, StringComparison.Ordinal) > -1)
            {
                var msg = new Message
                {
                    Text       = currData.Substring(0, currData.IndexOf(eom)),
                    DetailText = String.Format("<Received from {0} at {1}>", from, DateTime.Now.ToString("HH:mm:ss"))
                };

                yield return(msg);

                currData = currData.Substring(currData.IndexOf(eom) + eom.Length);
            }

            // Hit an EOF - client is gracefully disconnecting
            if (currData.IndexOf(eof, StringComparison.Ordinal) > -1)
            {
                var msg = new Message
                {
                    DetailText = String.Format("<{0} disconnected at {1}>", from, DateTime.Now.ToString("HH:mm:ss"))
                };

                yield return(msg);

                gotEof = true;
            }
        }

        // if we get here, either the stream broke, the cancellation token was cancelled, or the eof message was received
        // time to drop the client.

        try
        {
            client.DisconnectAsync().Wait();
            client.Dispose();
        }
        catch { }
    }
Beispiel #4
0
        private async void ProcessRequestAsync(ITcpSocketClient socket)
        {
            // this works for text only
            StringBuilder request = new StringBuilder();
            byte[] data = new byte[BufferSize];
            int byteread = BufferSize;
            bool error = false;
            while (byteread == BufferSize)
            {
                try
                {
                    byteread = await socket.ReadStream.ReadAsync(data, 0, BufferSize);
                    request.Append(Encoding.UTF8.GetString(data, 0, byteread));
                }
                catch (Exception ex)
                {
                    _2BDebug.Output(ex);
                    error = true;
                    break;
                }
            }

            if (!error && request.ToString().Length > 0)
            { 
                string requestMethod = request.ToString().Split('\n')[0];
                string[] requestParts = requestMethod.Split(' ');
                string requestPart = string.Empty;
                Debug.WriteLine(requestMethod);
                if (requestParts[0] == "GET" && requestParts.Count() > 1)
                {
                    requestPart = requestParts[1];
                }
                if (requestPart != string.Empty)
                    await WriteResponseAsync(requestPart, socket);
            }
            try
            {
                socket.Dispose();
            }
            catch (Exception ex)
            {
                _2BDebug.Output(ex);
            }
        }
Beispiel #5
0
 public void Dispose()
 {
     _tcpSocketClient.Dispose();
     _websocketListener.StopReceivingData();
 }