Exemple #1
0
        /// <summary>
        /// Reads the data from the client.
        /// </summary>
        /// <param name="cancellation">Cancellation token</param>
        /// <returns>A <see cref="Task"/> that represents an asynchronous operation.</returns>
        private async Task ReadData(CancellationToken cancellation)
        {
            int count;

            while ((count = await ReadDataSafe(CancellationToken.None)) > 0)
            {
                if (cancellation.IsCancellationRequested)
                {
                    break;
                }

                LogRawRequest(buffer, count);

                data.Append(buffer, 0, count);

                await OnData(data, CancellationToken.None);

                // Done processing the request, start listening again
                Logger.WriteDebug(TraceResources.AccumulatedDataProcessed);
            }

            Logger.WriteInfo(TraceResources.ConnectionClosingOnReceiveFormat, count);

            CloseDataStream();
        }
        /// <summary>
        /// Generates binary data that represents the request.
        /// </summary>
        /// <param name="code">Status code</param>
        /// <param name="message">Response text message</param>
        /// <param name="items">Items to send</param>
        /// <param name="lineFeed">Line termination sequence</param>
        /// <returns>Binary data that represents the request.</returns>
        private static byte[] GetData(int code, string message, IEnumerable <string> items, byte[] lineFeed)
        {
            var response = new ArrayBuffer();

            response.Append(Encoding.ASCII.GetBytes(code + "-" + message));
            response.Append(lineFeed);

            foreach (var item in items)
            {
                response.Append(Encoding.ASCII.GetBytes(ListIndentation));
                response.Append(Encoding.ASCII.GetBytes(item));
                response.Append(lineFeed);
            }

            response.Append(Encoding.ASCII.GetBytes(code + " End"));

            return(response.GetBuffer());
        }