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
Hide 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 @@
...
@@ -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
;
if
(
request_info
.
uri
!=
"/test"
)
{
}
else
if
(
ev
==
103
)
{
return
0
;
// Mark as unhandled
// MG_REQUEST
}
Mongoose
.
send_data
(
conn_ptr
,
"Hello from C#!\n"
);
Mongoose
.
send_data
(
conn_ptr
,
"URI: "
+
conn
.
uri
+
"\n"
);
Mongoose
.
write
(
ev
.
conn
,
"HTTP/1.1 200 OK\r\n\r\n"
);
Mongoose
.
send_data
(
conn_ptr
,
"HTTP Headers:\n"
);
Mongoose
.
write
(
ev
.
conn
,
"Hello from C#!\n"
);
for
(
int
i
=
0
;
i
<
conn
.
num_headers
;
i
++)
{
return
1
;
// Mark as handled
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"
);
}
return
1
;
}
return
0
;
}
}
static
void
Main
()
{
static
void
Main
()
{
Mongoose
web_server
=
new
Mongoose
(
"."
,
"900
0
"
,
Mongoose
web_server
=
new
Mongoose
(
"."
,
"900
1
"
,
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
();
}
}
}
}
examples/c#/mongoose.cs
View file @
f7fcfa6a
...
@@ -9,85 +9,60 @@ using System.Runtime.InteropServices;
...
@@ -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
Mongoose
RequestInfo
{
[StructLayout(LayoutKind.Sequential)]
public
struct
Mongoose
Connection
{
[
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
;
public
int
remote_port
;
public
int
is_ssl
;
[
MarshalAs
(
UnmanagedType
.
ByValArray
,
SizeConst
=
64
)]
public
MongooseHeader
[]
http_headers
;
};
[StructLayout(LayoutKind.Sequential)]
public
struct
MongooseEvent
{
[
MarshalAs
(
UnmanagedType
.
ByValArray
,
SizeConst
=
48
)]
public
char
[]
remote_ip
;
public
int
type
;
[
MarshalAs
(
UnmanagedType
.
LPTStr
)]
public
string
local_ip
;
public
IntPtr
user_data
;
[
MarshalAs
(
UnmanagedType
.
U2
)]
public
short
remote_port
;
public
IntPtr
conn_data
;
[
MarshalAs
(
UnmanagedType
.
U2
)]
public
short
local_port
;
public
IntPtr
event_param
;
public
IntPtr
conn
;
[
MarshalAs
(
UnmanagedType
.
SysInt
)]
public
int
num_headers
;
public
IntPtr
request_info
;
[
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
;
};
};
public
delegate
int
MongooseEventHandlerN
(
ref
MongooseEvent
ev
);
public
delegate
int
MongooseEventHandler
(
IntPtr
c
,
int
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_port
s
,
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
()
{
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