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
6996ef6f
Commit
6996ef6f
authored
Aug 29, 2009
by
Martin Sustrik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
improved error handling
parent
cb09c695
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
61 additions
and
87 deletions
+61
-87
zmq.hpp
include/zmq.hpp
+54
-73
ip.cpp
src/ip.cpp
+2
-1
tcp_listener.cpp
src/tcp_listener.cpp
+4
-5
tcp_listener.hpp
src/tcp_listener.hpp
+1
-4
zmq_listener.cpp
src/zmq_listener.cpp
+0
-4
No files found.
include/zmq.hpp
View file @
6996ef6f
...
...
@@ -24,6 +24,7 @@
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <exception>
namespace
zmq
...
...
@@ -38,36 +39,21 @@ namespace zmq
message_delimiter
=
1
<<
ZMQ_DELIMITER
};
class
no_memory
:
public
std
::
exception
// The class masquerades POSIX-style errno error as a C++ exception.
class
error_t
:
public
std
::
exception
{
virtual
const
char
*
what
()
{
return
"Out of memory"
;
}
};
public
:
class
invalid_argument
:
public
std
::
exception
{
virtual
const
char
*
what
()
{
return
"Invalid argument"
;
}
};
error_t
()
:
errnum
(
errno
)
{}
class
too_many_threads
:
public
std
::
exception
virtual
const
char
*
what
()
const
throw
()
{
virtual
const
char
*
what
()
{
return
"Too many threads"
;
return
strerror
(
errnum
);
}
};
class
address_in_use
:
public
std
::
exception
{
virtual
const
char
*
what
()
{
return
"Address in use"
;
}
private
:
int
errnum
;
};
// A message. Caution: Don't change the body of the message once you've
...
...
@@ -84,10 +70,8 @@ namespace zmq
inline
message_t
(
size_t
size_
=
0
)
{
int
rc
=
zmq_msg_init_size
(
this
,
size_
);
if
(
rc
==
-
1
)
{
assert
(
errno
==
ENOMEM
);
throw
no_memory
();
}
if
(
rc
!=
0
)
throw
error_t
();
}
// Creates message from the supplied buffer. 0MQ takes care of
...
...
@@ -98,14 +82,16 @@ namespace zmq
free_fn
*
ffn_
)
{
int
rc
=
zmq_msg_init_data
(
this
,
data_
,
size_
,
ffn_
);
assert
(
rc
==
0
);
if
(
rc
!=
0
)
throw
error_t
();
}
// Destroys the message.
inline
~
message_t
()
{
int
rc
=
zmq_msg_close
(
this
);
assert
(
rc
==
0
);
if
(
rc
!=
0
)
throw
error_t
();
}
// Destroys old content of the message and allocates buffer for the
...
...
@@ -114,12 +100,11 @@ namespace zmq
inline
void
rebuild
(
size_t
size_
)
{
int
rc
=
zmq_msg_close
(
this
);
assert
(
rc
==
0
);
if
(
rc
!=
0
)
throw
error_t
();
rc
=
zmq_msg_init_size
(
this
,
size_
);
if
(
rc
==
-
1
)
{
assert
(
errno
==
ENOMEM
);
throw
no_memory
();
}
if
(
rc
!=
0
)
throw
error_t
();
}
// Same as above, however, the message is rebuilt from the supplied
...
...
@@ -128,9 +113,11 @@ namespace zmq
inline
void
rebuild
(
void
*
data_
,
size_t
size_
,
free_fn
*
ffn_
)
{
int
rc
=
zmq_msg_close
(
this
);
assert
(
rc
==
0
);
if
(
rc
!=
0
)
throw
error_t
();
rc
=
zmq_msg_init_data
(
this
,
data_
,
size_
,
ffn_
);
assert
(
rc
==
0
);
if
(
rc
!=
0
)
throw
error_t
();
}
// Moves the message content from one message to the another. If the
...
...
@@ -140,7 +127,8 @@ namespace zmq
inline
void
move_to
(
message_t
*
msg_
)
{
int
rc
=
zmq_msg_move
(
this
,
(
zmq_msg_t
*
)
msg_
);
assert
(
rc
==
0
);
if
(
rc
!=
0
)
throw
error_t
();
}
// Copies the message content from one message to the another. If the
...
...
@@ -149,7 +137,8 @@ namespace zmq
inline
void
copy_to
(
message_t
*
msg_
)
{
int
rc
=
zmq_msg_copy
(
this
,
(
zmq_msg_t
*
)
msg_
);
assert
(
rc
==
0
);
if
(
rc
!=
0
)
throw
error_t
();
}
// Returns message type.
...
...
@@ -187,10 +176,8 @@ namespace zmq
inline
context_t
(
int
app_threads_
,
int
io_threads_
)
{
ptr
=
zmq_init
(
app_threads_
,
io_threads_
);
if
(
ptr
==
NULL
)
{
assert
(
errno
==
EINVAL
);
throw
invalid_argument
();
}
if
(
ptr
==
NULL
)
throw
error_t
();
}
inline
~
context_t
()
...
...
@@ -215,70 +202,64 @@ namespace zmq
inline
socket_t
(
context_t
&
context_
,
int
type_
=
0
)
{
ptr
=
zmq_socket
(
context_
.
ptr
,
type_
);
if
(
ptr
==
NULL
)
{
assert
(
errno
==
EMFILE
||
errno
==
EINVAL
);
if
(
errno
==
EMFILE
)
throw
too_many_threads
();
else
throw
invalid_argument
();
}
if
(
ptr
==
NULL
)
throw
error_t
();
}
inline
~
socket_t
()
{
int
rc
=
zmq_close
(
ptr
);
assert
(
rc
==
0
);
if
(
rc
!=
0
)
throw
error_t
();
}
inline
void
setsockopt
(
int
option_
,
const
void
*
optval_
,
size_t
optvallen_
)
{
int
rc
=
zmq_setsockopt
(
ptr
,
option_
,
optval_
,
optvallen_
);
assert
(
rc
==
0
);
if
(
rc
!=
0
)
throw
error_t
();
}
inline
void
bind
(
const
char
*
addr_
)
{
int
rc
=
zmq_bind
(
ptr
,
addr_
);
if
(
rc
==
-
1
)
{
assert
(
errno
==
EINVAL
||
errno
==
EADDRINUSE
);
if
(
errno
==
EINVAL
)
throw
invalid_argument
();
else
throw
address_in_use
();
}
if
(
rc
!=
0
)
throw
error_t
();
}
inline
void
connect
(
const
char
*
addr_
)
{
int
rc
=
zmq_connect
(
ptr
,
addr_
);
if
(
rc
==
-
1
)
{
assert
(
errno
==
EINVAL
||
errno
==
EADDRINUSE
);
if
(
errno
==
EINVAL
)
throw
invalid_argument
();
else
throw
address_in_use
();
}
if
(
rc
!=
0
)
throw
error_t
();
}
inline
int
send
(
message_t
&
msg_
,
int
flags_
=
0
)
inline
bool
send
(
message_t
&
msg_
,
int
flags_
=
0
)
{
int
rc
=
zmq_send
(
ptr
,
&
msg_
,
flags_
);
assert
(
rc
==
0
||
(
rc
==
-
1
&&
errno
==
EAGAIN
));
return
rc
;
if
(
rc
==
0
)
return
true
;
if
(
rc
==
-
1
&&
errno
==
EAGAIN
)
return
false
;
throw
error_t
();
}
inline
void
flush
()
{
int
rc
=
zmq_flush
(
ptr
);
assert
(
rc
==
0
);
if
(
rc
!=
0
)
throw
error_t
();
}
inline
int
recv
(
message_t
*
msg_
,
int
flags_
=
0
)
inline
bool
recv
(
message_t
*
msg_
,
int
flags_
=
0
)
{
int
rc
=
zmq_recv
(
ptr
,
msg_
,
flags_
);
assert
(
rc
==
0
||
(
rc
==
-
1
&&
errno
==
EAGAIN
));
return
rc
;
if
(
rc
==
0
)
return
true
;
if
(
rc
==
-
1
&&
errno
==
EAGAIN
)
return
false
;
throw
error_t
();
}
private
:
...
...
src/ip.cpp
View file @
6996ef6f
...
...
@@ -285,7 +285,8 @@ int zmq::resolve_ip_hostname (sockaddr_in *addr_, const char *hostname_)
// Separate the hostname.
std
::
string
hostname
(
hostname_
,
delimiter
-
hostname_
);
// Resolve host name.
// Resolve host name. Some of the error info is lost in case of error,
// however, there's no way to report EAI errors via errno.
addrinfo
req
;
memset
(
&
req
,
0
,
sizeof
(
req
));
req
.
ai_family
=
AF_INET
;
...
...
src/tcp_listener.cpp
View file @
6996ef6f
...
...
@@ -55,11 +55,10 @@ zmq::tcp_listener_t::~tcp_listener_t ()
int
zmq
::
tcp_listener_t
::
set_address
(
const
char
*
addr_
)
{
// Convert the interface into sockaddr_in structure.
return
resolve_ip_interface
(
&
addr
,
addr_
);
}
int
rc
=
resolve_ip_interface
(
&
addr
,
addr_
);
if
(
rc
!=
0
)
return
rc
;
int
zmq
::
tcp_listener_t
::
open
()
{
// Create a listening socket.
s
=
socket
(
AF_INET
,
SOCK_STREAM
,
IPPROTO_TCP
);
if
(
s
==
-
1
)
...
...
@@ -67,7 +66,7 @@ int zmq::tcp_listener_t::open ()
// Allow reusing of the address.
int
flag
=
1
;
int
rc
=
setsockopt
(
s
,
SOL_SOCKET
,
SO_REUSEADDR
,
&
flag
,
sizeof
(
int
));
rc
=
setsockopt
(
s
,
SOL_SOCKET
,
SO_REUSEADDR
,
&
flag
,
sizeof
(
int
));
errno_assert
(
rc
==
0
);
// Set the non-blocking flag.
...
...
src/tcp_listener.hpp
View file @
6996ef6f
...
...
@@ -35,14 +35,11 @@ namespace zmq
tcp_listener_t
();
~
tcp_listener_t
();
// S
et up the address to listen on
. Address is in
// S
tart listening on the interface
. Address is in
// <interface-name>:<port-number> format. Interface name may be '*'
// to bind to all the interfaces.
int
set_address
(
const
char
*
addr_
);
// Open TCP listining socket.
int
open
();
// Close the listening socket.
int
close
();
...
...
src/zmq_listener.cpp
View file @
6996ef6f
...
...
@@ -41,10 +41,6 @@ int zmq::zmq_listener_t::set_address (const char *addr_)
void
zmq
::
zmq_listener_t
::
process_plug
()
{
// Open the listening socket.
int
rc
=
tcp_listener
.
open
();
zmq_assert
(
rc
==
0
);
// Start polling for incoming connections.
handle
=
add_fd
(
tcp_listener
.
get_fd
());
set_pollin
(
handle
);
...
...
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