1 #include "hmbdc/Copyright.hpp" 3 #include "hmbdc/app/Logger.hpp" 4 #include "hmbdc/tips/tcpcast/Transport.hpp" 5 #include "hmbdc/tips/tcpcast/Messages.hpp" 6 #include "hmbdc/tips/TypeTagSet.hpp" 7 #include "hmbdc/comm/inet/Misc.hpp" 10 #include <boost/lexical_cast.hpp> 19 namespace hmbdc {
namespace tips {
namespace tcpcast {
21 namespace recvsession_detail {
26 template <
typename OutputBuffer,
typename AttachmentAllocator>
28 using ptr = shared_ptr<RecvSession<OutputBuffer, AttachmentAllocator>>;
29 using CleanupFunc = std::function<void()>;
32 , OutputBuffer& outputBuffer)
34 , subscriptions_(subscriptions)
36 , outputBuffer_(outputBuffer)
39 , bufSize_(config.
getExt<
size_t>(
"maxTcpReadBytes"))
40 , buf_((
char*)memalign(SMP_CACHE_BYTES, bufSize_))
43 , currTransportHeadFlag_(0)
44 , hasMemoryAttachmentItemSpace_(
45 new char[std::max(outputBuffer_.maxItemSize()
47 , hasMemoryAttachment_(&((
MessageHead*)(hasMemoryAttachmentItemSpace_.get()))
53 hasMemoryAttachment_->release();
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(utils::EpollTask::EPOLLOUT|utils::EpollTask::EPOLLET, writeFd_);
75 utils::EpollTask::instance().add(utils::EpollTask::EPOLLIN|utils::EpollTask::EPOLLET, readFd_);
79 if (currTransportHeadFlag_ == hasMemoryAttachment::flag) {
80 currTransportHeadFlag_ = 0;
82 outputBuffer_.putSome(sessionDropped_);
86 if (!initialized_)
return;
87 char const* hb =
"+\t";
88 if (hmbdc_unlikely(2 != send(writeFd_.fd, hb, 2, MSG_NOSIGNAL))){
89 HMBDC_LOG_C(
"error when sending heartbeat to ",
id(),
" errno=", errno);
94 char const* id()
const {
99 if (hmbdc_unlikely(stopped_))
return false;
100 if (hmbdc_unlikely(!initialized_ && writeFd_.isFdReady())) {
103 }
catch (std::exception
const& e) {
104 HMBDC_LOG_W(e.what());
107 HMBDC_LOG_C(
"unknown exception");
115 void initializeConn() {
116 int flags = fcntl(writeFd_.fd, F_GETFL, 0);
117 flags &= ~O_NONBLOCK;
118 if (fcntl(writeFd_.fd, F_SETFL, flags) < 0) {
119 HMBDC_THROW(std::runtime_error,
"fcntl failed errno=" << errno);
122 auto sz = config_.getExt<
int>(
"tcpRecvBufferBytes");
124 if (setsockopt(readFd_.fd, SOL_SOCKET, SO_RCVBUF, &sz,
sizeof(sz)) < 0) {
125 HMBDC_LOG_C(
"failed to set send buffer size=", sz);
129 auto addrPort = hmbdc::comm::inet::getPeerIpPort(writeFd_.fd);
130 id_ = addrPort.first +
":" + std::to_string(addrPort.second);
131 strncpy(sessionStarted_.payload.ip, id_.c_str()
132 ,
sizeof(sessionStarted_.payload.ip));
133 sessionStarted_.payload.ip[
sizeof(sessionStarted_.payload.ip) - 1] = 0;
134 strncpy(sessionDropped_.payload.ip, id_.c_str()
135 ,
sizeof(sessionDropped_.payload.ip));
136 sessionDropped_.payload.ip[
sizeof(sessionDropped_.payload.ip) - 1] = 0;
137 outputBuffer_.putSome(sessionStarted_);
138 HMBDC_LOG_N(
"RecvSession started: ",
id());
143 void sendSubscriptions() {
145 subscriptions_.exportTo([&oss](uint16_t tag,
auto&& count) {
147 oss <<
'+' << tag <<
'\t';
150 auto s = oss.str() +
"+\t";
151 auto sz = size_t(send(writeFd_.fd, s.c_str(), s.size(), MSG_NOSIGNAL));
152 if (hmbdc_unlikely(sz != s.size())) {
153 HMBDC_LOG_C(
"error when sending subscriptions to ",
id());
160 if (hmbdc_unlikely(currTransportHeadFlag_ == hasMemoryAttachment::flag
162 auto s = memoryAttachment_.write(bufCur_, filledLen_);
163 if (memoryAttachment_.writeDone()) {
164 memoryAttachment_.close();
165 currTransportHeadFlag_ = 0;
167 hasMemoryAttachmentItemSpace_.get(), outputBuffer_.maxItemSize());
172 while (currTransportHeadFlag_ == 0
175 auto wireSize = h->wireSize();
176 if (hmbdc_likely(filledLen_ >= wireSize)) {
177 if (hmbdc_likely(subscriptions_.check(h->typeTag()))) {
178 if (hmbdc_unlikely(h->flag == hasMemoryAttachment::flag)) {
179 currTransportHeadFlag_ = h->flag;
180 auto l = std::min<size_t>(outputBuffer_.maxItemSize(), h->messagePayloadLen);
181 memcpy(hasMemoryAttachmentItemSpace_.get(), h->payload(), l);
182 memoryAttachment_.open(h->typeTag(), hasMemoryAttachment_);
187 auto l = std::min<size_t>(outputBuffer_.maxItemSize(), h->messagePayloadLen);
188 outputBuffer_.put(h->payload(), l);
189 currTransportHeadFlag_ = 0;
194 filledLen_ -= wireSize;
199 memmove(buf_, bufCur_, filledLen_);
202 if (readFd_.isFdReady()) {
203 auto l = recv(readFd_.fd, buf_ + filledLen_, bufSize_ - filledLen_
204 , MSG_NOSIGNAL|MSG_DONTWAIT);
205 if (hmbdc_unlikely(l < 0)) {
206 if (hmbdc_unlikely(!readFd_.checkErr())) {
207 HMBDC_LOG_C(
"recv failed errno=", errno);
210 }
else if (hmbdc_unlikely(l == 0)) {
211 HMBDC_LOG_W(
"peer dropped:",
id());
219 }
while (filledLen_);
227 OutputBuffer& outputBuffer_;
233 size_t const bufSize_;
237 uint8_t currTransportHeadFlag_;
238 std::unique_ptr<char[]> hasMemoryAttachmentItemSpace_;
249 addr_ = (
char*)alloc_(typeTag, att);
257 bool writeDone()
const {
258 return fullLen_ == len_;
261 size_t write(
void const* mem,
size_t l) {
262 auto wl = std::min(l, fullLen_ - len_);
264 memcpy(addr_ + len_, mem, wl);
277 AttachmentAllocator alloc_;
285 template <
typename OutputBuffer,
typename AttachmentAllocator>
Definition: RecvSession.hpp:241
T getExt(const path_type ¶m, bool throwIfMissing=true) const
get a value from the config
Definition: Config.hpp:238
class to hold an hmbdc configuration
Definition: Config.hpp:45
Definition: TypedString.hpp:84
Definition: RecvSession.hpp:27
Definition: EpollTask.hpp:87
Definition: Message.hpp:212
Definition: Transport.hpp:20
Definition: Message.hpp:263
if a specific hmbdc network transport (for example tcpcast, rmcast, and rnetmap) supports message wit...
Definition: Message.hpp:125