0
0
mirror of https://github.com/zeromq/libzmq.git synced 2025-01-17 12:58:12 +08:00

Multi-hop REQ/REP, part IX., pass the peer identity as far as socket_base_t

This commit is contained in:
Martin Sustrik 2010-02-13 15:30:03 +01:00
parent f5ce81f289
commit 4405250d93
10 changed files with 46 additions and 16 deletions

View File

@ -28,6 +28,10 @@ void zmq::deallocate_command (command_t *cmd_)
if (cmd_->args.attach.peer_identity) if (cmd_->args.attach.peer_identity)
free (cmd_->args.attach.peer_identity); free (cmd_->args.attach.peer_identity);
break; break;
case command_t::bind:
if (cmd_->args.bind.peer_identity)
free (cmd_->args.bind.peer_identity);
break;
default: default:
/* noop */; /* noop */;
} }

View File

@ -75,6 +75,8 @@ namespace zmq
struct { struct {
class reader_t *in_pipe; class reader_t *in_pipe;
class writer_t *out_pipe; class writer_t *out_pipe;
unsigned char peer_identity_size;
unsigned char *peer_identity;
} bind; } bind;
// Sent by pipe writer to inform dormant pipe reader that there // Sent by pipe writer to inform dormant pipe reader that there

View File

@ -89,7 +89,9 @@ void zmq::object_t::process_command (command_t &cmd_)
break; break;
case command_t::bind: case command_t::bind:
process_bind (cmd_.args.bind.in_pipe, cmd_.args.bind.out_pipe); process_bind (cmd_.args.bind.in_pipe, cmd_.args.bind.out_pipe,
blob_t (cmd_.args.bind.peer_identity,
cmd_.args.bind.peer_identity_size));
process_seqnum (); process_seqnum ();
break; break;
@ -198,7 +200,9 @@ void zmq::object_t::send_attach (session_t *destination_, i_engine *engine_,
cmd.args.attach.peer_identity = NULL; cmd.args.attach.peer_identity = NULL;
} }
else { else {
cmd.args.attach.peer_identity_size = peer_identity_.size (); zmq_assert (peer_identity_.size () <= 0xff);
cmd.args.attach.peer_identity_size =
(unsigned char) peer_identity_.size ();
cmd.args.attach.peer_identity = cmd.args.attach.peer_identity =
(unsigned char*) malloc (peer_identity_.size ()); (unsigned char*) malloc (peer_identity_.size ());
zmq_assert (cmd.args.attach.peer_identity_size); zmq_assert (cmd.args.attach.peer_identity_size);
@ -209,7 +213,8 @@ void zmq::object_t::send_attach (session_t *destination_, i_engine *engine_,
} }
void zmq::object_t::send_bind (socket_base_t *destination_, void zmq::object_t::send_bind (socket_base_t *destination_,
reader_t *in_pipe_, writer_t *out_pipe_, bool inc_seqnum_) reader_t *in_pipe_, writer_t *out_pipe_, const blob_t &peer_identity_,
bool inc_seqnum_)
{ {
if (inc_seqnum_) if (inc_seqnum_)
destination_->inc_seqnum (); destination_->inc_seqnum ();
@ -219,6 +224,20 @@ void zmq::object_t::send_bind (socket_base_t *destination_,
cmd.type = command_t::bind; cmd.type = command_t::bind;
cmd.args.bind.in_pipe = in_pipe_; cmd.args.bind.in_pipe = in_pipe_;
cmd.args.bind.out_pipe = out_pipe_; cmd.args.bind.out_pipe = out_pipe_;
if (peer_identity_.empty ()) {
cmd.args.bind.peer_identity_size = 0;
cmd.args.bind.peer_identity = NULL;
}
else {
zmq_assert (peer_identity_.size () <= 0xff);
cmd.args.bind.peer_identity_size =
(unsigned char) peer_identity_.size ();
cmd.args.bind.peer_identity =
(unsigned char*) malloc (peer_identity_.size ());
zmq_assert (cmd.args.bind.peer_identity_size);
memcpy (cmd.args.bind.peer_identity, peer_identity_.data (),
peer_identity_.size ());
}
send_command (cmd); send_command (cmd);
} }
@ -293,7 +312,8 @@ void zmq::object_t::process_attach (i_engine *engine_,
zmq_assert (false); zmq_assert (false);
} }
void zmq::object_t::process_bind (reader_t *in_pipe_, writer_t *out_pipe_) void zmq::object_t::process_bind (reader_t *in_pipe_, writer_t *out_pipe_,
const blob_t &peer_identity_)
{ {
zmq_assert (false); zmq_assert (false);
} }

View File

@ -69,7 +69,7 @@ namespace zmq
bool inc_seqnum_ = true); bool inc_seqnum_ = true);
void send_bind (class socket_base_t *destination_, void send_bind (class socket_base_t *destination_,
class reader_t *in_pipe_, class writer_t *out_pipe_, class reader_t *in_pipe_, class writer_t *out_pipe_,
bool inc_seqnum_ = true); const blob_t &peer_identity_, bool inc_seqnum_ = true);
void send_revive (class object_t *destination_); void send_revive (class object_t *destination_);
void send_pipe_term (class writer_t *destination_); void send_pipe_term (class writer_t *destination_);
void send_pipe_term_ack (class reader_t *destination_); void send_pipe_term_ack (class reader_t *destination_);
@ -86,7 +86,7 @@ namespace zmq
virtual void process_attach (struct i_engine *engine_, virtual void process_attach (struct i_engine *engine_,
const blob_t &peer_identity_); const blob_t &peer_identity_);
virtual void process_bind (class reader_t *in_pipe_, virtual void process_bind (class reader_t *in_pipe_,
class writer_t *out_pipe_); class writer_t *out_pipe_, const blob_t &peer_identity_);
virtual void process_revive (); virtual void process_revive ();
virtual void process_pipe_term (); virtual void process_pipe_term ();
virtual void process_pipe_term_ack (); virtual void process_pipe_term_ack ();

