mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-07 16:01:30 +00:00
Fixed C# example to work with 5.3
This commit is contained in:
1 parent
293c5e5852
commit
f7fcfa6a42
2 files changed
+65
-84
No files matched your search
+28
-22
@@ -2,36 +2,42 @@
|
|||||||
// https://github.com/cesanta/mongoose
|
// https://github.com/cesanta/mongoose
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
|
|
||||||
public class Program {
|
public class Program {
|
||||||
static private int EventHandler(MongooseEvent ev) {
|
static private int EventHandler(IntPtr conn_ptr, int ev) {
|
||||||
if (ev.type != 1) {
|
MongooseConnection conn = (MongooseConnection)
|
||||||
return 0; // Mark as unhandled
|
System.Runtime.InteropServices.Marshal.PtrToStructure(
|
||||||
}
|
conn_ptr , typeof(MongooseConnection));
|
||||||
|
|
||||||
MongooseRequestInfo request_info = (MongooseRequestInfo)
|
if (ev == 102) {
|
||||||
Marshal.PtrToStructure(ev.request_info, typeof(MongooseRequestInfo));
|
// MG_AUTH
|
||||||
|
return 1;
|
||||||
|
} else if (ev == 103) {
|
||||||
|
// MG_REQUEST
|
||||||
|
Mongoose.send_data(conn_ptr, "Hello from C#!\n");
|
||||||
|
Mongoose.send_data(conn_ptr, "URI: " + conn.uri + "\n");
|
||||||
|
Mongoose.send_data(conn_ptr, "HTTP Headers:\n");
|
||||||
|
|
||||||
if (request_info.uri != "/test") {
|
for (int i = 0; i < conn.num_headers; i++) {
|
||||||
return 0; // Mark as unhandled
|
IntPtr name = conn.http_headers[i].name;
|
||||||
}
|
IntPtr val = conn.http_headers[i].value;
|
||||||
|
System.Runtime.InteropServices.Marshal.PtrToStringAnsi(name);
|
||||||
Mongoose.write(ev.conn, "HTTP/1.1 200 OK\r\n\r\n");
|
Mongoose.send_data(conn_ptr, " " +
|
||||||
Mongoose.write(ev.conn, "Hello from C#!\n");
|
System.Runtime.InteropServices.Marshal.PtrToStringAnsi(name) + ": " +
|
||||||
|
System.Runtime.InteropServices.Marshal.PtrToStringAnsi(val) + "\n");
|
||||||
return 1; // Mark as handled
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void Main() {
|
static void Main() {
|
||||||
Mongoose web_server = new Mongoose(".", "9000",
|
Mongoose web_server = new Mongoose(".", "9001",
|
||||||
new MongooseEventHandler(EventHandler));
|
new MongooseEventHandler(EventHandler));
|
||||||
|
|
||||||
Console.WriteLine("Mongoose v." + web_server.version_ + " started.");
|
Console.WriteLine("Mongoose started, press Ctrl-C to exit.");
|
||||||
Console.WriteLine("Press enter to exit program.");
|
for (;;) {
|
||||||
|
web_server.poll(1000);
|
||||||
// Serve requests until user presses "enter" on a keyboard
|
}
|
||||||
Console.ReadLine();
|
|
||||||
web_server.stop();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+37
-62
@@ -9,85 +9,60 @@ using System.Runtime.InteropServices;
|
|||||||
[MarshalAs(UnmanagedType.LPTStr)] public IntPtr value;
|
[MarshalAs(UnmanagedType.LPTStr)] public IntPtr value;
|
||||||
};
|
};
|
||||||
|
|
||||||
// mongoose.h :: struct mg_request_info
|
// mongoose.h :: struct mg_connection
|
||||||
[StructLayout(LayoutKind.Sequential)] public struct MongooseRequestInfo {
|
[StructLayout(LayoutKind.Sequential)] public struct MongooseConnection {
|
||||||
[MarshalAs(UnmanagedType.LPTStr)] public string request_method;
|
[MarshalAs(UnmanagedType.LPTStr)] public string request_method;
|
||||||
[MarshalAs(UnmanagedType.LPTStr)] public string uri;
|
[MarshalAs(UnmanagedType.LPTStr)] public string uri;
|
||||||
[MarshalAs(UnmanagedType.LPTStr)] public string http_version;
|
[MarshalAs(UnmanagedType.LPTStr)] public string http_version;
|
||||||
[MarshalAs(UnmanagedType.LPTStr)] public string query_string;
|
[MarshalAs(UnmanagedType.LPTStr)] public string query_string;
|
||||||
[MarshalAs(UnmanagedType.LPTStr)] public string remote_user;
|
|
||||||
public int remote_ip;
|
[MarshalAs(UnmanagedType.ByValArray,SizeConst=48)] public char[] remote_ip;
|
||||||
public int remote_port;
|
[MarshalAs(UnmanagedType.LPTStr)] public string local_ip;
|
||||||
public int is_ssl;
|
[MarshalAs(UnmanagedType.U2)] public short remote_port;
|
||||||
[MarshalAs(UnmanagedType.ByValArray,SizeConst=64)]
|
[MarshalAs(UnmanagedType.U2)] public short local_port;
|
||||||
|
|
||||||
|
[MarshalAs(UnmanagedType.SysInt)] public int num_headers;
|
||||||
|
[MarshalAs(UnmanagedType.ByValArray,SizeConst=30)]
|
||||||
public MongooseHeader[] http_headers;
|
public MongooseHeader[] http_headers;
|
||||||
|
|
||||||
|
[MarshalAs(UnmanagedType.LPTStr)] public IntPtr content;
|
||||||
|
[MarshalAs(UnmanagedType.SysInt)] public int content_len;
|
||||||
|
|
||||||
|
[MarshalAs(UnmanagedType.SysInt)] public int is_websocket;
|
||||||
|
[MarshalAs(UnmanagedType.SysInt)] public int status_code;
|
||||||
|
[MarshalAs(UnmanagedType.SysInt)] public int wsbits;
|
||||||
};
|
};
|
||||||
|
|
||||||
[StructLayout(LayoutKind.Sequential)] public struct MongooseEvent {
|
public delegate int MongooseEventHandler(IntPtr c, int ev);
|
||||||
public int type;
|
|
||||||
public IntPtr user_data;
|
|
||||||
public IntPtr conn_data;
|
|
||||||
public IntPtr event_param;
|
|
||||||
public IntPtr conn;
|
|
||||||
public IntPtr request_info;
|
|
||||||
};
|
|
||||||
|
|
||||||
public delegate int MongooseEventHandlerN(ref MongooseEvent ev);
|
|
||||||
public delegate int MongooseEventHandler(MongooseEvent ev);
|
|
||||||
|
|
||||||
public class Mongoose {
|
public class Mongoose {
|
||||||
public const string dll_name_ = "mongoose";
|
public const string dll_ = "mongoose";
|
||||||
public string version_ = "??";
|
private IntPtr server_;
|
||||||
|
|
||||||
// These are here to store a ref to the callbacks
|
[DllImport(dll_)] private static extern IntPtr
|
||||||
// while they are over in unmanaged code, to prevent garbage collection.
|
mg_create_server(IntPtr user_data, MongooseEventHandler eh);
|
||||||
private event MongooseEventHandlerN delegates;
|
[DllImport(dll_)] private static extern int
|
||||||
|
mg_poll_server(IntPtr server, int milli);
|
||||||
private IntPtr ctx_;
|
[DllImport(dll_)] private static extern IntPtr
|
||||||
|
mg_set_option(IntPtr server, string name, string value);
|
||||||
[DllImport(dll_name_)] private static extern
|
[DllImport(dll_)] public static extern int
|
||||||
IntPtr mg_start([MarshalAs(UnmanagedType.LPArray,
|
mg_send_data(IntPtr conn, string data, int length);
|
||||||
ArraySubType=UnmanagedType.LPTStr)] string[] options,
|
|
||||||
MongooseEventHandlerN callback,
|
|
||||||
IntPtr user_data);
|
|
||||||
[DllImport(dll_name_)] private static extern void mg_stop(IntPtr ctx);
|
|
||||||
[DllImport(dll_name_)] private static extern IntPtr mg_version();
|
|
||||||
[DllImport(dll_name_)] public static extern int mg_write(IntPtr conn,
|
|
||||||
string data, int length);
|
|
||||||
|
|
||||||
public Mongoose(string document_root,
|
public Mongoose(string document_root,
|
||||||
string listening_ports,
|
string listening_port,
|
||||||
MongooseEventHandler event_handler) {
|
MongooseEventHandler event_handler) {
|
||||||
version_ = Marshal.PtrToStringAnsi(mg_version());
|
server_ = mg_create_server(IntPtr.Zero, event_handler);
|
||||||
|
mg_set_option(server_, "document_root", document_root);
|
||||||
string[] options = {
|
mg_set_option(server_, "listening_port", listening_port);
|
||||||
"document_root", document_root,
|
|
||||||
"listening_ports", listening_ports,
|
|
||||||
null
|
|
||||||
};
|
|
||||||
|
|
||||||
MongooseEventHandlerN cb = delegate(ref MongooseEvent ev) {
|
|
||||||
return event_handler(ev);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Prevent garbage collection
|
|
||||||
delegates += cb;
|
|
||||||
|
|
||||||
ctx_ = mg_start(options, cb, IntPtr.Zero);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int write(IntPtr conn, string data) {
|
public static int send_data(IntPtr conn, string data) {
|
||||||
return mg_write(conn, data, data.Length);
|
return mg_send_data(conn, data, data.Length);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stop() {
|
public void poll(int milli) {
|
||||||
if (this.ctx_ != IntPtr.Zero) {
|
mg_poll_server(server_, milli);
|
||||||
mg_stop(this.ctx_);
|
|
||||||
}
|
|
||||||
this.ctx_ = IntPtr.Zero;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
~Mongoose() {
|
// TODO: add destructor and call mg_destroy_server()
|
||||||
stop();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user