Skip to content

Commit

Permalink
Added recv method
Browse files Browse the repository at this point in the history
  • Loading branch information
duxet committed Apr 9, 2014
1 parent 8af951f commit 0d3b1ec
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
43 changes: 39 additions & 4 deletions zmq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,49 @@ static Object HHVM_METHOD(ZMQSocket, disconnect, const String& dsn) {
return this_;
}

static Object HHVM_METHOD(ZMQSocket, send, const String& message, int64_t mode) {
static Variant HHVM_METHOD(ZMQSocket, recv, int64_t mode) {
auto socket = getResource<SocketData>(this_, "socket");

int result = zmq_send(socket->get(), message.c_str(), message.size() + 1, mode);

zmq_msg_t msg;
int result = zmq_msg_init(&msg);

if (result != 0) {
throwException(s_ZMQSocketException, errno, "Failed to send message: %s", zmq_strerror(errno));
throwException(s_ZMQSocketException, errno, "Failed to initialize message structure: %s", zmq_strerror(errno));
}

result = zmq_msg_recv(&msg, socket->get(), mode);
int errno_ = errno;

if (result == -1) {
zmq_msg_close(&msg);

if (errno_ == EAGAIN) {
return false;
}

throwException(s_ZMQSocketException, errno, "Failed to receive message: %s", zmq_strerror(errno));
}

std::string data((const char*) zmq_msg_data(&msg), zmq_msg_size(&msg));

zmq_msg_close(&msg);

return data;
}

static Variant HHVM_METHOD(ZMQSocket, send, const String& message, int64_t mode) {
auto socket = getResource<SocketData>(this_, "socket");

result = zmq_send(socket->get(), message.c_str(), message.size() + 1, mode);

if (result == -1) {
if (errno == EAGAIN) {
return false;
}

throwException(s_ZMQSocketException, errno, "Failed to send message: %s", zmq_strerror(errno));
}

return this_;
}

Expand Down Expand Up @@ -156,6 +190,7 @@ static class ZMQExtension : public Extension {
HHVM_ME(ZMQSocket, bind);
HHVM_ME(ZMQSocket, connect);
HHVM_ME(ZMQSocket, disconnect);
HHVM_ME(ZMQSocket, recv);
HHVM_ME(ZMQSocket, send);
HHVM_ME(ZMQSocket, setSockOpt);
HHVM_ME(ZMQSocket, getSocketType);
Expand Down
11 changes: 6 additions & 5 deletions zmq.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,20 +240,21 @@ public function __construct(ZMQContext $context, int $type, string $persistent_i
* @param integer $flags self::MODE_NOBLOCK or 0
* @throws ZMQException if sending message fails
*
* @return ZMQ
* @return mixed
*/
<<__Native>>
public function send(string $message, int $flags = 0): ZMQSocket;
public function send(string $message, int $flags = 0): mixed;

/**
* Receives a message from the queue.
*
* @param integer $flags self::MODE_NOBLOCK or 0
* @throws ZMQException if receiving fails.
*
* @return string
* @return mixed
*/
public function recv($flags = 0) {}
<<__Native>>
public function recv(int $flags = 0): mixed;

/**
* Connect the socket to a remote endpoint. For more information about the dsn
Expand Down Expand Up @@ -318,7 +319,7 @@ public function setSockOpt(int $key, mixed $value): ZMQSocket;
* @throws ZMQException
* @return mixed
*/
public function getSockOpt($key) {}
public function getSockOpt(int $key) {}

/**
* Get endpoints where the socket is connected to. The return array
Expand Down

0 comments on commit 0d3b1ec

Please sign in to comment.