1 #include "hmbdc/Copyright.hpp" 3 #include "hmbdc/app/Logger.hpp" 4 #include "hmbdc/app/tcpcast/Transport.hpp" 5 #include "hmbdc/app/tcpcast/Messages.hpp" 6 #include "hmbdc/time/Time.hpp" 7 #include "hmbdc/os/DownloadFile.hpp" 8 #include "hmbdc/os/DownloadMemory.hpp" 9 #include "hmbdc/comm/inet/Misc.hpp" 11 #include "hmbdc/app/tcpcast/RecvSession.hpp" 13 #include <boost/lexical_cast.hpp> 21 namespace hmbdc {
namespace app {
namespace tcpcast {
23 namespace recvsession_detail {
27 template <
typename OutputBuffer,
typename MsgArbitrator>
29 using ptr = shared_ptr<RecvSession<OutputBuffer, MsgArbitrator>>;
30 using CleanupFunc = std::function<void()>;
33 , boost::regex
const& topicRegex
34 , OutputBuffer& outputBuffer
37 , subscriptions_(subscriptions)
38 , topicRegex_(topicRegex)
41 , outputBuffer_(outputBuffer)
44 , bufSize_(config.
getExt<
size_t>(
"maxTcpReadBytes"))
45 , buf_((
char*)memalign(SMP_CACHE_BYTES, bufSize_))
48 , currTransportHeadFlag_(0) {
49 config(allowRecvMemoryAttachment_,
"allowRecvMemoryAttachment");
54 HMBDC_LOG_N(
"RecvSession retired: ",
id());
57 void start(in_addr_t ip, uint16_t port) {
58 sockaddr_in remoteAddr = {0};
59 remoteAddr.sin_family = AF_INET;
60 remoteAddr.sin_addr.s_addr = ip;
61 remoteAddr.sin_port = htons(port);
62 if (connect(writeFd_.fd, (sockaddr*)&remoteAddr,
sizeof(remoteAddr)) < 0) {
63 if (errno != EINPROGRESS) {
64 HMBDC_THROW(runtime_error,
"connect fail, errno=" << errno);
68 auto forRead = dup(writeFd_.fd);
70 HMBDC_THROW(std::runtime_error,
"dup failed errno=" << errno);
74 utils::EpollTask::instance().add(EPOLLOUT|EPOLLET, writeFd_);
75 utils::EpollTask::instance().add(EPOLLIN|EPOLLET, readFd_);
79 if (currTransportHeadFlag_ == hasMemoryAttachment::flag) {
81 outputBuffer_.commit(itPending_);
82 currTransportHeadFlag_ = 0;
84 outputBuffer_.putSome(sessionDropped_);
87 void sendSubscribe(
Topic const& t) {
88 if (hmbdc_unlikely(boost::regex_match(t.c_str(), topicRegex_))) {
91 auto l = int(t.copyTo(buf + 1));
93 if (hmbdc_unlikely(send(writeFd_.fd, buf, l + 2, MSG_NOSIGNAL) != l + 2)) {
94 HMBDC_LOG_C(
"error when sending subscribe to ",
id());
101 void sendUnsubscribe(
Topic const& t) {
102 if (hmbdc_unlikely(boost::regex_match(t.c_str(), topicRegex_))) {
105 auto l = int(t.copyTo(buf + 1));
107 if (hmbdc_unlikely(l + 2 != send(writeFd_.fd, buf, l + 2, MSG_NOSIGNAL))) {
108 HMBDC_LOG_C(
"error when sending unsubscribe to ",
id(),
" errno=", errno);
115 if (!initialized_)
return;
116 char const* hb =
"+\t";
117 if (hmbdc_unlikely(2 != send(writeFd_.fd, hb, 2, MSG_NOSIGNAL))){
118 HMBDC_LOG_C(
"error when sending heartbeat to ",
id(),
" errno=", errno);
123 char const* id()
const {
128 if (hmbdc_unlikely(stopped_))
return false;
129 if (hmbdc_unlikely(!initialized_ && writeFd_.isFdReady())) {
132 }
catch (std::exception
const& e) {
133 HMBDC_LOG_W(e.what());
136 HMBDC_LOG_C(
"unknown exception");
144 void initializeConn() {
145 int flags = fcntl(writeFd_.fd, F_GETFL, 0);
146 flags &= ~O_NONBLOCK;
147 if (fcntl(writeFd_.fd, F_SETFL, flags) < 0) {
148 HMBDC_THROW(std::runtime_error,
"fcntl failed errno=" << errno);
151 auto sz = config_.getExt<
int>(
"tcpRecvBufferBytes");
153 if (setsockopt(readFd_.fd, SOL_SOCKET, SO_RCVBUF, &sz,
sizeof(sz)) < 0) {
154 HMBDC_LOG_C(
"failed to set send buffer size=", sz);
158 auto addrPort = hmbdc::comm::inet::getPeerIpPort(writeFd_.fd);
159 id_ = addrPort.first +
":" + std::to_string(addrPort.second);
160 strncpy(sessionStarted_.payload.ip, id_.c_str()
161 ,
sizeof(sessionStarted_.payload.ip));
162 sessionStarted_.payload.ip[
sizeof(sessionStarted_.payload.ip) - 1] = 0;
163 strncpy(sessionDropped_.payload.ip, id_.c_str()
164 ,
sizeof(sessionDropped_.payload.ip));
165 sessionDropped_.payload.ip[
sizeof(sessionDropped_.payload.ip) - 1] = 0;
166 outputBuffer_.putSome(sessionStarted_);
167 HMBDC_LOG_N(
"RecvSession started: ",
id());
172 void sendSubscriptions() {
174 for (
auto it = subscriptions_.begin(); it != subscriptions_.end(); ++it) {
175 auto const& t = it->first;
176 if (boost::regex_match(t.c_str(), topicRegex_)) {
184 auto s = oss.str() +
"+\t";
185 auto sz = size_t(send(writeFd_.fd, s.c_str(), s.size(), MSG_NOSIGNAL));
186 if (hmbdc_unlikely(sz != s.size())) {
187 HMBDC_LOG_C(
"error when sending subscriptions to ",
id());
194 if (hmbdc_unlikely(currTransportHeadFlag_ == hasMemoryAttachment::flag
196 auto s = memoryAttachment_.write(bufCur_, filledLen_);
197 if (memoryAttachment_.writeDone()) {
198 memoryAttachment_.close();
199 currTransportHeadFlag_ = 0;
200 outputBuffer_.commit(itPending_);
205 while (currTransportHeadFlag_ == 0
208 auto wireSize = h->wireSize();
209 if (hmbdc_likely(filledLen_ >= wireSize)) {
210 if (hmbdc_likely(subscriptions_.check(h->topic()))) {
212 if (hmbdc_likely(a > 0)) {
213 if (hmbdc_unlikely(h->flag == hasMemoryAttachment::flag
214 && allowRecvMemoryAttachment_.find(h->typeTag()) != allowRecvMemoryAttachment_.end())) {
215 currTransportHeadFlag_ = h->flag;
216 itPending_ = outputBuffer_.claim();
217 char* b =
static_cast<char*
>(*itPending_);
218 auto l = std::min<size_t>(outputBuffer_.maxItemSize(), h->messagePayloadLen);
219 memcpy(b, h->payload(), l);
222 att.hasMemoryAttachment::attachment = memoryAttachment_.open(att.len);
223 att.hasMemoryAttachment::afterConsumedCleanupFunc = hasMemoryAttachment::free;
224 }
else if (h->flag != hasMemoryAttachment::flag) {
225 auto l = std::min<size_t>(outputBuffer_.maxItemSize(), h->messagePayloadLen);
226 outputBuffer_.put(h->payload(), l);
227 currTransportHeadFlag_ = 0;
229 }
else if (hmbdc_unlikely(a == 0)) {
237 filledLen_ -= wireSize;
242 memmove(buf_, bufCur_, filledLen_);
245 if (readFd_.isFdReady()) {
246 auto l = recv(readFd_.fd, buf_ + filledLen_, bufSize_ - filledLen_
247 , MSG_NOSIGNAL|MSG_DONTWAIT);
248 if (hmbdc_unlikely(l < 0)) {
249 if (hmbdc_unlikely(!readFd_.checkErr())) {
250 HMBDC_LOG_C(
"recv failed errno=", errno);
254 }
else if (hmbdc_unlikely(l == 0)) {
255 HMBDC_LOG_W(
"peer dropped:",
id());
268 boost::regex topicRegex_;
272 OutputBuffer& outputBuffer_;
278 size_t const bufSize_;
282 uint8_t currTransportHeadFlag_;
283 typename OutputBuffer::iterator itPending_;
285 std::unordered_set<uint16_t> allowRecvMemoryAttachment_;
289 template <
typename OutputBuffer,
typename MsgArbitrator>
class to hold an hmbdc configuration
Definition: Config.hpp:44
topic as in the publish / subscribe communication paradigm
Definition: Topic.hpp:14
T getExt(const path_type ¶m) const
get a value from the config
Definition: Config.hpp:154
Definition: TypedString.hpp:74
Definition: EpollTask.hpp:31
Definition: DownloadMemory.hpp:10
Definition: Transport.hpp:24
Definition: RecvSession.hpp:28
Definition: StringTrieSetDetail.hpp:115
Definition: Message.hpp:72
Definition: LfbStream.hpp:11