From ec1bd9e0e41c64e99b28dc23234258b69990c4b4 Mon Sep 17 00:00:00 2001 From: Clifton Kilby Date: Sat, 21 Feb 2015 03:03:04 -0500 Subject: [PATCH] Change to fix with HHVM 3.6.0 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) Switch sockopts on numeric/strings. ZMQ_* are ints, not int64 I make no promises this works outside of "it worked for me" --- zmq.cpp | 16 ++++++++++++---- zmq_common.cpp | 20 +++++++++++++++++--- zmq_common.h | 4 ++-- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/zmq.cpp b/zmq.cpp index 0c87a63..93d88cb 100644 --- a/zmq.cpp +++ b/zmq.cpp @@ -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(context->get(), type); setVariable(this_, "socket", Resource(socket)); } @@ -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(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)); diff --git a/zmq_common.cpp b/zmq_common.cpp index 2713ddb..3f7f7bf 100644 --- a/zmq_common.cpp +++ b/zmq_common.cpp @@ -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 s_connections; +} ContextData *ContextData::GetCachedImpl(const char *name, int64_t io_threads) { - return dynamic_cast(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) { @@ -48,4 +62,4 @@ ContextData::~ContextData() { } } -} // namespace HPHP \ No newline at end of file +} // namespace HPHP diff --git a/zmq_common.h b/zmq_common.h index b93e800..3d8c81e 100644 --- a/zmq_common.h +++ b/zmq_common.h @@ -1,5 +1,4 @@ #include "hphp/runtime/base/base-includes.h" -#include "hphp/runtime/base/persistent-resource-store.h" #include "string.h" #include @@ -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); @@ -75,4 +75,4 @@ class SocketData : public ResourceData { void *m_socket; }; -} // namespace HPHP \ No newline at end of file +} // namespace HPHP