Skip to content

Commit

Permalink
Change to fix with HHVM 3.6.0
Browse files Browse the repository at this point in the history
NEWOBJ to newres
persistent-resource-store is gone, use thread_local map (found in https://github.com/facebook/hhvm/blob/master/hphp/runtime/ext/mysql/mysql_common.cpp)
<nitpick>Switch sockopts on numeric/strings.</nitpick>
<nitpick>ZMQ_* are ints, not int64</nitpick>

I make no promises this works outside of "it worked for me"
  • Loading branch information
kilyo committed Feb 21, 2015
1 parent 9185ecc commit ec1bd9e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
16 changes: 12 additions & 4 deletions zmq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ static void HHVM_METHOD(ZMQContext, __construct, int64_t io_threads) {

static void HHVM_METHOD(ZMQSocket, __construct, const Object& zmqcontext, int64_t type, const String& persistent_id, const Object& on_new_socket) {
auto context = get_context(zmqcontext);
auto socket = NEWOBJ(SocketData)(context->get(), type);
auto socket = newres<SocketData>(context->get(), type);

setVariable(this_, "socket", Resource(socket));
}
Expand Down Expand Up @@ -255,12 +255,20 @@ static Variant HHVM_METHOD(ZMQSocket, send, const String& message, int64_t mode)
return this_;
}

static Object HHVM_METHOD(ZMQSocket, setSockOpt, int64_t key, const Variant& value) {
static Object HHVM_METHOD(ZMQSocket, setSockOpt, int key, const Variant& value) {
auto socket = getResource<SocketData>(this_, "socket");

String sValue = value.toString();
int result = 0;
if(value.isNumeric()==true){
int64_t val = value.toInt64();
size_t size = sizeof(val);
result = zmq_setsockopt(socket->get(), key, &val, size);
}
else{
String sValue = value.toString();
result = zmq_setsockopt(socket->get(), key, sValue.c_str(), sValue.size());
}

int result = zmq_setsockopt(socket->get(), key, sValue.c_str(), sValue.size());

if (result != 0) {
throwException(s_ZMQSocketException, errno, "Failed to set socket option: %s", zmq_strerror(errno));
Expand Down
20 changes: 17 additions & 3 deletions zmq_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,27 @@ ContextData *ContextData::GetPersistent(int64_t io_threads) {
void ContextData::SetPersistent(int64_t io_threads, ContextData *context) {
SetCachedImpl("zmq::persistent_contexts", io_threads, context);
}
std::string ContextData::GetHash(const char *name, int64_t io_threads) {
char buf[1024];
snprintf(buf, sizeof(buf), "%s:%ld",
name, io_threads);
return std::string(buf);
}


namespace {
thread_local std::unordered_map<std::string,
ContextData *> s_connections;
}

ContextData *ContextData::GetCachedImpl(const char *name, int64_t io_threads) {
return dynamic_cast<ContextData*>(g_persistentResources->get(name, std::to_string(io_threads).data()));
auto key = GetHash(name, io_threads);
return s_connections[key];
}

void ContextData::SetCachedImpl(const char *name, int64_t io_threads, ContextData *context) {
g_persistentResources->set(name, std::to_string(io_threads).data(), context);
auto key = GetHash(name, io_threads);
s_connections[key] = context;
}

ContextData::ContextData(int64_t io_threads) {
Expand All @@ -48,4 +62,4 @@ ContextData::~ContextData() {
}
}

} // namespace HPHP
} // namespace HPHP
4 changes: 2 additions & 2 deletions zmq_common.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "hphp/runtime/base/base-includes.h"
#include "hphp/runtime/base/persistent-resource-store.h"
#include "string.h"

#include <zmq.h>
Expand All @@ -19,6 +18,7 @@ class ContextData : public SweepableResourceData {
static void SetPersistent(int64_t io_threads, ContextData *context);

private:
static std::string GetHash(const char *name, int64_t io_threads);
static ContextData *GetCachedImpl(const char *name, int64_t io_threads);
static void SetCachedImpl(const char *name, int64_t io_threads, ContextData *context);

Expand Down Expand Up @@ -75,4 +75,4 @@ class SocketData : public ResourceData {
void *m_socket;
};

} // namespace HPHP
} // namespace HPHP

0 comments on commit ec1bd9e

Please sign in to comment.