Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongoose
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
esp
mongoose
Commits
f7fcfa6a
Commit
f7fcfa6a
authored
Mar 12, 2014
by
Sergey Lyubka
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed C# example to work with 5.3
parent
293c5e58
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
71 additions
and
90 deletions
+71
-90
example.cs
examples/c#/example.cs
+30
-24
mongoose.cs
examples/c#/mongoose.cs
+41
-66
No files found.
examples/c#/example.cs
View file @
f7fcfa6a
...
...
@@ -2,36 +2,42 @@
// https://github.com/cesanta/mongoose
using
System
;
using
System.Runtime.InteropServices
;
public
class
Program
{
static
private
int
EventHandler
(
MongooseEvent
ev
)
{
if
(
ev
.
type
!=
1
)
{
return
0
;
// Mark as unhandled
static
private
int
EventHandler
(
IntPtr
conn_ptr
,
int
ev
)
{
MongooseConnection
conn
=
(
MongooseConnection
)
System
.
Runtime
.
InteropServices
.
Marshal
.
PtrToStructure
(
conn_ptr
,
typeof
(
MongooseConnection
));
if
(
ev
==
102
)
{
// 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"
);
for
(
int
i
=
0
;
i
<
conn
.
num_headers
;
i
++)
{
IntPtr
name
=
conn
.
http_headers
[
i
].
name
;
IntPtr
val
=
conn
.
http_headers
[
i
].
value
;
System
.
Runtime
.
InteropServices
.
Marshal
.
PtrToStringAnsi
(
name
);
Mongoose
.
send_data
(
conn_ptr
,
" "
+
System
.
Runtime
.
InteropServices
.
Marshal
.
PtrToStringAnsi
(
name
)
+
": "
+
System
.
Runtime
.
InteropServices
.
Marshal
.
PtrToStringAnsi
(
val
)
+
"\n"
);
}
MongooseRequestInfo
request_info
=
(
MongooseRequestInfo
)
Marshal
.
PtrToStructure
(
ev
.
request_info
,
typeof
(
MongooseRequestInfo
));
if
(
request_info
.
uri
!=
"/test"
)
{
return
0
;
// Mark as unhandled
return
1
;
}
Mongoose
.
write
(
ev
.
conn
,
"HTTP/1.1 200 OK\r\n\r\n"
);
Mongoose
.
write
(
ev
.
conn
,
"Hello from C#!\n"
);
return
1
;
// Mark as handled
return
0
;
}
static
void
Main
()
{
Mongoose
web_server
=
new
Mongoose
(
"."
,
"900
0
"
,
Mongoose
web_server
=
new
Mongoose
(
"."
,
"900
1
"
,
new
MongooseEventHandler
(
EventHandler
));
Console
.
WriteLine
(
"Mongoose v."
+
web_server
.
version_
+
" started."
);
Console
.
WriteLine
(
"Press enter to exit program."
);
// Serve requests until user presses "enter" on a keyboard
Console
.
ReadLine
();
web_server
.
stop
();
Console
.
WriteLine
(
"Mongoose started, press Ctrl-C to exit."
);
for
(;;)
{
web_server
.
poll
(
1000
);
}
}
}
examples/c#/mongoose.cs
View file @
f7fcfa6a
...
...
@@ -9,85 +9,60 @@ using System.Runtime.InteropServices;
[
MarshalAs
(
UnmanagedType
.
LPTStr
)]
public
IntPtr
value
;
};
// mongoose.h :: struct mg_
request_info
[StructLayout(LayoutKind.Sequential)]
public
struct
Mongoose
RequestInfo
{
// mongoose.h :: struct mg_
connection
[StructLayout(LayoutKind.Sequential)]
public
struct
Mongoose
Connection
{
[
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
;
public
int
remote_ip
;
public
int
remote_port
;
public
int
is_ssl
;
[
MarshalAs
(
UnmanagedType
.
ByValArray
,
SizeConst
=
64
)]
public
MongooseHeader
[]
http_headers
;
};
[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
;
};
[
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
;
public
delegate
int
MongooseEventHandlerN
(
ref
MongooseEvent
ev
);
public
delegate
int
MongooseEventHandler
(
MongooseEvent
ev
);
[
MarshalAs
(
UnmanagedType
.
SysInt
)]
public
int
num_headers
;
[
MarshalAs
(
UnmanagedType
.
ByValArray
,
SizeConst
=
30
)]
public
MongooseHeader
[]
http_headers
;
public
class
Mongoose
{
public
const
string
dll_name_
=
"mongoose"
;
public
string
version_
=
"??"
;
[
MarshalAs
(
UnmanagedType
.
LPTStr
)]
public
IntPtr
content
;
[
MarshalAs
(
UnmanagedType
.
SysInt
)]
public
int
content_len
;
// These are here to store a ref to the callbacks
// while they are over in unmanaged code, to prevent garbage collection.
private
event
MongooseEventHandlerN
delegates
;
[
MarshalAs
(
UnmanagedType
.
SysInt
)]
public
int
is_websocket
;
[
MarshalAs
(
UnmanagedType
.
SysInt
)]
public
int
status_code
;
[
MarshalAs
(
UnmanagedType
.
SysInt
)]
public
int
wsbits
;
};
private
IntPtr
ctx_
;
public
delegate
int
MongooseEventHandler
(
IntPtr
c
,
int
ev
)
;
[
DllImport
(
dll_name_
)]
private
static
extern
IntPtr
mg_start
([
MarshalAs
(
UnmanagedType
.
LPArray
,
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
class
Mongoose
{
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
);
public
Mongoose
(
string
document_root
,
string
listening_port
s
,
string
listening_port
,
MongooseEventHandler
event_handler
)
{
version_
=
Marshal
.
PtrToStringAnsi
(
mg_version
());
string
[]
options
=
{
"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
);
server_
=
mg_create_server
(
IntPtr
.
Zero
,
event_handler
);
mg_set_option
(
server_
,
"document_root"
,
document_root
);
mg_set_option
(
server_
,
"listening_port"
,
listening_port
);
}
public
static
int
write
(
IntPtr
conn
,
string
data
)
{
return
mg_
write
(
conn
,
data
,
data
.
Length
);
public
static
int
send_data
(
IntPtr
conn
,
string
data
)
{
return
mg_
send_data
(
conn
,
data
,
data
.
Length
);
}
public
void
stop
()
{
if
(
this
.
ctx_
!=
IntPtr
.
Zero
)
{
mg_stop
(
this
.
ctx_
);
}
this
.
ctx_
=
IntPtr
.
Zero
;
public
void
poll
(
int
milli
)
{
mg_poll_server
(
server_
,
milli
);
}
~
Mongoose
()
{
stop
();
}
// TODO: add destructor and call mg_destroy_server()
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment