static void Main(string[] args) { byte[] quote = Encoding.ASCII.GetBytes("Insanity: doing the same thing over and over again and expecting different results. - Albert Einstein"); var loop = new UVLoop(); var listener = new TcpListener("0.0.0.0", 17, loop); listener.ConnectionAccepted += (Tcp connection) => { connection.ReadCompleted += (ByteSpan data) => { unsafe { fixed (byte* pQuote = quote) { connection.TryWrite(new ByteSpan(pQuote, quote.Length)); } } }; connection.ReadStart(); }; listener.Listen(); loop.Run(); }
protected UVHandle(UVLoop loop, HandleType handleType) { Debug.Assert(loop != null); _loop = loop; _handle = Allocate(handleType); _gcHandle = GCHandle.Alloc(this); Data = GCHandle.ToIntPtr(_gcHandle); }
static void RunLoop(bool log) { var loop = new UVLoop(); var listener = new TcpListener(s_ipAddress, s_port, loop); var formatter = new BufferFormatter(512, FormattingData.InvariantUtf8); listener.ConnectionAccepted += (Tcp connection) => { if (log) { Console.WriteLine("connection accepted"); } connection.ReadCompleted += (ByteSpan data) => { if (log) { unsafe { var requestString = new Utf8String(data.UnsafeBuffer, data.Length); 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 response = formatter.Buffer.Slice(0, formatter.CommitedByteCount); // formatter should have a property for written bytes GCHandle gcHandle; var byteSpan = response.Pin(out gcHandle); connection.TryWrite(byteSpan); connection.Dispose(); gcHandle.Free(); // TODO: formatter should format to ByteSpan, to avoid pinning }; connection.ReadStart(); }; listener.Listen(); loop.Run(); }
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 Utf8String(data.Span); Console.WriteLine("*REQUEST:\n {0}", requestString.ToString()); } } var formatter = new ArrayFormatter(512, EncodingData.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 OwnedPinnedArray<byte>(segment.Array)) { connection.TryWrite(memory.Memory.Slice(segment.Offset, segment.Count)); connection.Dispose(); } }; connection.ReadStart(); }; listener.Listen(); loop.Run(); }
static void Main(string[] args) { var loop = new UVLoop(); var listener = new TcpListener("0.0.0.0", 7, loop); listener.ConnectionAccepted += (Tcp connection) => { connection.ReadCompleted += (data) => { connection.TryWrite(data); }; connection.ReadStart(); }; listener.Listen(); loop.Run(); }
public static void Main(string[] args) { var buffer = new byte[1024]; var quote = new Utf8String("Insanity: doing the same thing over and over again and expecting different results. - Albert Einstein"); ; var loop = new UVLoop(); var listener = new TcpListener("0.0.0.0", 17, loop); listener.ConnectionAccepted += (Tcp connection) => { connection.ReadCompleted += (data) => { quote.CopyTo(buffer); connection.TryWrite(buffer, quote.Length); }; connection.ReadStart(); }; listener.Listen(); loop.Run(); }
protected UVStream(UVLoop loop, HandleType type) : base(loop, type) { _stream = (uv_stream_t *)(Handle.ToInt64() + GetSize(HandleType.UV_HANDLE)); }
protected UVStream(UVLoop loop, HandleType type) : base(loop, type) { _stream = (uv_stream_t*)(Handle.ToInt64() + GetSize(HandleType.UV_HANDLE)); }
protected UVListener(UVLoop loop, HandleType type) : base(loop, type) { DefaultBacklog = 128; }
public static void Run(string[] args, Action<ApplicationBuilder> config) { // Build middleware pipeline var appBuilder = new ApplicationBuilder(); config(appBuilder); var appDelegate = appBuilder.Build(); var loop = new UVLoop(); var listener = new TcpListener("0.0.0.0", 5000, loop); listener.ConnectionAccepted += (Tcp connection) => { connection.ReadCompleted += (ByteSpan data) => { //unsafe //{ // var requestString = Encoding.UTF8.GetString(data.UnsafeBuffer, data.Length); // Console.WriteLine("*REQUEST:\n {0}", requestString.ToString()); //} var context = new LibuvHttpContext(); // BAD: Single threaded for now, we're ignoring the result of the task // because we know it's synchronous appDelegate(context); context.Response.ContentType = context.Response.ContentType ?? "text/plain"; if (context.Response.StatusCode == 0) { context.Response.StatusCode = 200; } // Flush and dispose (keep alive not supported) var contentLength = (int)context.Response.ContentLength; var responseBuffer = new byte[1024]; var written = 0; Append(responseBuffer, ref written, "HTTP/1.1 "); Append(responseBuffer, ref written, context.Response.StatusCode); Append(responseBuffer, ref written, " "); Append(responseBuffer, ref written, GetStatusText(context.Response.StatusCode)); Append(responseBuffer, ref written, "\r\n"); Append(responseBuffer, ref written, "Content-Length:"); Append(responseBuffer, ref written, contentLength); Append(responseBuffer, ref written, "\r\n"); Append(responseBuffer, ref written, "Content-Type:"); Append(responseBuffer, ref written, context.Response.ContentType); Append(responseBuffer, ref written, "\r\n"); Append(responseBuffer, ref written, "Connection:Close\r\n\r\n"); Append(responseBuffer, ref written, ((LibuvStream)context.Response.Body).GetBuffer()); connection.TryWrite(responseBuffer, written); connection.Dispose(); }; connection.ReadStart(); }; listener.Listen(); // Console.WriteLine("Listening on ::5000"); loop.Run(); }
public TcpListener(string ipAddress, int port, UVLoop loop) : base(loop, HandleType.UV_TCP) { UVException.ThrowIfError(UVInterop.uv_tcp_init(Loop.Handle, Handle)); Bind(this, ipAddress, port); }
public Tcp(UVLoop loop) : base(loop, HandleType.UV_TCP) { UVInterop.uv_tcp_init(loop.Handle, Handle); }