View File

@ -76,7 +76,7 @@ int zmq::options_t::setsockopt (int option_, const void *optval_,
return 0; return 0;
case ZMQ_IDENTITY: case ZMQ_IDENTITY:
identity.assign ((const char*) optval_, optvallen_); identity.assign ((const unsigned char*) optval_, optvallen_);
return 0; return 0;
case ZMQ_RATE: case ZMQ_RATE:

View File

@ -20,10 +20,9 @@
#ifndef __ZMQ_OPTIONS_HPP_INCLUDED__ #ifndef __ZMQ_OPTIONS_HPP_INCLUDED__
#define __ZMQ_OPTIONS_HPP_INCLUDED__ #define __ZMQ_OPTIONS_HPP_INCLUDED__
#include <string>
#include "stddef.h" #include "stddef.h"
#include "stdint.hpp" #include "stdint.hpp"
#include "blob.hpp"
namespace zmq namespace zmq
{ {
@ -38,7 +37,7 @@ namespace zmq
int64_t lwm; int64_t lwm;
int64_t swap; int64_t swap;
uint64_t affinity; uint64_t affinity;
std::string identity; blob_t identity;
// Maximum tranfer rate [kb/s]. Default 100kb/s. // Maximum tranfer rate [kb/s]. Default 100kb/s.
uint32_t rate; uint32_t rate;

View File

@ -89,8 +89,11 @@ int zmq::pgm_socket_t::init (bool udp_encapsulation_, const char *network_)
if (options.identity.size () > 0) { if (options.identity.size () > 0) {
// Create gsi from identity string. // Create gsi from identity.
gsi_base = options.identity; // TODO: We assume that identity is standard C string here.
// What if it contains binary zeroes?
gsi_base.assign ((const char*) options.identity.data (),
options.identity.size ());
} else { } else {
// Generate random gsi. // Generate random gsi.

View File

@ -245,7 +245,7 @@ void zmq::session_t::process_attach (i_engine *engine_,
} }
send_bind (owner, outbound ? &outbound->reader : NULL, send_bind (owner, outbound ? &outbound->reader : NULL,
inbound ? &inbound->writer : NULL); inbound ? &inbound->writer : NULL, peer_identity);
} }
// Plug in the engine. // Plug in the engine.

View File

@ -171,7 +171,7 @@ int zmq::socket_base_t::connect (const char *addr_)
// was incremented in find_endpoint function. The callee is notified // was incremented in find_endpoint function. The callee is notified
// about the fact via the last parameter. // about the fact via the last parameter.
send_bind (peer, out_pipe ? &out_pipe->reader : NULL, send_bind (peer, out_pipe ? &out_pipe->reader : NULL,
in_pipe ? &in_pipe->writer : NULL, false); in_pipe ? &in_pipe->writer : NULL, options.identity, false);
return 0; return 0;
} }
@ -564,7 +564,8 @@ void zmq::socket_base_t::process_own (owned_t *object_)
io_objects.insert (object_); io_objects.insert (object_);
} }
void zmq::socket_base_t::process_bind (reader_t *in_pipe_, writer_t *out_pipe_) void zmq::socket_base_t::process_bind (reader_t *in_pipe_, writer_t *out_pipe_,
const blob_t &peer_identity_)
{ {
attach_pipes (in_pipe_, out_pipe_); attach_pipes (in_pipe_, out_pipe_);
} }

View File

@ -122,7 +122,8 @@ namespace zmq
// Handlers for incoming commands. // Handlers for incoming commands.
void process_own (class owned_t *object_); void process_own (class owned_t *object_);
void process_bind (class reader_t *in_pipe_, class writer_t *out_pipe_); void process_bind (class reader_t *in_pipe_, class writer_t *out_pipe_,
const blob_t &peer_identity_);
void process_term_req (class owned_t *object_); void process_term_req (class owned_t *object_);
void process_term_ack (); void process_term_ack ();
void process_seqnum (); void process_seqnum ();