-
-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathProgram.cs
136 lines (117 loc) · 5.23 KB
/
Program.cs
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
namespace Test.ServerSentEvents
{
using System;
using System.Text;
using WatsonWebserver;
using WatsonWebserver.Core;
using WatsonWebserver.Lite;
public static class Program
{
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
static bool _UsingLite = false;
static string _Hostname = "localhost";
static int _Port = 8080;
static WebserverSettings _Settings = null;
static WebserverBase _Server = null;
static async Task Main(string[] args)
{
if (args != null && args.Length > 0)
{
if (args[0].Equals("lite")) _UsingLite = true;
}
_Settings = new WebserverSettings
{
Hostname = _Hostname,
Port = _Port
};
if (_UsingLite)
{
Console.WriteLine("Initializing webserver lite");
_Server = new WatsonWebserver.Lite.WebserverLite(_Settings, DefaultRoute);
}
else
{
Console.WriteLine("Initializing webserver");
_Server = new Webserver(_Settings, DefaultRoute);
}
Console.WriteLine("Listening on " + _Settings.Prefix);
Console.WriteLine("Use /txt/test.txt");
_Server.Start();
Console.WriteLine("Press ENTER to exit");
Console.ReadLine();
}
static async Task DefaultRoute(HttpContextBase ctx)
{
try
{
if (ctx.Request.Url.RawWithoutQuery.Equals("/txt/test.txt"))
{
Console.WriteLine("- User requested /txt/test.txt using " + ctx.Request.ProtocolVersion);
ctx.Response.StatusCode = 200;
ctx.Response.ServerSentEvents = true;
long fileSize = new FileInfo("./txt/test.txt").Length;
Console.WriteLine("Sending file of size " + fileSize + " bytes");
long bytesSent = 0;
using (FileStream fs = new FileStream("./txt/test.txt", FileMode.Open, FileAccess.Read))
{
byte[] buffer = new byte[16];
long bytesRemaining = fileSize;
while (bytesRemaining > 0)
{
Thread.Sleep(500);
int bytesRead = await fs.ReadAsync(buffer, 0, buffer.Length);
if (bytesRead > 0)
{
bytesRemaining -= bytesRead;
if (bytesRemaining > 0)
{
Console.WriteLine("- Sending event of size " + bytesRead);
if (bytesRead == buffer.Length)
{
await ctx.Response.SendEvent(Encoding.UTF8.GetString(buffer), false);
}
else
{
byte[] temp = new byte[bytesRead];
Buffer.BlockCopy(buffer, 0, temp, 0, bytesRead);
await ctx.Response.SendEvent(Encoding.UTF8.GetString(temp), false);
}
}
else
{
Console.WriteLine("- Sending final chunk of size " + bytesRead);
if (bytesRead == buffer.Length)
{
await ctx.Response.SendEvent(Encoding.UTF8.GetString(buffer), true);
}
else
{
byte[] temp = new byte[bytesRead];
Buffer.BlockCopy(buffer, 0, temp, 0, bytesRead);
await ctx.Response.SendEvent(Encoding.UTF8.GetString(temp), true);
}
}
bytesSent += bytesRead;
}
}
}
Console.WriteLine("Sent " + bytesSent + " bytes");
return;
}
else
{
ctx.Response.StatusCode = 200;
await ctx.Response.Send("Watson says try using GET /txt/test.txt to see what happens!");
return;
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
}
}