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
c68df310
Commit
c68df310
authored
11 years ago
by
Sergey Lyubka
Browse files
Options
Downloads
Patches
Plain Diff
Added mg_websocket_write(), per morgan3d pull request
parent
70ce6c5a
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
examples/websocket.c
+5
-28
5 additions, 28 deletions
examples/websocket.c
mongoose.c
+42
-0
42 additions, 0 deletions
mongoose.c
mongoose.h
+22
-0
22 additions, 0 deletions
mongoose.h
with
69 additions
and
28 deletions
examples/websocket.c
+
5
−
28
View file @
c68df310
...
@@ -6,10 +6,8 @@
...
@@ -6,10 +6,8 @@
#include
"mongoose.h"
#include
"mongoose.h"
static
void
websocket_ready_handler
(
struct
mg_connection
*
conn
)
{
static
void
websocket_ready_handler
(
struct
mg_connection
*
conn
)
{
unsigned
char
buf
[
40
];
static
const
char
*
message
=
"server ready"
;
buf
[
0
]
=
0x81
;
mg_websocket_write
(
conn
,
WEBSOCKET_OPCODE_TEXT
,
message
,
strlen
(
message
));
buf
[
1
]
=
snprintf
((
char
*
)
buf
+
2
,
sizeof
(
buf
)
-
2
,
"%s"
,
"server ready"
);
mg_write
(
conn
,
buf
,
2
+
buf
[
1
]);
}
}
// Arguments:
// Arguments:
...
@@ -18,33 +16,12 @@ static void websocket_ready_handler(struct mg_connection *conn) {
...
@@ -18,33 +16,12 @@ static void websocket_ready_handler(struct mg_connection *conn) {
// data, data_len: payload data. Mask, if any, is already applied.
// data, data_len: payload data. Mask, if any, is already applied.
static
int
websocket_data_handler
(
struct
mg_connection
*
conn
,
int
flags
,
static
int
websocket_data_handler
(
struct
mg_connection
*
conn
,
int
flags
,
char
*
data
,
size_t
data_len
)
{
char
*
data
,
size_t
data_len
)
{
unsigned
char
reply
[
200
];
(
void
)
flags
;
// Unused
size_t
i
;
mg_websocket_write
(
conn
,
WEBSOCKET_OPCODE_TEXT
,
data
,
data_len
);
(
void
)
flags
;
printf
(
"rcv: [%.*s]
\n
"
,
(
int
)
data_len
,
data
);
// Truncate echoed message, to simplify output code.
if
(
data_len
>
125
)
{
data_len
=
125
;
}
// Prepare frame
reply
[
0
]
=
0x81
;
// text, FIN set
reply
[
1
]
=
data_len
;
// Copy message from request to reply, applying the mask if required.
for
(
i
=
0
;
i
<
data_len
;
i
++
)
{
reply
[
i
+
2
]
=
data
[
i
];
}
// Echo the message back to the client
mg_write
(
conn
,
reply
,
2
+
data_len
);
// Returning zero means stoping websocket conversation.
// Returning zero means stoping websocket conversation.
// Close the conversation if client has sent us "exit" string.
// Close the conversation if client has sent us "exit" string.
return
memcmp
(
reply
+
2
,
"exit"
,
4
);
return
memcmp
(
data
,
"exit"
,
4
);
}
}
int
main
(
void
)
{
int
main
(
void
)
{
...
...
This diff is collapsed.
Click to expand it.
mongoose.c
+
42
−
0
View file @
c68df310
...
@@ -4090,6 +4090,48 @@ static void read_websocket(struct mg_connection *conn) {
...
@@ -4090,6 +4090,48 @@ static void read_websocket(struct mg_connection *conn) {
}
}
}
}
int
mg_websocket_write
(
struct
mg_connection
*
conn
,
int
opcode
,
const
char
*
data
,
size_t
data_len
)
{
unsigned
char
*
copy
;
size_t
copy_len
=
0
;
int
retval
=
-
1
;
if
((
copy
=
(
unsigned
char
*
)
malloc
(
data_len
+
10
))
==
NULL
)
{
return
-
1
;
}
copy
[
0
]
=
0x80
+
(
opcode
&
0x0f
);
// Frame format: http://tools.ietf.org/html/rfc6455#section-5.2
if
(
data_len
<
126
)
{
// Inline 7-bit length field
copy
[
1
]
=
data_len
;
memcpy
(
copy
+
2
,
data
,
data_len
);
copy_len
=
2
+
data_len
;
}
else
if
(
data_len
<=
0xFFFF
)
{
// 16-bit length field
copy
[
1
]
=
126
;
*
(
uint16_t
*
)
(
copy
+
2
)
=
htons
(
data_len
);
memcpy
(
copy
+
4
,
data
,
data_len
);
copy_len
=
4
+
data_len
;
}
else
{
// 64-bit length field
copy
[
1
]
=
127
;
*
(
uint32_t
*
)
(
copy
+
2
)
=
htonl
((
uint64_t
)
data_len
>>
32
);
*
(
uint32_t
*
)
(
copy
+
6
)
=
htonl
(
data_len
&
0xffffffff
);
memcpy
(
copy
+
10
,
data
,
data_len
);
copy_len
=
10
+
data_len
;
}
// Not thread safe
if
(
copy_len
>
0
)
{
retval
=
mg_write
(
conn
,
copy
,
copy_len
);
}
free
(
copy
);
return
retval
;
}
static
void
handle_websocket_request
(
struct
mg_connection
*
conn
)
{
static
void
handle_websocket_request
(
struct
mg_connection
*
conn
)
{
const
char
*
version
=
mg_get_header
(
conn
,
"Sec-WebSocket-Version"
);
const
char
*
version
=
mg_get_header
(
conn
,
"Sec-WebSocket-Version"
);
if
(
version
==
NULL
||
strcmp
(
version
,
"13"
)
!=
0
)
{
if
(
version
==
NULL
||
strcmp
(
version
,
"13"
)
!=
0
)
{
...
...
This diff is collapsed.
Click to expand it.
mongoose.h
+
22
−
0
View file @
c68df310
...
@@ -208,6 +208,28 @@ struct mg_request_info *mg_get_request_info(struct mg_connection *);
...
@@ -208,6 +208,28 @@ struct mg_request_info *mg_get_request_info(struct mg_connection *);
int
mg_write
(
struct
mg_connection
*
,
const
void
*
buf
,
size_t
len
);
int
mg_write
(
struct
mg_connection
*
,
const
void
*
buf
,
size_t
len
);
// Send data to a websocket client wrapped in a websocket frame.
// It is unsafe to read/write to this connection from another thread.
// This function is available when mongoose is compiled with -DUSE_WEBSOCKET
//
// Return:
// 0 when the connection has been closed
// -1 on error
// >0 number of bytes written on success
int
mg_websocket_write
(
struct
mg_connection
*
conn
,
int
opcode
,
const
char
*
data
,
size_t
data_len
);
// Opcodes, from http://tools.ietf.org/html/rfc6455
enum
{
WEBSOCKET_OPCODE_CONTINUATION
=
0x0
,
WEBSOCKET_OPCODE_TEXT
=
0x1
,
WEBSOCKET_OPCODE_BINARY
=
0x2
,
WEBSOCKET_OPCODE_CONNECTION_CLOSE
=
0x8
,
WEBSOCKET_OPCODE_PING
=
0x9
,
WEBSOCKET_OPCODE_PONG
=
0xa
};
// Macros for enabling compiler-specific checks for printf-like arguments.
// Macros for enabling compiler-specific checks for printf-like arguments.
#undef PRINTF_FORMAT_STRING
#undef PRINTF_FORMAT_STRING
#if _MSC_VER >= 1400
#if _MSC_VER >= 1400
...
...
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