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
c68df310
Commit
c68df310
authored
Jul 12, 2013
by
Sergey Lyubka
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added mg_websocket_write(), per morgan3d pull request
parent
70ce6c5a
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
69 additions
and
28 deletions
+69
-28
websocket.c
examples/websocket.c
+5
-28
mongoose.c
mongoose.c
+42
-0
mongoose.h
mongoose.h
+22
-0
No files found.
examples/websocket.c
View file @
c68df310
...
...
@@ -6,10 +6,8 @@
#include "mongoose.h"
static
void
websocket_ready_handler
(
struct
mg_connection
*
conn
)
{
unsigned
char
buf
[
40
];
buf
[
0
]
=
0x81
;
buf
[
1
]
=
snprintf
((
char
*
)
buf
+
2
,
sizeof
(
buf
)
-
2
,
"%s"
,
"server ready"
);
mg_write
(
conn
,
buf
,
2
+
buf
[
1
]);
static
const
char
*
message
=
"server ready"
;
mg_websocket_write
(
conn
,
WEBSOCKET_OPCODE_TEXT
,
message
,
strlen
(
message
));
}
// Arguments:
...
...
@@ -18,33 +16,12 @@ static void websocket_ready_handler(struct mg_connection *conn) {
// data, data_len: payload data. Mask, if any, is already applied.
static
int
websocket_data_handler
(
struct
mg_connection
*
conn
,
int
flags
,
char
*
data
,
size_t
data_len
)
{
unsigned
char
reply
[
200
];
size_t
i
;
(
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
);
(
void
)
flags
;
// Unused
mg_websocket_write
(
conn
,
WEBSOCKET_OPCODE_TEXT
,
data
,
data_len
);
// Returning zero means stoping websocket conversation.
// Close the conversation if client has sent us "exit" string.
return
memcmp
(
reply
+
2
,
"exit"
,
4
);
return
memcmp
(
data
,
"exit"
,
4
);
}
int
main
(
void
)
{
...
...
mongoose.c
View file @
c68df310
...
...
@@ -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
)
{
const
char
*
version
=
mg_get_header
(
conn
,
"Sec-WebSocket-Version"
);
if
(
version
==
NULL
||
strcmp
(
version
,
"13"
)
!=
0
)
{
...
...
mongoose.h
View file @
c68df310
...
...
@@ -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
);
// 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.
#undef PRINTF_FORMAT_STRING
#if _MSC_VER >= 1400
...
...
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