2013-09-26 19:50:11 +08:00
|
|
|
// This file is part of mongoose web server project,
|
|
|
|
// https://github.com/cesanta/mongoose
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
[StructLayout(LayoutKind.Sequential)] public struct MongooseHeader {
|
2013-10-06 19:16:46 +08:00
|
|
|
[MarshalAs(UnmanagedType.LPTStr)] public IntPtr name;
|
|
|
|
[MarshalAs(UnmanagedType.LPTStr)] public IntPtr value;
|
2013-09-26 19:50:11 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// mongoose.h :: struct mg_request_info
|
|
|
|
[StructLayout(LayoutKind.Sequential)] public struct MongooseRequestInfo {
|
2013-10-06 19:16:46 +08:00
|
|
|
[MarshalAs(UnmanagedType.LPTStr)] public string request_method;
|
|
|
|
[MarshalAs(UnmanagedType.LPTStr)] public string uri;
|
|
|
|
[MarshalAs(UnmanagedType.LPTStr)] public string http_version;
|
|
|
|
[MarshalAs(UnmanagedType.LPTStr)] public string query_string;
|
|
|
|
[MarshalAs(UnmanagedType.LPTStr)] public string remote_user;
|
2013-09-26 19:50:11 +08:00
|
|
|
public int remote_ip;
|
|
|
|
public int remote_port;
|
|
|
|
public int is_ssl;
|
|
|
|
[MarshalAs(UnmanagedType.ByValArray,SizeConst=64)]
|
|
|
|
public MongooseHeader[] http_headers;
|
|
|
|
};
|
|
|
|
|
2013-10-06 19:16:46 +08:00
|
|
|
[StructLayout(LayoutKind.Sequential)] public struct MongooseEvent {
|
|
|
|
public int type;
|
|
|
|
public IntPtr user_data;
|
|
|
|
public IntPtr conn_data;
|
|
|
|
public IntPtr event_param;
|
|
|
|
public IntPtr conn;
|
|
|
|
public IntPtr request_info;
|
2013-09-26 19:50:11 +08:00
|
|
|
};
|
|
|
|
|
2013-10-06 19:16:46 +08:00
|
|
|
public delegate int MongooseEventHandlerN(ref MongooseEvent ev);
|
|
|
|
public delegate int MongooseEventHandler(MongooseEvent ev);
|
|
|
|
|
2013-09-26 19:50:11 +08:00
|
|
|
public class Mongoose {
|
|
|
|
public const string dll_name_ = "mongoose";
|
|
|
|
public string version_ = "??";
|
|
|
|
|
|
|
|
// These are here to store a ref to the callbacks
|
|
|
|
// while they are over in unmanaged code, to prevent garbage collection.
|
2013-10-06 19:16:46 +08:00
|
|
|
private event MongooseEventHandlerN delegates;
|
2013-09-26 19:50:11 +08:00
|
|
|
|
|
|
|
private IntPtr ctx_;
|
|
|
|
|
|
|
|
[DllImport(dll_name_)] private static extern
|
|
|
|
IntPtr mg_start([MarshalAs(UnmanagedType.LPArray,
|
|
|
|
ArraySubType=UnmanagedType.LPTStr)] string[] options,
|
2013-10-06 19:16:46 +08:00
|
|
|
MongooseEventHandlerN callback,
|
2013-09-26 19:50:11 +08:00
|
|
|
IntPtr user_data);
|
|
|
|
[DllImport(dll_name_)] private static extern void mg_stop(IntPtr ctx);
|
2013-10-06 19:16:46 +08:00
|
|
|
[DllImport(dll_name_)] private static extern IntPtr mg_version();
|
|
|
|
[DllImport(dll_name_)] public static extern int mg_write(IntPtr conn,
|
|
|
|
string data, int length);
|
2013-09-26 19:50:11 +08:00
|
|
|
|
2013-10-06 19:16:46 +08:00
|
|
|
public Mongoose(string document_root,
|
|
|
|
string listening_ports,
|
|
|
|
MongooseEventHandler event_handler) {
|
|
|
|
version_ = Marshal.PtrToStringAnsi(mg_version());
|
2013-09-26 19:50:11 +08:00
|
|
|
|
|
|
|
string[] options = {
|
|
|
|
"document_root", document_root,
|
|
|
|
"listening_ports", listening_ports,
|
|
|
|
null
|
|
|
|
};
|
|
|
|
|
2013-10-06 19:16:46 +08:00
|
|
|
MongooseEventHandlerN cb = delegate(ref MongooseEvent ev) {
|
|
|
|
return event_handler(ev);
|
2013-09-26 19:50:11 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Prevent garbage collection
|
2013-10-06 19:16:46 +08:00
|
|
|
delegates += cb;
|
2013-09-26 19:50:11 +08:00
|
|
|
|
2013-10-06 19:16:46 +08:00
|
|
|
ctx_ = mg_start(options, cb, IntPtr.Zero);
|
2013-09-26 19:50:11 +08:00
|
|
|
}
|
|
|
|
|
2013-10-06 19:16:46 +08:00
|
|
|
public static int write(IntPtr conn, string data) {
|
|
|
|
return mg_write(conn, data, data.Length);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void stop() {
|
2013-09-26 19:50:11 +08:00
|
|
|
if (this.ctx_ != IntPtr.Zero) {
|
|
|
|
mg_stop(this.ctx_);
|
|
|
|
}
|
|
|
|
this.ctx_ = IntPtr.Zero;
|
|
|
|
}
|
|
|
|
|
2013-10-06 19:16:46 +08:00
|
|
|
~Mongoose() {
|
|
|
|
stop();
|
2013-09-26 19:50:11 +08:00
|
|
|
}
|
|
|
|
}
|