static void RunLoop(bool log) { var loop = new UVLoop(); var listener = new TcpListener(s_ipAddress, s_port, loop); var formatter = new ArrayFormatter(512, EncodingData.InvariantUtf8); listener.ConnectionAccepted += (Tcp connection) => { if (log) { Console.WriteLine("connection accepted"); } connection.ReadCompleted += (data) => { if (log) { unsafe { var requestString = new Utf8String(data); Console.WriteLine("*REQUEST:\n {0}", requestString.ToString()); } } formatter.Clear(); formatter.Append("HTTP/1.1 200 OK"); formatter.Append("\r\n\r\n"); formatter.Append("Hello World!"); if (log) { formatter.Format(" @ {0:O}", DateTime.UtcNow); } var segment = formatter.Formatted; unsafe { fixed(byte *p = segment.Array) { var response = new Memory <byte>(segment.Array, segment.Offset, segment.Count, pointer: p); connection.TryWrite(response); } } connection.Dispose(); }; connection.ReadStart(); }; listener.Listen(); loop.Run(); }
public void ArrayFormatter_String() { var testArray = new string[] { "alpha", "beta", "gamma" }; var arrayFormatter = new ArrayFormatter(",", Scrubber); var result = arrayFormatter.Format(testArray, 0); Assert.Single(result); Assert.True(result.ContainsKey(string.Empty), "Should have a single, empty key"); Assert.Equal("alpha,beta,gamma", result[string.Empty]); }
public void ArrayFormatter_Double() { var testArray = new double[] { 1.1, 2.2, 3.3, 4.4 }; var arrayFormatter = new ArrayFormatter(",", Scrubber); var result = arrayFormatter.Format(testArray, 0); Assert.Single(result); Assert.True(result.ContainsKey(string.Empty), "Should have a single, empty key"); Assert.Equal("1.1,2.2,3.3,4.4", result[string.Empty]); }
static void RunLoop(bool log) { var loop = new UVLoop(); var listener = new TcpListener(s_ipAddress, s_port, loop); listener.ConnectionAccepted += (Tcp connection) => { if (log) { Console.WriteLine("connection accepted"); } connection.ReadCompleted += (data) => { if (log) { unsafe { var requestString = new Utf8Span(data.Span); Console.WriteLine("*REQUEST:\n {0}", requestString.ToString()); } } var formatter = new ArrayFormatter(512, SymbolTable.InvariantUtf8); formatter.Clear(); formatter.Append("HTTP/1.1 200 OK"); formatter.Append("\r\n\r\n"); formatter.Append("Hello World!"); if (log) { formatter.Format(" @ {0:O}", DateTime.UtcNow); } var segment = formatter.Formatted; using (var memory = new OwnedPinnedBuffer <byte>(segment.Array)) { connection.TryWrite(memory.Memory.Slice(segment.Offset, segment.Count)); connection.Dispose(); } }; connection.ReadStart(); }; listener.Listen(); loop.Run(); }