-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonEchoServer.cs
More file actions
117 lines (108 loc) · 4.09 KB
/
JsonEchoServer.cs
File metadata and controls
117 lines (108 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace JsonEchoServer
{
// CAUTION: does not support proper shutdown
// To represent a JSON request
public class Request
{
public String Method { get; set; }
public Dictionary<String, String> Headers { get; set; }
public JObject Payload { get; set; }
public override String ToString()
{
return $"Method: {Method}, Headers: {Headers}, Payload: {Payload}";
}
}
// To represent a JSON response
public class Response
{
public int Status { get; set; }
public Dictionary<String, String> Headers { get; set; }
public JObject Payload { get; set; }
}
class Program
{
private const int port = 8081;
private static int counter;
static async Task Main(string[] args)
{
var listener = new TcpListener(IPAddress.Loopback, port);
listener.Start();
Console.WriteLine($"Listening on {port}");
while (true)
{
var client = await listener.AcceptTcpClientAsync();
var id = counter++;
Console.WriteLine($"connection accepted with id '{id}'");
Handle(id, client);
}
}
private static readonly JsonSerializer serializer = new JsonSerializer();
private static async void Handle(int id, TcpClient client)
{
using (client)
{
var stream = client.GetStream();
var reader = new JsonTextReader(new StreamReader(stream))
{
// To support reading multiple top-level objects
SupportMultipleContent = true
};
var writer = new JsonTextWriter(new StreamWriter(stream));
while (true)
{
try
{
// to consume any bytes until start of object ('{')
do
{
await reader.ReadAsync();
Console.WriteLine($"advanced to {reader.TokenType}");
} while (reader.TokenType != JsonToken.StartObject
&& reader.TokenType != JsonToken.None);
if (reader.TokenType == JsonToken.None)
{
Console.WriteLine($"[{id}] reached end of input stream, ending.");
return;
}
var json = await JObject.LoadAsync(reader);
// to ensure that proper deserialization is possible
json.ToObject<Request>();
var response = new Response
{
Status = 200,
Payload = json,
};
serializer.Serialize(writer, response);
await writer.FlushAsync();
}
catch (JsonReaderException e)
{
Console.WriteLine($"[{id}] Error reading JSON: {e.Message}, continuing");
var response = new Response
{
Status = 400,
};
serializer.Serialize(writer, response);
await writer.FlushAsync();
// close the connection because an error may not be recoverable by the reader
return;
}
catch (Exception e)
{
Console.WriteLine($"[{id}] Unexpected exception, closing connection {e.Message}");
return;
}
}
}
}
}
}