Permalink
Browse files

initial

  • Loading branch information...
0 parents commit ebfcad8704be155bd1421cb87792357d852b53ae @kersny committed Jul 12, 2011
Showing with 192 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +17 −0 Makefile
  3. +1 −0 libuv
  4. +49 −0 uv_wrap.c
  5. +75 −0 webserver.cs
  6. +49 −0 webserver.csproj
@@ -0,0 +1 @@
+build/
@@ -0,0 +1,17 @@
+all: build/webserver.exe build/libuvwrap.dylib
+
+build/webserver.exe: webserver.cs webserver.csproj
+ xbuild
+
+libuv/uv.a:
+ CPPFLAGS=-m32 $(MAKE) -C libuv
+
+build/libuvwrap.dylib: libuv/uv.a uv_wrap.c
+ #ar -x libuv/uv.a
+ $(CC) -dynamiclib uv_wrap.c -m32 -o build/libuvwrap.dylib libuv/uv.a
+ #$(RM) *.o __.SYMDEF\ SORTED
+
+
+clean:
+ $(RM) -rf build/
+ $(MAKE) -C libuv clean
1 libuv
Submodule libuv added at 2e49cc
@@ -0,0 +1,49 @@
+#include "libuv/include/uv.h"
+#include <stdlib.h>
+
+typedef void (*manos_uv_read_cb)(uv_stream_t* tcp, long nread, char* data);
+
+int manos_uv_tcp_bind(uv_tcp_t *handle, const char* host, int port)
+{
+ uv_tcp_bind(handle, uv_ip4_addr(host, port));
+}
+
+static uv_buf_t alloc_cb(uv_stream_t* handle, size_t suggested_size)
+{
+ uv_buf_t buf;
+ buf.base = (char*) malloc(suggested_size);
+ buf.len = suggested_size;
+ return buf;
+}
+static void on_close(uv_handle_t* peer) {
+ free(peer);
+}
+static void after_shutdown(uv_req_t* req, int status) {
+ uv_close(req->handle, on_close);
+ free(req);
+}
+static void read_cb(uv_stream_t* handle, ssize_t nread, uv_buf_t buf)
+{
+ uv_req_t* req;
+ if (nread < 0) {
+ /* error or eof */
+ if (buf.base) {
+ free(buf.base);
+ }
+ req = (uv_req_t*) malloc(sizeof *req);
+ uv_req_init(req, (uv_handle_t*)handle, (void *(*)(void *))after_shutdown);
+ uv_shutdown(req);
+ return;
+ }
+ if (nread == 0) {
+ free(buf.base);
+ return;
+ }
+ ((manos_uv_read_cb)handle->data)(handle, nread, buf.base);
+}
+int manos_uv_read_start(uv_stream_t* handle, manos_uv_read_cb manos_read_cb)
+{
+ handle->data = manos_read_cb;
+ uv_read_start(handle, alloc_cb, read_cb);
+}
+
@@ -0,0 +1,75 @@
+using System;
+using System.Runtime.InteropServices;
+using System.IO;
+
+namespace webserver {
+ class webserver {
+ [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+ internal delegate void uv_connection_cb(IntPtr socket, int status);
+ [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+ internal delegate void manos_uv_read_cb(IntPtr socket, int count, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)] byte[] data);
+ static void Main ()
+ {
+ uv_init();
+ //THIS NEEDS TO BE CHANGED... size changes based on OS, we need some way to get that
+ //152 is what it is on my mac
+ IntPtr server = Marshal.AllocHGlobal(152);
+ uv_tcp_init(server);
+ manos_uv_tcp_bind(server, "0.0.0.0", 8080);
+ uv_tcp_listen(server, 128, (sock, status) => {
+ IntPtr handle = Marshal.AllocHGlobal(152);
+ uv_tcp_init(handle);
+ uv_accept(sock, handle);
+ manos_uv_read_start(handle, (socket, count, data) => {
+ Console.WriteLine(BitConverter.ToString(data, 0, count));
+ Console.WriteLine(System.Text.Encoding.ASCII.GetString(data, 0, count));
+ });
+ });
+ Console.WriteLine ("Hello World");
+ uv_run();
+ Marshal.FreeHGlobal(server);
+ }
+
+ [DllImport ("uvwrap")]
+ public static extern void uv_init ();
+ [DllImport ("uvwrap")]
+ public static extern void uv_run ();
+ [DllImport ("uvwrap")]
+ public static extern void uv_tcp_init (IntPtr socket);
+ [DllImport ("uvwrap")]
+ public static extern void manos_uv_tcp_bind (IntPtr socket, string host, int port);
+ [DllImport ("uvwrap")]
+ public static extern void uv_tcp_listen(IntPtr socket, int backlog, uv_connection_cb callback);
+ [DllImport ("uvwrap")]
+ public static extern sockaddr_in uv_ip4_addr(string host, int port);
+ [DllImport ("uvwrap")]
+ public static extern void uv_accept(IntPtr socket, IntPtr stream);
+ [DllImport ("uvwrap")]
+ public static extern int manos_uv_read_start(IntPtr stream, manos_uv_read_cb cb);
+
+ //Stolen from http://www.elitepvpers.com/forum/co2-programming/159327-advanced-winsock-c.html
+ [StructLayout(LayoutKind.Sequential, Size=16)]
+ public struct sockaddr_in
+ {
+ public const int Size = 16;
+
+ public short sin_family;
+ public ushort sin_port;
+ public struct in_addr
+ {
+ public uint S_addr;
+ public struct _S_un_b
+ {
+ public byte s_b1, s_b2, s_b3, s_b4;
+ }
+ public _S_un_b S_un_b;
+ public struct _S_un_w
+ {
+ public ushort s_w1, s_w2;
+ }
+ public _S_un_w S_un_w;
+ }
+ public in_addr sin_addr;
+ }
+ }
+}
@@ -0,0 +1,49 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build;" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProductVersion>9.0.21022</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{2548B25A-2E1F-4E21-BC43-7E39B1F6F23F}</ProjectGuid>
+ <OutputType>Exe</OutputType>
+ <RootNamespace>webserver</RootNamespace>
+ <AssemblyName>webserver</AssemblyName>
+ <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+ <IntermediateOutputPath>build/tmp/</IntermediateOutputPath>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>build\</OutputPath>
+ <DefineConstants>DEBUG</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ <ConsolePause>false</ConsolePause>
+ <GenerateDocumentation>true</GenerateDocumentation>
+ <NoWarn>1591</NoWarn>
+ <PlatformTarget>x86</PlatformTarget>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>none</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>build\</OutputPath>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ <ConsolePause>false</ConsolePause>
+ <GenerateDocumentation>true</GenerateDocumentation>
+ <NoWarn>1591</NoWarn>
+ <PlatformTarget>x86</PlatformTarget>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="System" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="webserver.cs" />
+ </ItemGroup>
+ <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+ <Target Name="ListSources">
+ <Message Text="@(Compile)" Importance="High" />
+ </Target>
+</Project>

0 comments on commit ebfcad8

Please sign in to comment.