Permalink
Browse files

transition to get rid of c layer

  • Loading branch information...
1 parent d7ff48a commit 8b89c3716c52e727d8f06c13862b30acf5e28963 @kersny committed Aug 5, 2011
View
@@ -33,7 +33,7 @@ deps/libuv/uv.a:
build/libuv.%: deps/libuv/uv.a
ar -x deps/libuv/uv.a
- $(CC) $($(STATIC_LIBRARY)) -m32 -o build/libuvwrap.$(STATIC_LIBRARY) *.o
+ $(CC) $($(STATIC_LIBRARY)) -m32 -o build/libuv.$(STATIC_LIBRARY) *.o
rm -rf __.SYMDEF\ SORTED *.o
clean:
@@ -1,70 +1,69 @@
using System;
using System.Runtime.InteropServices;
using System.IO;
+using System.Net;
using Libuv;
namespace Libuv.Tests {
class webserver {
- [DllImport ("uvwrap")]
+ [DllImport ("uv")]
public static extern void uv_init ();
- [DllImport ("uvwrap")]
+ [DllImport ("uv")]
public static extern void uv_run ();
- [DllImport ("uvwrap")]
- public static extern void uv_unref ();
static int clientcount = 0;
static void Main ()
{
+ var endpoint = new IPEndPoint(new IPAddress(new byte[] { 127, 0, 0, 1}), 8080);
uv_init();
- var watch = new PrepareWatcher((ptr, stat) => {
- // Console.WriteLine("Prepare Watcher Called");
+ var watch = new PrepareWatcher(() => {
+ Console.WriteLine("Prepare Watcher Called");
});
watch.Start();
- var server = new TcpServer();
- server.Listen("0.0.0.0", 8080, (socket) => {
+ var server = new TcpServer((socket) => {
clientcount++;
socket.Write(System.Text.Encoding.ASCII.GetBytes(clientcount.ToString()), 1);
if (clientcount > 5) {
socket.Close();
- server.Close();
}
Console.WriteLine("Client Connected");
- socket.OnData += (data, len) => {
- Console.WriteLine("Data Recieved: {0}", System.Text.Encoding.ASCII.GetString(data, 0, len));
- socket.Write(data, len);
- };
- socket.OnClose += () => {
- Console.WriteLine("Client Disconnected");
+ socket.OnData += (data) => {
+ Console.WriteLine("Data Recieved: {0}", System.Text.Encoding.ASCII.GetString(data, 0, data.Length));
+ socket.Write(data, data.Length);
};
+ //socket.OnClose += () => {
+ // Console.WriteLine("Client Disconnected");
+ //};
});
+ server.Listen(endpoint);
var client = new TcpSocket();
- client.OnData += (data, len) => {
- Console.WriteLine("Client Recieved: {0}", System.Text.Encoding.ASCII.GetString(data, 0, len));
+ client.OnData += (data) => {
+ Console.WriteLine("Client Recieved: {0}", System.Text.Encoding.ASCII.GetString(data, 0, data.Length));
watch.Stop();
watch.Dispose();
client.Close();
};
- client.Connect("127.0.0.1", 8080, () => {
+ client.Connect(endpoint, () => {
byte[] message = System.Text.Encoding.ASCII.GetBytes("Hello World\n");
client.Write(message, message.Length);
});
- var watch2 = new PrepareWatcher((ptr, stat) => {
- // Console.WriteLine("Prepare Watcher 2 Called");
+ var watch2 = new PrepareWatcher(() => {
+ Console.WriteLine("Prepare Watcher 2 Called");
});
watch2.Start();
- var check = new CheckWatcher((ptr, stat) => {
- // Console.WriteLine("Check Watcher Called");
+ var check = new CheckWatcher(() => {
+ Console.WriteLine("Check Watcher Called");
});
check.Start();
- var idle = new IdleWatcher((ptr, stat) => {
- // Console.WriteLine("Idle Watcher Called");
+ var idle = new IdleWatcher(() => {
+ Console.WriteLine("Idle Watcher Called");
});
idle.Start();
- var after = new TimerWatcher(new TimeSpan(0,0,5), new TimeSpan(1,0,0), (ptr, stat) => {
+ var after = new TimerWatcher(new TimeSpan(0,0,5), new TimeSpan(1,0,0), () => {
Console.WriteLine("After 5 Seconds");
});
after.Start();
- var every = new TimerWatcher(new TimeSpan(0,0,5), (ptr, stat) => {
+ var every = new TimerWatcher(new TimeSpan(0,0,5), () => {
Console.WriteLine("Every 5 Seconds");
// after.Stop();
});
@@ -18,7 +18,7 @@ static CheckWatcher()
{
unmanaged_callback = StaticCallback;
}
- public CheckWatcher(Action<int> callback)
+ public CheckWatcher(Action callback)
{
this._handle = Marshal.AllocHGlobal(Sizes.CheckWatcherSize);
uv_check_init(this._handle);
@@ -32,7 +32,7 @@ private static void StaticCallback(IntPtr watcher, int status)
var handle = (uv_handle_t)Marshal.PtrToStructure(watcher, typeof(uv_handle_t));
var instance = GCHandle.FromIntPtr(handle.data);
var watcher_instance = (CheckWatcher)instance.Target;
- watcher_instance.callback(status);
+ watcher_instance.callback();
}
public void Start()
{
@@ -18,7 +18,7 @@ static IdleWatcher()
{
unmanaged_callback = StaticCallback;
}
- public IdleWatcher(Action<int> callback)
+ public IdleWatcher(Action callback)
{
this._handle = Marshal.AllocHGlobal(Sizes.IdleWatcherSize);
uv_idle_init(this._handle);
@@ -32,7 +32,7 @@ private static void StaticCallback(IntPtr watcher, int status)
var handle = (uv_handle_t)Marshal.PtrToStructure(watcher, typeof(uv_handle_t));
var instance = GCHandle.FromIntPtr(handle.data);
var watcher_instance = (IdleWatcher)instance.Target;
- watcher_instance.callback(status);
+ watcher_instance.callback();
}
public void Start()
{
@@ -19,7 +19,7 @@ static PrepareWatcher()
unmanaged_callback = StaticCallback;
}
- public PrepareWatcher(Action<int> callback)
+ public PrepareWatcher(Action callback)
{
this._handle = Marshal.AllocHGlobal(Sizes.PrepareWatcherSize);
uv_prepare_init(this._handle);
@@ -30,10 +30,11 @@ public PrepareWatcher(Action<int> callback)
}
private static void StaticCallback(IntPtr watcher, int status)
{
+ //do something with status here maybe
var handle = (uv_handle_t)Marshal.PtrToStructure(watcher, typeof(uv_handle_t));
var instance = GCHandle.FromIntPtr(handle.data);
var watcher_instance = (PrepareWatcher)instance.Target;
- watcher_instance.callback(status);
+ watcher_instance.callback();
}
public void Start()
{
@@ -1,99 +0,0 @@
-using System;
-using System.Runtime.InteropServices;
-
-namespace Libuv {
- enum uv_err_code
- {
- UV_UNKNOWN = -1,
- UV_OK = 0,
- UV_EOF,
- UV_EACCESS,
- UV_EAGAIN,
- UV_EADDRINUSE,
- UV_EADDRNOTAVAIL,
- UV_EAFNOSUPPORT,
- UV_EALREADY,
- UV_EBADF,
- UV_EBUSY,
- UV_ECONNABORTED,
- UV_ECONNREFUSED,
- UV_ECONNRESET,
- UV_EDESTADDRREQ,
- UV_EFAULT,
- UV_EHOSTUNREACH,
- UV_EINTR,
- UV_EINVAL,
- UV_EISCONN,
- UV_EMFILE,
- UV_ENETDOWN,
- UV_ENETUNREACH,
- UV_ENFILE,
- UV_ENOBUFS,
- UV_ENOMEM,
- UV_ENONET,
- UV_ENOPROTOOPT,
- UV_ENOTCONN,
- UV_ENOTSOCK,
- UV_ENOTSUP,
- UV_EPROTO,
- UV_EPROTONOSUPPORT,
- UV_EPROTOTYPE,
- UV_ETIMEDOUT,
- UV_ECHARSET,
- UV_EAIFAMNOSUPPORT,
- UV_EAINONAME,
- UV_EAISERVICE,
- UV_EAISOCKTYPE,
- UV_ESHUTDOWN
- }
- struct uv_err_t
- {
- public uv_err_code code;
- int sys_errno_;
- }
- public abstract class TcpEntity : IDisposable {
- internal IntPtr _handle;
- internal GCHandle gc_handle;
- public TcpEntity()
- {
- this._handle = manos_uv_tcp_t_create();
- int err = uv_tcp_init(this._handle);
- if (err != 0) throw new Exception(uv_last_error().code.ToString());
- }
- private void Cleanup()
- {
- manos_uv_destroy(this._handle);
- this._handle = IntPtr.Zero;
- }
- ~TcpEntity()
- {
- Cleanup();
- }
- public void Dispose()
- {
- Cleanup();
- System.GC.SuppressFinalize(this);
- }
- public void Close()
- {
- int err = uv_close(this._handle, (x) => {
- this.Dispose();
- });
- if (err != 0) throw new Exception(uv_last_error().code.ToString());
- }
- [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
- public delegate void uv_connection_cb(IntPtr socket, int status);
- [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
- public delegate void uv_close_cb(IntPtr socket);
- [DllImport ("uvwrap")]
- internal static extern int uv_tcp_init (IntPtr socket);
- [DllImport ("uvwrap")]
- internal static extern IntPtr manos_uv_tcp_t_create();
- [DllImport ("uvwrap")]
- internal static extern void manos_uv_destroy(IntPtr uv_tcp_t_ptr);
- [DllImport ("uvwrap")]
- internal static extern int uv_close(IntPtr handle, uv_close_cb cb);
- [DllImport ("uvwrap")]
- internal static extern uv_err_t uv_last_error();
- }
-}
@@ -1,22 +1,52 @@
using System;
+using System.Net;
using System.Runtime.InteropServices;
+
namespace Libuv {
- public class TcpServer : TcpEntity {
- public TcpServer() : base()
+ public class TcpServer {
+ [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+ public delegate void uv_connection_cb(IntPtr server, int status);
+ [DllImport("uv")]
+ internal static extern int uv_tcp_init(IntPtr prepare);
+ [DllImport("uv")]
+ internal static extern int uv_tcp_bind(IntPtr prepare, sockaddr_in address);
+ [DllImport("uv")]
+ internal static extern int uv_tcp_listen(IntPtr stream, int backlog, uv_connection_cb cb);
+ [DllImport("uv")]
+ internal static extern sockaddr_in uv_ip4_addr(string ip, int port);
+
+ private static uv_connection_cb unmanaged_callback;
+
+ static TcpServer()
{
+ unmanaged_callback = StaticCallback;
}
- public void Listen(string ip, int port, Action<TcpSocket> OnConnect)
+
+ private Action<TcpSocket> callback;
+ private IntPtr _handle;
+ private GCHandle me;
+
+ public TcpServer(Action<TcpSocket> callback)
{
- int err = manos_uv_tcp_bind(this._handle, ip, port);
- //if (err != 0 ) throw new Exception(uv_last_error().code.ToString());
- err = uv_tcp_listen(this._handle, 128, (sock, status) => {
- OnConnect(new TcpSocket(this._handle));
- });
- if (err != 0 ) throw new Exception(uv_last_error().code.ToString());
+ this.callback = callback;
+ this._handle = Marshal.AllocHGlobal(Sizes.TcpTSize);
+ uv_tcp_init(this._handle);
+ var handle = (uv_handle_t)Marshal.PtrToStructure(this._handle, typeof(uv_handle_t));
+ this.me = GCHandle.Alloc(this, GCHandleType.Pinned);
+ handle.data = GCHandle.ToIntPtr(this.me);
+ }
+ public void Listen(IPEndPoint endpoint)
+ {
+ var info = uv_ip4_addr(endpoint.Address.ToString(), endpoint.Port);
+ uv_tcp_bind(this._handle, info);
+ uv_tcp_listen(this._handle, 128, unmanaged_callback);
+ }
+ public static void StaticCallback(IntPtr server_ptr, int status)
+ {
+ var handle = (uv_handle_t)Marshal.PtrToStructure(server_ptr, typeof(uv_handle_t));
+ var instance = GCHandle.FromIntPtr(handle.data);
+ var server = (TcpServer)instance.Target;
+ server.callback(new TcpSocket(server._handle));
}
- [DllImport ("uvwrap")]
- internal static extern int manos_uv_tcp_bind (IntPtr socket, string host, int port);
- [DllImport ("uvwrap")]
- internal static extern int uv_tcp_listen(IntPtr socket, int backlog, uv_connection_cb callback);
}
}
Oops, something went wrong.

0 comments on commit 8b89c37

Please sign in to comment.