void OTSocket::Listen(const OTString &strBind) { if (NULL != m_pSocket) delete m_pSocket; // m_pSocket = NULL; m_pSocket = new zmq::socket_t(*m_pContext, ZMQ_REP); // RESPONSE socket (Request / Response.) OT_ASSERT_MSG(NULL != m_pSocket, "OTSocket::Listen: new zmq::socket(context, ZMQ_REP)"); OTString strTemp(strBind); // In case m_strBindPath is what was passed in. (It happens.) m_strBindPath.Set(strTemp); // In case we have to close/reopen the socket to finish a send/receive. // ------------------------ // Configure socket to not wait at close time // const int linger = 0; // close immediately m_pSocket->setsockopt (ZMQ_LINGER, &linger, sizeof (linger)); /* int zmq_setsockopt (void *socket, int option_name, const void *option_value, size_t option_len); Caution: All options, with the exception of ZMQ_SUBSCRIBE, ZMQ_UNSUBSCRIBE and ZMQ_LINGER, only take effect for subsequent socket bind/connects. */ // ------------------------ m_pSocket->bind(strBind.Get()); }
SampleSensor::SampleSensor( VaneID specificId, SensorStaticAssetParams& params, DynamicAssetParams& dynamicParams ) : Sensor(specificId, params, dynamicParams) { //Get the current IP address String ip; if(!getMyIP(ip)) { LogMessage("Failed to get IP", kLogMsgError); running = false; } else { LogMessage(ip); ipaddr = ip; running = true; //Bind to the computer's IP adress socket_.init(context_, ZMQ_PAIR); socket_.bind("tcp://" + ip + ":9000"); LogMessage("Connected", kLogMsgSpecial); } frame = 0; sendRate = 15; quality_factor = 85; //cast to our specific type of asset params, and grab data const SampleSensorStaticAssetParams& sampleParams = static_cast<const SampleSensorStaticAssetParams&>( params ); m_sampleIntData = sampleParams.m_intData; }
comm_handler(config_param_t &cparam): zmq_ctx(1), msgq(zmq_ctx, ZMQ_PULL), taskq(zmq_ctx, ZMQ_PUSH), conn_sock(zmq_ctx, ZMQ_PULL), shutdown_sock(zmq_ctx, ZMQ_PULL), router_sock(zmq_ctx, ZMQ_ROUTER), monitor_sock(zmq_ctx, ZMQ_PAIR), pub_sendpull(zmq_ctx, ZMQ_PULL), pubstart_pull(zmq_ctx, ZMQ_PULL), suberq(zmq_ctx, ZMQ_PUSH), subpull(zmq_ctx, ZMQ_PULL), pub_sock(zmq_ctx, ZMQ_PUB), sub_sock(zmq_ctx, ZMQ_SUB), id(IDE2I(cparam.id)), ip(cparam.ip), port(cparam.port), accept_conns(cparam.accept_conns){ pthread_mutex_init(&sync_mtx, NULL); try{ msgq.bind(MSGQ_ENDP); taskq.bind(TASKQ_ENDP); conn_sock.bind(CONN_ENDP); shutdown_sock.bind(SHUTDOWN_ENDP); pub_sendpull.bind(PUB_SEND_ENDP); pubstart_pull.bind(PUB_START_ENDP); suberq.bind(SUBER_Q_ENDP); subpull.bind(SUB_PULL_ENDP); }catch(zmq::error_t &e){ LOG(DBG, stderr, "Failed to bind to inproc socket: %s\n", e.what()); throw std::bad_alloc(); } pthread_mutex_lock(&sync_mtx); errcode = 0; int ret = pthread_create(&pthr, NULL, start_handler, this); if(ret != 0) throw std::bad_alloc(); pthread_mutex_lock(&sync_mtx); //cannot return until comm thread starts running pthread_mutex_unlock(&sync_mtx); if(errcode) throw std::bad_alloc(); }
uint16_t _bind_tcp(zmq::socket_t & sock, const std::string & ip, uint16_t port) { std::ostringstream ep; ep << "tcp://" << ip << ":"; if (port == 0) { ep << "*"; } else { ep << port; } DLOG(INFO) << "ZMQ binding endpoint " << ep.str(); sock.bind(ep.str().c_str()); if (port == 0) { const std::string endpoint = _get_zmq_last_endpoint(sock); uint16_t out_port = 0; _try_zmq_endpoint_get_port(endpoint, out_port); return out_port; } return port; }
/** This function compactly allows binding a ZMQ socket to inproc address without needing to specify an exact address. The function will try binding to addresses in the format: inproc://inputBaseString.inputExtensionNumberAsString and will try repeatedly while incrementing inputExtensionNumber until it succeeds or the maximum number of tries has been exceeded. @param inputSocket: The ZMQ socket to bind @param inputBaseString: The base string to use @param inputExtensionNumber: The extension number to start with @param inputMaximumNumberOfTries: How many times to try binding before giving up @return: A tuple of form <connectionString ("inproc://etc"), extensionNumberThatWorked> @throws: This function can throw exceptions if the bind call throws something besides "address taken" or the number of tries are exceeded */ std::tuple<std::string, int> pylongps::bindZMQSocketWithAutomaticAddressGeneration(zmq::socket_t &inputSocket, const std::string &inputBaseString, int inputExtensionNumber, unsigned int inputMaximumNumberOfTries) { bool socketBindSuccessful = false; std::string connectionString; int extensionNumber = inputExtensionNumber; for(int i=0; i<inputMaximumNumberOfTries; i++) { try //Attempt to bind the socket { connectionString = std::string("inproc://") + inputBaseString + std::string(".") + std::to_string(extensionNumber); inputSocket.bind(connectionString.c_str()); socketBindSuccessful = true; //Got this far without throwing break; } catch(const zmq::error_t &inputZMQError) { if(inputZMQError.num() == EADDRINUSE) { extensionNumber++; //Increment so the next attempted address won't conflict } else { throw SOMException(std::string("Error binding socket") + connectionString + std::string("\n"), ZMQ_ERROR, __FILE__, __LINE__); } } } if(!socketBindSuccessful) { throw SOMException(std::string("Socket bind did not succeed in ") + std::to_string(inputMaximumNumberOfTries) + std::string(" attempts\n"), UNKNOWN, __FILE__, __LINE__); } return make_tuple(connectionString, extensionNumber); }
Transport() : context_(1), requestSocket_(context_, ZMQ_REQ), replySocket_(context_, ZMQ_REP) { replySocket_.bind("tcp://*:5555"); requestSocket_.connect("tcp://localhost:5555"); }
void bind(::zmq::socket_t& socket, const AddressList& address_list) { typedef typename AddressList::const_iterator iter_t; for (iter_t it = address_list.begin(); it != address_list.end(); ++it) socket.bind(it->c_str()); }