Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
mongoose
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Admin message
Gitlab has been updated. More info
here
and
there
.
Show more breadcrumbs
Ganil-acq
GANILinux
linux-service
library
mongoose
Commits
f9f623bd
Commit
f9f623bd
authored
11 years ago
by
Sergey Lyubka
Browse files
Options
Downloads
Patches
Plain Diff
c# example synced to the current version, and tested
parent
fe95707b
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
examples/c#/example.cs
+19
-9
19 additions, 9 deletions
examples/c#/example.cs
examples/c#/mongoose.cs
+37
-62
37 additions, 62 deletions
examples/c#/mongoose.cs
with
56 additions
and
71 deletions
examples/c#/example.cs
+
19
−
9
View file @
f9f623bd
...
@@ -5,23 +5,33 @@ using System;
...
@@ -5,23 +5,33 @@ using System;
using
System.Runtime.InteropServices
;
using
System.Runtime.InteropServices
;
public
class
Program
{
public
class
Program
{
static
private
int
RequestHandler
(
MongooseConnection
conn
)
{
static
private
int
EventHandler
(
MongooseEvent
ev
)
{
return
0
;
if
(
ev
.
type
!=
1
)
{
/*
return
0
;
// Mark as unhandled
conn.write("HTTP/1.1 200 OK\r\n\r\n");
}
conn.write("Hello from C#!\n");
conn.write("Your user-agent is: " + conn.get_header("User-Agent") + "\n");
MongooseRequestInfo
request_info
=
(
MongooseRequestInfo
)
*/
Marshal
.
PtrToStructure
(
ev
.
request_info
,
typeof
(
MongooseRequestInfo
));
if
(
request_info
.
uri
!=
"/test"
)
{
return
0
;
// Mark as unhandled
}
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
}
}
static
void
Main
()
{
static
void
Main
()
{
Mongoose
web_server
=
new
Mongoose
(
"
c:\\
"
,
"
808
0"
,
Mongoose
web_server
=
new
Mongoose
(
"
.
"
,
"
900
0"
,
new
Mongoose
BeginRequest
(
Reques
tHandler
));
new
Mongoose
EventHandler
(
Even
tHandler
));
Console
.
WriteLine
(
"Mongoose v."
+
web_server
.
version_
+
" started."
);
Console
.
WriteLine
(
"Mongoose v."
+
web_server
.
version_
+
" started."
);
Console
.
WriteLine
(
"Press enter to exit program."
);
Console
.
WriteLine
(
"Press enter to exit program."
);
// Serve requests until user presses "enter" on a keyboard
// Serve requests until user presses "enter" on a keyboard
Console
.
ReadLine
();
Console
.
ReadLine
();
web_server
.
stop
();
}
}
}
}
This diff is collapsed.
Click to expand it.
examples/c#/mongoose.cs
+
37
−
62
View file @
f9f623bd
...
@@ -5,60 +5,60 @@ using System;
...
@@ -5,60 +5,60 @@ using System;
using
System.Runtime.InteropServices
;
using
System.Runtime.InteropServices
;
[
StructLayout
(
LayoutKind
.
Sequential
)]
public
struct
MongooseHeader
{
[
StructLayout
(
LayoutKind
.
Sequential
)]
public
struct
MongooseHeader
{
public
IntPtr
name
;
// Using IntPtr here because if we use strings here,
[
MarshalAs
(
UnmanagedType
.
LPTStr
)]
public
IntPtr
name
;
public
IntPtr
value
;
// it won't be properly marshalled.
[
MarshalAs
(
UnmanagedType
.
LPTStr
)]
public
IntPtr
value
;
};
};
// mongoose.h :: struct mg_request_info
// mongoose.h :: struct mg_request_info
[
StructLayout
(
LayoutKind
.
Sequential
)]
public
struct
MongooseRequestInfo
{
[
StructLayout
(
LayoutKind
.
Sequential
)]
public
struct
MongooseRequestInfo
{
public
string
request_method
;
[
MarshalAs
(
UnmanagedType
.
LPTStr
)]
public
string
request_method
;
public
string
uri
;
[
MarshalAs
(
UnmanagedType
.
LPTStr
)]
public
string
uri
;
public
string
http_version
;
[
MarshalAs
(
UnmanagedType
.
LPTStr
)]
public
string
http_version
;
public
string
query_string
;
[
MarshalAs
(
UnmanagedType
.
LPTStr
)]
public
string
query_string
;
public
string
remote_user
;
[
MarshalAs
(
UnmanagedType
.
LPTStr
)]
public
string
remote_user
;
public
int
remote_ip
;
public
int
remote_ip
;
public
int
remote_port
;
public
int
remote_port
;
public
int
is_ssl
;
public
int
is_ssl
;
public
IntPtr
user_data
;
public
IntPtr
conn_data
;
[
MarshalAs
(
UnmanagedType
.
ByValArray
,
SizeConst
=
64
)]
[
MarshalAs
(
UnmanagedType
.
ByValArray
,
SizeConst
=
64
)]
public
MongooseHeader
[]
http_headers
;
public
MongooseHeader
[]
http_headers
;
};
};
[
UnmanagedFunctionPointer
(
CallingConvention
.
StdCall
)]
public
delegate
[
StructLayout
(
LayoutKind
.
Sequential
)]
public
struct
MongooseEvent
{
int
MongooseBeginRequest_native
(
IntPtr
conn
);
public
int
type
;
public
delegate
int
MongooseBeginRequest
(
MongooseConnection
conn
);
public
IntPtr
user_data
;
public
IntPtr
conn_data
;
// mongoose.h :: struct mg_callbacks
public
IntPtr
event_param
;
[
StructLayout
(
LayoutKind
.
Sequential
)]
public
struct
MongooseCallbacks
{
public
IntPtr
conn
;
public
MongooseBeginRequest_native
begin_request
;
public
IntPtr
request_info
;
[
MarshalAs
(
UnmanagedType
.
ByValArray
,
SizeConst
=
20
)]
public
IntPtr
[]
other_callbacks
;
};
};
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_name_
=
"mongoose"
;
public
string
version_
=
"??"
;
public
string
version_
=
"??"
;
// These are here to store a ref to the callbacks
// These are here to store a ref to the callbacks
// while they are over in unmanaged code, to prevent garbage collection.
// while they are over in unmanaged code, to prevent garbage collection.
private
event
Mongoose
BeginRequest_native
delegates
;
private
event
Mongoose
EventHandlerN
delegates
;
private
IntPtr
ctx_
;
private
IntPtr
ctx_
;
[
DllImport
(
dll_name_
)]
private
static
extern
[
DllImport
(
dll_name_
)]
private
static
extern
IntPtr
mg_start
([
MarshalAs
(
UnmanagedType
.
LPArray
,
IntPtr
mg_start
([
MarshalAs
(
UnmanagedType
.
LPArray
,
ArraySubType
=
UnmanagedType
.
LPTStr
)]
string
[]
options
,
ArraySubType
=
UnmanagedType
.
LPTStr
)]
string
[]
options
,
ref
Mongoose
Callbacks
callback
s
,
Mongoose
EventHandlerN
callback
,
IntPtr
user_data
);
IntPtr
user_data
);
[
DllImport
(
dll_name_
)]
public
static
extern
IntPtr
mg_get_request_info
(
IntPtr
conn
);
[
DllImport
(
dll_name_
)]
private
static
extern
void
mg_stop
(
IntPtr
ctx
);
[
DllImport
(
dll_name_
)]
private
static
extern
void
mg_stop
(
IntPtr
ctx
);
[
DllImport
(
dll_name_
)]
private
static
extern
string
mg_version
();
[
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
,
string
listening_ports
,
public
Mongoose
(
string
document_root
,
MongooseBeginRequest
request_handler
)
{
string
listening_ports
,
version_
=
mg_version
();
MongooseEventHandler
event_handler
)
{
version_
=
Marshal
.
PtrToStringAnsi
(
mg_version
());
string
[]
options
=
{
string
[]
options
=
{
"document_root"
,
document_root
,
"document_root"
,
document_root
,
...
@@ -66,53 +66,28 @@ public class Mongoose {
...
@@ -66,53 +66,28 @@ public class Mongoose {
null
null
};
};
Mongoose
BeginRequest_native
begin_request_cb
=
delegate
(
IntPtr
conn
)
{
Mongoose
EventHandlerN
cb
=
delegate
(
ref
MongooseEvent
ev
)
{
return
reques
t_handler
(
new
MongooseConnection
(
conn
,
this
)
);
return
even
t_handler
(
ev
);
};
};
MongooseCallbacks
callbacks
=
new
MongooseCallbacks
();
callbacks
.
begin_request
=
begin_request_cb
;
// Prevent garbage collection
// Prevent garbage collection
delegates
+=
begin_request_
cb
;
delegates
+=
cb
;
ctx_
=
mg_start
(
options
,
ref
callbacks
,
IntPtr
.
Zero
);
ctx_
=
mg_start
(
options
,
cb
,
IntPtr
.
Zero
);
}
}
~
Mongoose
()
{
public
static
int
write
(
IntPtr
conn
,
string
data
)
{
return
mg_write
(
conn
,
data
,
data
.
Length
);
}
public
void
stop
()
{
if
(
this
.
ctx_
!=
IntPtr
.
Zero
)
{
if
(
this
.
ctx_
!=
IntPtr
.
Zero
)
{
mg_stop
(
this
.
ctx_
);
mg_stop
(
this
.
ctx_
);
}
}
this
.
ctx_
=
IntPtr
.
Zero
;
this
.
ctx_
=
IntPtr
.
Zero
;
}
}
}
public
class
MongooseConnection
{
~
Mongoose
()
{
public
Mongoose
mongoose
;
stop
();
private
IntPtr
conn
;
[
DllImport
(
Mongoose
.
dll_name_
)]
private
static
extern
string
mg_get_header
(
IntPtr
conn
,
string
name
);
[
DllImport
(
Mongoose
.
dll_name_
)]
private
static
extern
string
mg_get_var
(
IntPtr
conn
,
string
name
);
[
DllImport
(
Mongoose
.
dll_name_
)]
public
static
extern
int
mg_write
(
IntPtr
conn
,
string
data
,
int
length
);
public
MongooseConnection
(
IntPtr
conn_
,
Mongoose
mongoose_
)
{
mongoose
=
mongoose_
;
conn
=
conn_
;
}
public
string
get_header
(
string
header_name
)
{
return
mg_get_header
(
this
.
conn
,
header_name
);
}
public
string
get_var
(
string
header_name
)
{
string
s
=
mg_get_var
(
this
.
conn
,
header_name
);
string
copy
=
""
+
s
;
return
copy
;
}
public
int
write
(
string
data
)
{
return
mg_write
(
this
.
conn
,
data
,
data
.
Length
);
}
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment