|
1 | 1 | using System; |
2 | 2 | using System.Runtime.InteropServices; |
3 | 3 | using System.IO; |
| 4 | +using System.Net; |
4 | 5 | using Libuv; |
5 | 6 |
|
6 | 7 | namespace Libuv.Tests { |
7 | 8 | class webserver { |
8 | | - [DllImport ("uvwrap")] |
| 9 | + [DllImport ("uv")] |
9 | 10 | public static extern void uv_init (); |
10 | | - [DllImport ("uvwrap")] |
| 11 | + [DllImport ("uv")] |
11 | 12 | public static extern void uv_run (); |
12 | | - [DllImport ("uvwrap")] |
13 | | - public static extern void uv_unref (); |
14 | 13 | static int clientcount = 0; |
15 | 14 | static void Main () |
16 | 15 | { |
| 16 | + var endpoint = new IPEndPoint(new IPAddress(new byte[] { 127, 0, 0, 1}), 8080); |
17 | 17 | uv_init(); |
18 | 18 |
|
19 | | - var watch = new PrepareWatcher((ptr, stat) => { |
20 | | - // Console.WriteLine("Prepare Watcher Called"); |
| 19 | + var watch = new PrepareWatcher(() => { |
| 20 | + Console.WriteLine("Prepare Watcher Called"); |
21 | 21 | }); |
22 | 22 | watch.Start(); |
23 | | - var server = new TcpServer(); |
24 | | - server.Listen("0.0.0.0", 8080, (socket) => { |
| 23 | + var server = new TcpServer((socket) => { |
25 | 24 | clientcount++; |
26 | 25 | socket.Write(System.Text.Encoding.ASCII.GetBytes(clientcount.ToString()), 1); |
27 | 26 | if (clientcount > 5) { |
28 | 27 | socket.Close(); |
29 | | - server.Close(); |
30 | 28 | } |
31 | 29 | 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); |
38 | 33 | }; |
| 34 | + //socket.OnClose += () => { |
| 35 | + // Console.WriteLine("Client Disconnected"); |
| 36 | + //}; |
39 | 37 | }); |
| 38 | + server.Listen(endpoint); |
40 | 39 | 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)); |
43 | 42 | watch.Stop(); |
44 | 43 | watch.Dispose(); |
45 | 44 | client.Close(); |
46 | 45 | }; |
47 | | - client.Connect("127.0.0.1", 8080, () => { |
| 46 | + client.Connect(endpoint, () => { |
48 | 47 | byte[] message = System.Text.Encoding.ASCII.GetBytes("Hello World\n"); |
49 | 48 | client.Write(message, message.Length); |
50 | 49 | }); |
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"); |
53 | 52 | }); |
54 | 53 | 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"); |
57 | 56 | }); |
58 | 57 | 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"); |
61 | 60 | }); |
62 | 61 | 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), () => { |
64 | 63 | Console.WriteLine("After 5 Seconds"); |
65 | 64 | }); |
66 | 65 | after.Start(); |
67 | | - var every = new TimerWatcher(new TimeSpan(0,0,5), (ptr, stat) => { |
| 66 | + var every = new TimerWatcher(new TimeSpan(0,0,5), () => { |
68 | 67 | Console.WriteLine("Every 5 Seconds"); |
69 | 68 | // after.Stop(); |
70 | 69 | }); |
|
0 commit comments