David Reiss | 9f3296b | 2010-08-31 16:58:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
| 20 | #include "TZmqServer.h" |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 21 | #include <thrift/transport/TBufferTransports.h> |
David Reiss | 9f3296b | 2010-08-31 16:58:41 +0000 | [diff] [blame] | 22 | #include <boost/scoped_ptr.hpp> |
| 23 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 24 | using apache::thrift::std::shared_ptr; |
David Reiss | 9f3296b | 2010-08-31 16:58:41 +0000 | [diff] [blame] | 25 | using apache::thrift::transport::TMemoryBuffer; |
| 26 | using apache::thrift::protocol::TProtocol; |
| 27 | |
| 28 | namespace apache { namespace thrift { namespace server { |
| 29 | |
David Reiss | 9f3296b | 2010-08-31 16:58:41 +0000 | [diff] [blame] | 30 | bool TZmqServer::serveOne(int recv_flags) { |
| 31 | zmq::message_t msg; |
| 32 | bool received = sock_.recv(&msg, recv_flags); |
| 33 | if (!received) { |
| 34 | return false; |
| 35 | } |
| 36 | shared_ptr<TMemoryBuffer> inputTransport(new TMemoryBuffer((uint8_t*)msg.data(), msg.size())); |
| 37 | shared_ptr<TMemoryBuffer> outputTransport(new TMemoryBuffer()); |
| 38 | shared_ptr<TProtocol> inputProtocol( |
| 39 | inputProtocolFactory_->getProtocol(inputTransport)); |
| 40 | shared_ptr<TProtocol> outputProtocol( |
| 41 | outputProtocolFactory_->getProtocol(outputTransport)); |
Jens Geyer | 3e9c3a2 | 2014-12-22 22:21:24 +0100 | [diff] [blame] | 42 | shared_ptr<TMemoryBuffer> transport(new TMemoryBuffer); |
David Reiss | 9f3296b | 2010-08-31 16:58:41 +0000 | [diff] [blame] | 43 | |
zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 44 | processor_->process(inputProtocol, outputProtocol, nullptr); |
David Reiss | 9f3296b | 2010-08-31 16:58:41 +0000 | [diff] [blame] | 45 | |
| 46 | if (zmq_type_ == ZMQ_REP) { |
| 47 | uint8_t* buf; |
| 48 | uint32_t size; |
| 49 | outputTransport->getBuffer(&buf, &size); |
| 50 | msg.rebuild(size); |
| 51 | std::memcpy(msg.data(), buf, size); |
| 52 | (void)sock_.send(msg); |
| 53 | } |
| 54 | |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | |
| 59 | void TZmqMultiServer::serveOne(long timeout) { |
| 60 | boost::scoped_ptr<zmq::pollitem_t> items(setupPoll()); |
| 61 | serveActive(items.get(), timeout); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | void TZmqMultiServer::serveForever() { |
| 66 | boost::scoped_ptr<zmq::pollitem_t> items(setupPoll()); |
| 67 | while (true) { |
| 68 | serveActive(items.get(), -1); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | |
| 73 | zmq::pollitem_t* TZmqMultiServer::setupPoll() { |
| 74 | zmq::pollitem_t* items = new zmq::pollitem_t[servers_.size()]; |
| 75 | for (int i = 0; i < servers_.size(); ++i) { |
| 76 | items[i].socket = servers_[i]->getSocket(); |
| 77 | items[i].events = ZMQ_POLLIN; |
| 78 | } |
| 79 | return items; |
| 80 | } |
| 81 | |
| 82 | void TZmqMultiServer::serveActive(zmq::pollitem_t* items, long timeout) { |
| 83 | int rc = zmq::poll(items, servers_.size(), timeout); |
| 84 | if (rc == 0) { |
| 85 | return; |
| 86 | } |
| 87 | for (int i = 0; i < servers_.size(); ++i) { |
| 88 | if ((items[i].revents & ZMQ_POLLIN) != 0) { |
| 89 | // Should we pass ZMQ_NOBLOCK here to be safe? |
| 90 | servers_[i]->serveOne(); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | |
| 96 | }}} // apache::thrift::server |