Skip to content

Commit 8b89c37

Browse files
committed
transition to get rid of c layer
1 parent d7ff48a commit 8b89c37

File tree

11 files changed

+300
-212
lines changed

11 files changed

+300
-212
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ deps/libuv/uv.a:
3333

3434
build/libuv.%: deps/libuv/uv.a
3535
ar -x deps/libuv/uv.a
36-
$(CC) $($(STATIC_LIBRARY)) -m32 -o build/libuvwrap.$(STATIC_LIBRARY) *.o
36+
$(CC) $($(STATIC_LIBRARY)) -m32 -o build/libuv.$(STATIC_LIBRARY) *.o
3737
rm -rf __.SYMDEF\ SORTED *.o
3838

3939
clean:

src/Libuv.Tests/webserver.cs

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,69 @@
11
using System;
22
using System.Runtime.InteropServices;
33
using System.IO;
4+
using System.Net;
45
using Libuv;
56

67
namespace Libuv.Tests {
78
class webserver {
8-
[DllImport ("uvwrap")]
9+
[DllImport ("uv")]
910
public static extern void uv_init ();
10-
[DllImport ("uvwrap")]
11+
[DllImport ("uv")]
1112
public static extern void uv_run ();
12-
[DllImport ("uvwrap")]
13-
public static extern void uv_unref ();
1413
static int clientcount = 0;
1514
static void Main ()
1615
{
16+
var endpoint = new IPEndPoint(new IPAddress(new byte[] { 127, 0, 0, 1}), 8080);
1717
uv_init();
1818

19-
var watch = new PrepareWatcher((ptr, stat) => {
20-
// Console.WriteLine("Prepare Watcher Called");
19+
var watch = new PrepareWatcher(() => {
20+
Console.WriteLine("Prepare Watcher Called");
2121
});
2222
watch.Start();
23-
var server = new TcpServer();
24-
server.Listen("0.0.0.0", 8080, (socket) => {
23+
var server = new TcpServer((socket) => {
2524
clientcount++;
2625
socket.Write(System.Text.Encoding.ASCII.GetBytes(clientcount.ToString()), 1);
2726
if (clientcount > 5) {
2827
socket.Close();
29-
server.Close();
3028
}
3129
Console.WriteLine("Client Connected");
32-
socket.OnData += (data, len) => {
33-
Console.WriteLine("Data Recieved: {0}", System.Text.Encoding.ASCII.GetString(data, 0, len));
34-
socket.Write(data, len);
35-
};
36-
socket.OnClose += () => {
37-
Console.WriteLine("Client Disconnected");
30+
socket.OnData += (data) => {
31+
Console.WriteLine("Data Recieved: {0}", System.Text.Encoding.ASCII.GetString(data, 0, data.Length));
32+
socket.Write(data, data.Length);
3833
};
34+
//socket.OnClose += () => {
35+
// Console.WriteLine("Client Disconnected");
36+
//};
3937
});
38+
server.Listen(endpoint);
4039
var client = new TcpSocket();
41-
client.OnData += (data, len) => {
42-
Console.WriteLine("Client Recieved: {0}", System.Text.Encoding.ASCII.GetString(data, 0, len));
40+
client.OnData += (data) => {
41+
Console.WriteLine("Client Recieved: {0}", System.Text.Encoding.ASCII.GetString(data, 0, data.Length));
4342
watch.Stop();
4443
watch.Dispose();
4544
client.Close();
4645
};
47-
client.Connect("127.0.0.1", 8080, () => {
46+
client.Connect(endpoint, () => {
4847
byte[] message = System.Text.Encoding.ASCII.GetBytes("Hello World\n");
4948
client.Write(message, message.Length);
5049
});
51-
var watch2 = new PrepareWatcher((ptr, stat) => {
52-
// Console.WriteLine("Prepare Watcher 2 Called");
50+
var watch2 = new PrepareWatcher(() => {
51+
Console.WriteLine("Prepare Watcher 2 Called");
5352
});
5453
watch2.Start();
55-
var check = new CheckWatcher((ptr, stat) => {
56-
// Console.WriteLine("Check Watcher Called");
54+
var check = new CheckWatcher(() => {
55+
Console.WriteLine("Check Watcher Called");
5756
});
5857
check.Start();
59-
var idle = new IdleWatcher((ptr, stat) => {
60-
// Console.WriteLine("Idle Watcher Called");
58+
var idle = new IdleWatcher(() => {
59+
Console.WriteLine("Idle Watcher Called");
6160
});
6261
idle.Start();
63-
var after = new TimerWatcher(new TimeSpan(0,0,5), new TimeSpan(1,0,0), (ptr, stat) => {
62+
var after = new TimerWatcher(new TimeSpan(0,0,5), new TimeSpan(1,0,0), () => {
6463
Console.WriteLine("After 5 Seconds");
6564
});
6665
after.Start();
67-
var every = new TimerWatcher(new TimeSpan(0,0,5), (ptr, stat) => {
66+
var every = new TimerWatcher(new TimeSpan(0,0,5), () => {
6867
Console.WriteLine("Every 5 Seconds");
6968
// after.Stop();
7069
});

src/Libuv/CheckWatcher.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ static CheckWatcher()
1818
{
1919
unmanaged_callback = StaticCallback;
2020
}
21-
public CheckWatcher(Action<int> callback)
21+
public CheckWatcher(Action callback)
2222
{
2323
this._handle = Marshal.AllocHGlobal(Sizes.CheckWatcherSize);
2424
uv_check_init(this._handle);
@@ -32,7 +32,7 @@ private static void StaticCallback(IntPtr watcher, int status)
3232
var handle = (uv_handle_t)Marshal.PtrToStructure(watcher, typeof(uv_handle_t));
3333
var instance = GCHandle.FromIntPtr(handle.data);
3434
var watcher_instance = (CheckWatcher)instance.Target;
35-
watcher_instance.callback(status);
35+
watcher_instance.callback();
3636
}
3737
public void Start()
3838
{

src/Libuv/IdleWatcher.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ static IdleWatcher()
1818
{
1919
unmanaged_callback = StaticCallback;
2020
}
21-
public IdleWatcher(Action<int> callback)
21+
public IdleWatcher(Action callback)
2222
{
2323
this._handle = Marshal.AllocHGlobal(Sizes.IdleWatcherSize);
2424
uv_idle_init(this._handle);
@@ -32,7 +32,7 @@ private static void StaticCallback(IntPtr watcher, int status)
3232
var handle = (uv_handle_t)Marshal.PtrToStructure(watcher, typeof(uv_handle_t));
3333
var instance = GCHandle.FromIntPtr(handle.data);
3434
var watcher_instance = (IdleWatcher)instance.Target;
35-
watcher_instance.callback(status);
35+
watcher_instance.callback();
3636
}
3737
public void Start()
3838
{

src/Libuv/PrepareWatcher.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ static PrepareWatcher()
1919
unmanaged_callback = StaticCallback;
2020
}
2121

22-
public PrepareWatcher(Action<int> callback)
22+
public PrepareWatcher(Action callback)
2323
{
2424
this._handle = Marshal.AllocHGlobal(Sizes.PrepareWatcherSize);
2525
uv_prepare_init(this._handle);
@@ -30,10 +30,11 @@ public PrepareWatcher(Action<int> callback)
3030
}
3131
private static void StaticCallback(IntPtr watcher, int status)
3232
{
33+
//do something with status here maybe
3334
var handle = (uv_handle_t)Marshal.PtrToStructure(watcher, typeof(uv_handle_t));
3435
var instance = GCHandle.FromIntPtr(handle.data);
3536
var watcher_instance = (PrepareWatcher)instance.Target;
36-
watcher_instance.callback(status);
37+
watcher_instance.callback();
3738
}
3839
public void Start()
3940
{

src/Libuv/TcpEntity.cs

Lines changed: 0 additions & 99 deletions
This file was deleted.

src/Libuv/TcpServer.cs

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,52 @@
11
using System;
2+
using System.Net;
23
using System.Runtime.InteropServices;
4+
35
namespace Libuv {
4-
public class TcpServer : TcpEntity {
5-
public TcpServer() : base()
6+
public class TcpServer {
7+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
8+
public delegate void uv_connection_cb(IntPtr server, int status);
9+
[DllImport("uv")]
10+
internal static extern int uv_tcp_init(IntPtr prepare);
11+
[DllImport("uv")]
12+
internal static extern int uv_tcp_bind(IntPtr prepare, sockaddr_in address);
13+
[DllImport("uv")]
14+
internal static extern int uv_tcp_listen(IntPtr stream, int backlog, uv_connection_cb cb);
15+
[DllImport("uv")]
16+
internal static extern sockaddr_in uv_ip4_addr(string ip, int port);
17+
18+
private static uv_connection_cb unmanaged_callback;
19+
20+
static TcpServer()
621
{
22+
unmanaged_callback = StaticCallback;
723
}
8-
public void Listen(string ip, int port, Action<TcpSocket> OnConnect)
24+
25+
private Action<TcpSocket> callback;
26+
private IntPtr _handle;
27+
private GCHandle me;
28+
29+
public TcpServer(Action<TcpSocket> callback)
930
{
10-
int err = manos_uv_tcp_bind(this._handle, ip, port);
11-
//if (err != 0 ) throw new Exception(uv_last_error().code.ToString());
12-
err = uv_tcp_listen(this._handle, 128, (sock, status) => {
13-
OnConnect(new TcpSocket(this._handle));
14-
});
15-
if (err != 0 ) throw new Exception(uv_last_error().code.ToString());
31+
this.callback = callback;
32+
this._handle = Marshal.AllocHGlobal(Sizes.TcpTSize);
33+
uv_tcp_init(this._handle);
34+
var handle = (uv_handle_t)Marshal.PtrToStructure(this._handle, typeof(uv_handle_t));
35+
this.me = GCHandle.Alloc(this, GCHandleType.Pinned);
36+
handle.data = GCHandle.ToIntPtr(this.me);
37+
}
38+
public void Listen(IPEndPoint endpoint)
39+
{
40+
var info = uv_ip4_addr(endpoint.Address.ToString(), endpoint.Port);
41+
uv_tcp_bind(this._handle, info);
42+
uv_tcp_listen(this._handle, 128, unmanaged_callback);
43+
}
44+
public static void StaticCallback(IntPtr server_ptr, int status)
45+
{
46+
var handle = (uv_handle_t)Marshal.PtrToStructure(server_ptr, typeof(uv_handle_t));
47+
var instance = GCHandle.FromIntPtr(handle.data);
48+
var server = (TcpServer)instance.Target;
49+
server.callback(new TcpSocket(server._handle));
1650
}
17-
[DllImport ("uvwrap")]
18-
internal static extern int manos_uv_tcp_bind (IntPtr socket, string host, int port);
19-
[DllImport ("uvwrap")]
20-
internal static extern int uv_tcp_listen(IntPtr socket, int backlog, uv_connection_cb callback);
2151
}
2252
}

0 commit comments

Comments
 (0)