void ProcessInfo(Guid loginToken, HttpContextAdapter adapter) { //Construct a response. WebMessage message = new WebMessage("webServerInfo", ToJSON()); //If we have a client then process the request as a message WebClient client; if (m_Clients.TryGetValue(loginToken, out client)) { //Que the Information to the client client.Que(message); //End the request on the currentContext client.EndRequest(adapter); } else { //Write response WriteContextResponse(adapter, 200, "text/html", message.ToJSON(), Encoding); //WriteContextResponse(context, message.ToJSON(), "You have recieved the Server Information via JSON RPC, Please see the HEAD of this response!"); } }
internal void EndRequest(HttpContextAdapter adapter) { if (adapter == null) { return; } //Ensure there is only one thread working on this adapter lock (adapter) { //If there are messages to send, send them if (Messages.Count > 0) { //Empty the volatile messages list into an array WebMessage[] messages = Messages.ToArray(); Messages.Clear(); //Get the server encoding Encoding encoding = Server.Encoding; try { //FrameStart adapter.OutputStream.WriteByte(encoding.GetBytes("[")[0]); } catch { //Reque the messages Messages.AddRange(messages); goto EndRequest; } byte[] binary; //Get all of the messages except the last if (messages.Length > 1) { //Iterate the array appending the JSON Object to the buffer foreach (WebMessage message in messages.Take(Math.Max(1, messages.Length - 2))) { try { //Attempt to write the message binary = encoding.GetBytes(message.ToJSON() + ','); adapter.OutputStream.Write(binary, 0, binary.Length); } catch { //Reque the messages Messages.Add(message); continue; } } } //Get the last Message WebMessage lastMessage = messages.Skip(messages.Length - 1).Take(1).Single(); try { //Attempt to write the lastMessage and FrameEnd binary = encoding.GetBytes(lastMessage.ToJSON() + ']'); adapter.OutputStream.Write(binary, 0, binary.Length); } catch { //Reque the message Messages.Add(lastMessage); goto EndRequest; } } EndRequest: //End the current listen try { adapter.OutputStream.Flush(); adapter.OutputStream.Close(); adapter.Close(); adapter.End(); } catch { return; } } }