private void ListenerThread()

in Nodejs/Product/Nodejs/JsonListener.cs [30:137]


        private void ListenerThread()
        {
            var pos = 0;
            var text = Array.Empty<byte>();

            // Use a local for Socket to keep nulling of _socket field (on non listener thread)
            // from causing spurious null dereferences
            var socket = this.Socket;

            try
            {
                if (socket != null && socket.Connected)
                {
                    // _socket == null || !_socket.Connected effectively stops listening and associated packet processing
                    while (this.Socket != null && socket.Connected)
                    {
                        if (pos >= text.Length)
                        {
                            ReadMoreData(socket.Receive(this.socketBuffer), ref text, ref pos);
                        }

                        var headers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
                        while (this.Socket != null && socket.Connected)
                        {
                            var newPos = text.FirstNewLine(pos);
                            if (newPos == pos)
                            {
                                // double \r\n, we're done with headers.
                                pos += 2;
                                break;
                            }
                            else if (newPos == -1)
                            {
                                // we need to get more data...
                                ReadMoreData(socket.Receive(this.socketBuffer), ref text, ref pos);
                            }
                            else
                            {
                                // continue onto next header
                                // save header, continue to the next one.
                                var nameEnd = text.IndexOf((byte)':', pos, newPos - pos);
                                if (nameEnd != -1)
                                {
                                    var headerName = text.Substring(pos, nameEnd - pos);
                                    var headerNameStr = Encoding.UTF8.GetString(headerName).Trim();

                                    var headerValue = text.Substring(nameEnd + 1, newPos - nameEnd - 1);
                                    var headerValueStr = Encoding.UTF8.GetString(headerValue).Trim();
                                    headers[headerNameStr] = headerValueStr;
                                }
                                pos = newPos + 2;
                            }
                        }

                        var body = string.Empty;
                        if (headers.TryGetValue("Content-Length", out var contentLen))
                        {
                            var lengthRemaining = int.Parse(contentLen, CultureInfo.InvariantCulture);
                            if (lengthRemaining != 0)
                            {
                                var bodyBuilder = new StringBuilder();

                                while (this.Socket != null && socket.Connected)
                                {
                                    var len = Math.Min(text.Length - pos, lengthRemaining);
                                    bodyBuilder.Append(Encoding.UTF8.GetString(text.Substring(pos, len)));
                                    pos += len;

                                    lengthRemaining -= len;

                                    if (lengthRemaining == 0)
                                    {
                                        break;
                                    }

                                    ReadMoreData(socket.Receive(this.socketBuffer), ref text, ref pos);
                                }
                                body = bodyBuilder.ToString();
                            }
                        }

                        if (this.Socket != null && socket.Connected)
                        {
                            try
                            {
                                ProcessPacket(new JsonResponse(headers, body));
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine("Error: {0}", e);
                            }
                        }
                    }
                }
            }
            catch (SocketException)
            {
            }
            finally
            {
                Debug.Assert(this.Socket == null || !this.Socket.Connected);
                if (socket != null && socket.Connected)
                {
                    socket.Disconnect(false);
                }
                OnSocketDisconnected();
            }
        }