mongoose/examples/c#/mongoose.cs

69 lines
2.5 KiB
C#
Raw Normal View History

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 {
[MarshalAs(UnmanagedType.LPTStr)] public IntPtr name;
[MarshalAs(UnmanagedType.LPTStr)] public IntPtr value;
2013-09-26 19:50:11 +08:00
};
2014-03-12 20:31:53 +08:00
// mongoose.h :: struct mg_connection
[StructLayout(LayoutKind.Sequential)] public struct MongooseConnection {
[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;
2013-09-26 19:50:11 +08:00
2014-03-12 20:31:53 +08:00
[MarshalAs(UnmanagedType.ByValArray,SizeConst=48)] public char[] remote_ip;
[MarshalAs(UnmanagedType.LPTStr)] public string local_ip;
[MarshalAs(UnmanagedType.U2)] public short remote_port;
[MarshalAs(UnmanagedType.U2)] public short local_port;
[MarshalAs(UnmanagedType.SysInt)] public int num_headers;
[MarshalAs(UnmanagedType.ByValArray,SizeConst=30)]
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;
2013-09-26 19:50:11 +08:00
};
2014-03-12 20:31:53 +08:00
public delegate int MongooseEventHandler(IntPtr c, int ev);
2013-09-26 19:50:11 +08:00
public class Mongoose {
2014-03-12 20:31:53 +08:00
public const string dll_ = "mongoose";
private IntPtr server_;
[DllImport(dll_)] private static extern IntPtr
mg_create_server(IntPtr user_data, MongooseEventHandler eh);
[DllImport(dll_)] private static extern int
mg_poll_server(IntPtr server, int milli);
[DllImport(dll_)] private static extern IntPtr
mg_set_option(IntPtr server, string name, string value);
[DllImport(dll_)] public static extern int
mg_send_data(IntPtr conn, string data, int length);
2013-09-26 19:50:11 +08:00
public Mongoose(string document_root,
2014-03-12 20:31:53 +08:00
string listening_port,
MongooseEventHandler event_handler) {
2014-03-12 20:31:53 +08:00
server_ = mg_create_server(IntPtr.Zero, event_handler);
mg_set_option(server_, "document_root", document_root);
mg_set_option(server_, "listening_port", listening_port);
2013-09-26 19:50:11 +08:00
}
2014-03-12 20:31:53 +08:00
public static int send_data(IntPtr conn, string data) {
return mg_send_data(conn, data, data.Length);
}
2014-03-12 20:31:53 +08:00
public void poll(int milli) {
mg_poll_server(server_, milli);
2013-09-26 19:50:11 +08:00
}
2014-03-12 20:31:53 +08:00
// TODO: add destructor and call mg_destroy_server()
2013-09-26 19:50:11 +08:00
}