Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
L
libzmq
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
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
libzmq
Commits
9be877c6
Commit
9be877c6
authored
Dec 08, 2009
by
Martin Sustrik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ZMQII-26: Use zero-copy for large messages
parent
bfef2fcd
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
44 additions
and
17 deletions
+44
-17
encoder.hpp
src/encoder.hpp
+23
-5
zmq_engine.cpp
src/zmq_engine.cpp
+13
-8
zmq_engine.hpp
src/zmq_engine.hpp
+8
-4
No files found.
src/encoder.hpp
View file @
9be877c6
...
@@ -24,6 +24,8 @@
...
@@ -24,6 +24,8 @@
#include <string.h>
#include <string.h>
#include <algorithm>
#include <algorithm>
#include <stdio.h>
namespace
zmq
namespace
zmq
{
{
...
@@ -44,17 +46,31 @@ namespace zmq
...
@@ -44,17 +46,31 @@ namespace zmq
// NULL, it is filled by offset of the first message in the batch.
// NULL, it is filled by offset of the first message in the batch.
// If there's no beginning of a message in the batch, offset is
// If there's no beginning of a message in the batch, offset is
// set to -1.
// set to -1.
inline
size_t
read
(
unsigned
char
*
data_
,
size_t
size_
,
inline
void
read
(
unsigned
char
**
data_
,
size_t
*
size_
,
int
*
offset_
=
NULL
)
int
*
offset_
=
NULL
)
{
{
int
offset
=
-
1
;
int
offset
=
-
1
;
size_t
pos
=
0
;
size_t
pos
=
0
;
while
(
pos
<
size_
)
{
while
(
pos
<
*
size_
)
{
// If we are able to fill whole buffer in a single go, let's
// use zero-copy. There's no disadvantage to it as we cannot
// stuck multiple messages into the buffer anyway.
if
(
pos
==
0
&&
to_write
>=
*
size_
)
{
*
data_
=
write_pos
;
write_pos
+=
*
size_
;
to_write
-=
*
size_
;
// TODO: manage beginning & offset here.
return
;
}
if
(
to_write
)
{
if
(
to_write
)
{
size_t
to_copy
=
std
::
min
(
to_write
,
size_
-
pos
);
size_t
to_copy
=
std
::
min
(
to_write
,
*
size_
-
pos
);
memcpy
(
data_
+
pos
,
write_pos
,
to_copy
);
memcpy
(
*
data_
+
pos
,
write_pos
,
to_copy
);
pos
+=
to_copy
;
pos
+=
to_copy
;
write_pos
+=
to_copy
;
write_pos
+=
to_copy
;
to_write
-=
to_copy
;
to_write
-=
to_copy
;
...
@@ -70,10 +86,12 @@ namespace zmq
...
@@ -70,10 +86,12 @@ namespace zmq
}
}
}
}
// Return offset of the first message in the buffer.
if
(
offset_
)
if
(
offset_
)
*
offset_
=
offset
;
*
offset_
=
offset
;
return
pos
;
// Return the size of the filled-in portion of the buffer.
*
size_
=
pos
;
}
}
protected
:
protected
:
...
...
src/zmq_engine.cpp
View file @
9be877c6
...
@@ -26,17 +26,19 @@
...
@@ -26,17 +26,19 @@
zmq
::
zmq_engine_t
::
zmq_engine_t
(
io_thread_t
*
parent_
,
fd_t
fd_
)
:
zmq
::
zmq_engine_t
::
zmq_engine_t
(
io_thread_t
*
parent_
,
fd_t
fd_
)
:
io_object_t
(
parent_
),
io_object_t
(
parent_
),
inbuf
(
NULL
),
insize
(
0
),
insize
(
0
),
inpos
(
0
),
inpos
(
0
),
outbuf
(
NULL
),
outsize
(
0
),
outsize
(
0
),
outpos
(
0
),
outpos
(
0
),
inout
(
NULL
)
inout
(
NULL
)
{
{
// Allocate read & write buffer.
// Allocate read & write buffer.
inbuf
=
(
unsigned
char
*
)
malloc
(
in_batch_size
);
inbuf
_storage
=
(
unsigned
char
*
)
malloc
(
in_batch_size
);
zmq_assert
(
inbuf
);
zmq_assert
(
inbuf
_storage
);
outbuf
=
(
unsigned
char
*
)
malloc
(
out_batch_size
);
outbuf
_storage
=
(
unsigned
char
*
)
malloc
(
out_batch_size
);
zmq_assert
(
outbuf
);
zmq_assert
(
outbuf
_storage
);
// Initialise the underlying socket.
// Initialise the underlying socket.
int
rc
=
tcp_socket
.
open
(
fd_
);
int
rc
=
tcp_socket
.
open
(
fd_
);
...
@@ -45,8 +47,8 @@ zmq::zmq_engine_t::zmq_engine_t (io_thread_t *parent_, fd_t fd_) :
...
@@ -45,8 +47,8 @@ zmq::zmq_engine_t::zmq_engine_t (io_thread_t *parent_, fd_t fd_) :
zmq
::
zmq_engine_t
::~
zmq_engine_t
()
zmq
::
zmq_engine_t
::~
zmq_engine_t
()
{
{
free
(
outbuf
);
free
(
outbuf
_storage
);
free
(
inbuf
);
free
(
inbuf
_storage
);
}
}
void
zmq
::
zmq_engine_t
::
plug
(
i_inout
*
inout_
)
void
zmq
::
zmq_engine_t
::
plug
(
i_inout
*
inout_
)
...
@@ -80,11 +82,12 @@ void zmq::zmq_engine_t::in_event ()
...
@@ -80,11 +82,12 @@ void zmq::zmq_engine_t::in_event ()
if
(
inpos
==
insize
)
{
if
(
inpos
==
insize
)
{
// Read as much data as possible to the read buffer.
// Read as much data as possible to the read buffer.
inbuf
=
inbuf_storage
;
insize
=
tcp_socket
.
read
(
inbuf
,
in_batch_size
);
insize
=
tcp_socket
.
read
(
inbuf
,
in_batch_size
);
inpos
=
0
;
inpos
=
0
;
// Check whether the peer has closed the connection.
// Check whether the peer has closed the connection.
if
(
insize
==
-
1
)
{
if
(
insize
==
(
size_t
)
-
1
)
{
insize
=
0
;
insize
=
0
;
error
();
error
();
return
;
return
;
...
@@ -111,7 +114,9 @@ void zmq::zmq_engine_t::out_event ()
...
@@ -111,7 +114,9 @@ void zmq::zmq_engine_t::out_event ()
// If write buffer is empty, try to read new data from the encoder.
// If write buffer is empty, try to read new data from the encoder.
if
(
outpos
==
outsize
)
{
if
(
outpos
==
outsize
)
{
outsize
=
encoder
.
read
(
outbuf
,
out_batch_size
);
outbuf
=
outbuf_storage
;
outsize
=
out_batch_size
;
encoder
.
read
(
&
outbuf
,
&
outsize
);
outpos
=
0
;
outpos
=
0
;
// If there is no data to send, stop polling for output.
// If there is no data to send, stop polling for output.
...
...
src/zmq_engine.hpp
View file @
9be877c6
...
@@ -20,6 +20,8 @@
...
@@ -20,6 +20,8 @@
#ifndef __ZMQ_ZMQ_ENGINE_HPP_INCLUDED__
#ifndef __ZMQ_ZMQ_ENGINE_HPP_INCLUDED__
#define __ZMQ_ZMQ_ENGINE_HPP_INCLUDED__
#define __ZMQ_ZMQ_ENGINE_HPP_INCLUDED__
#include <stddef.h>
#include "i_engine.hpp"
#include "i_engine.hpp"
#include "io_object.hpp"
#include "io_object.hpp"
#include "tcp_socket.hpp"
#include "tcp_socket.hpp"
...
@@ -54,13 +56,15 @@ namespace zmq
...
@@ -54,13 +56,15 @@ namespace zmq
tcp_socket_t
tcp_socket
;
tcp_socket_t
tcp_socket
;
handle_t
handle
;
handle_t
handle
;
unsigned
char
*
inbuf_storage
;
unsigned
char
*
inbuf
;
unsigned
char
*
inbuf
;
in
t
insize
;
size_
t
insize
;
in
t
inpos
;
size_
t
inpos
;
unsigned
char
*
outbuf_storage
;
unsigned
char
*
outbuf
;
unsigned
char
*
outbuf
;
in
t
outsize
;
size_
t
outsize
;
in
t
outpos
;
size_
t
outpos
;
i_inout
*
inout
;
i_inout
*
inout
;
...
...
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