Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
300 additions
and
212 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.