1 // std::bind and syscall bind have the same name, so don't use std::bind in this file
2 #define LIBT4_NO_FUNCTIONAL
3 #include "connection.h"
6 #include <netinet/tcp.h>
11 #include <sys/socket.h>
13 #define MAX_PDU (10<<20) //maximum PDF is 10M
15 connection::connection(chanmgr *m1, int f1, int l1)
16 : mgr_(m1), fd_(f1), dead_(false),waiters_(0), refno_(1),lossy_(l1)
19 int flags = fcntl(fd_, F_GETFL, NULL);
21 fcntl(fd_, F_SETFL, flags);
23 signal(SIGPIPE, SIG_IGN);
25 create_time_ = steady_clock::now();
27 PollMgr::Instance()->add_callback(fd_, CB_RDONLY, this);
30 connection::~connection()
54 connection::closeconn()
60 shutdown(fd_,SHUT_RDWR);
65 //after block_remove_fd, select will never wait on fd_
66 //and no callbacks will be active
67 PollMgr::Instance()->block_remove_fd(fd_);
96 connection::compare(connection *another)
98 if (create_time_ > another->create_time_)
100 if (create_time_ < another->create_time_)
106 connection::send(char *b, size_t sz)
110 while (!dead_ && wpdu_.buf) {
122 if ((random()%100) < lossy_) {
123 jsl_log(JSL_DBG_1, "connection::send LOSSY TEST shutdown fd_ %d\n", fd_);
124 shutdown(fd_,SHUT_RDWR);
131 PollMgr::Instance()->block_remove_fd(fd_);
134 if (wpdu_.solong == wpdu_.sz) {
136 //should be rare to need to explicitly add write callback
137 PollMgr::Instance()->add_callback(fd_, CB_WRONLY, this);
138 while (!dead_ && wpdu_.solong != size_t_max && wpdu_.solong < wpdu_.sz) {
139 send_complete_.wait(ml);
143 bool ret = (!dead_ && wpdu_.solong == wpdu_.sz);
144 wpdu_.solong = wpdu_.sz = 0;
147 send_wait_.notify_all();
151 //fd_ is ready to be written
153 connection::write_cb(int s)
159 PollMgr::Instance()->del_callback(fd_,CB_WRONLY);
163 PollMgr::Instance()->del_callback(fd_, CB_RDWR);
166 VERIFY(wpdu_.solong != size_t_max);
167 if (wpdu_.solong < wpdu_.sz) {
171 send_complete_.notify_one();
174 //fd_ is ready to be read
176 connection::read_cb(int s)
185 if (!rpdu_.buf || rpdu_.solong < rpdu_.sz) {
190 PollMgr::Instance()->del_callback(fd_,CB_RDWR);
192 send_complete_.notify_one();
195 if (rpdu_.buf && rpdu_.sz == rpdu_.solong) {
196 if (mgr_->got_pdu(this, rpdu_.buf, rpdu_.sz)) {
197 //chanmgr has successfully consumed the pdu
199 rpdu_.sz = rpdu_.solong = 0;
205 connection::writepdu()
207 VERIFY(wpdu_.solong != size_t_max);
208 if (wpdu_.solong == wpdu_.sz)
211 if (wpdu_.solong == 0) {
212 uint32_t sz = htonl((uint32_t)wpdu_.sz);
213 bcopy(&sz,wpdu_.buf,sizeof(sz));
215 ssize_t n = write(fd_, wpdu_.buf + wpdu_.solong, (wpdu_.sz-wpdu_.solong));
217 if (errno != EAGAIN) {
218 jsl_log(JSL_DBG_1, "connection::writepdu fd_ %d failure errno=%d\n", fd_, errno);
219 wpdu_.solong = size_t_max;
222 return (errno == EAGAIN);
224 wpdu_.solong += (size_t)n;
229 connection::readpdu()
233 ssize_t n = read(fd_, &sz1, sizeof(sz1));
240 VERIFY(errno!=EAGAIN);
244 if (n > 0 && n != sizeof(sz1)) {
245 jsl_log(JSL_DBG_OFF, "connection::readpdu short read of sz\n");
249 size_t sz = ntohl(sz1);
252 char *tmpb = (char *)&sz1;
253 jsl_log(JSL_DBG_2, "connection::readpdu read pdu TOO BIG %lu network order=%x %x %x %x %x\n", sz,
254 sz1, tmpb[0],tmpb[1],tmpb[2],tmpb[3]);
259 VERIFY(rpdu_.buf == NULL);
260 rpdu_.buf = (char *)malloc(sz+sizeof(sz1));
262 bcopy(&sz1,rpdu_.buf,sizeof(sz1));
263 rpdu_.solong = sizeof(sz1);
266 ssize_t n = read(fd_, rpdu_.buf + rpdu_.solong, rpdu_.sz - rpdu_.solong);
273 rpdu_.sz = rpdu_.solong = 0;
274 return (errno == EAGAIN);
276 rpdu_.solong += (size_t)n;
280 tcpsconn::tcpsconn(chanmgr *m1, unsigned int port, int lossytest)
281 : mgr_(m1), lossy_(lossytest)
283 struct sockaddr_in sin;
284 memset(&sin, 0, sizeof(sin));
285 sin.sin_family = AF_INET;
286 sin.sin_port = htons(port);
288 tcp_ = socket(AF_INET, SOCK_STREAM, 0);
290 perror("tcpsconn::tcpsconn accept_loop socket:");
295 setsockopt(tcp_, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
296 setsockopt(tcp_, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes));
298 if (bind(tcp_, (sockaddr *)&sin, sizeof(sin)) < 0) {
299 perror("accept_loop tcp bind:");
303 if (listen(tcp_, 1000) < 0) {
304 perror("tcpsconn::tcpsconn listen:");
308 socklen_t addrlen = sizeof(sin);
309 VERIFY(getsockname(tcp_, (sockaddr *)&sin, &addrlen) == 0);
310 port_ = ntohs(sin.sin_port);
312 jsl_log(JSL_DBG_2, "tcpsconn::tcpsconn listen on %d %d\n", port_,
315 if (pipe(pipe_) < 0) {
316 perror("accept_loop pipe:");
320 int flags = fcntl(pipe_[0], F_GETFL, NULL);
322 fcntl(pipe_[0], F_SETFL, flags);
324 th_ = thread(&tcpsconn::accept_conn, this);
327 tcpsconn::~tcpsconn()
329 VERIFY(close(pipe_[1]) == 0);
332 //close all the active connections
333 map<int, connection *>::iterator i;
334 for (i = conns_.begin(); i != conns_.end(); i++) {
335 i->second->closeconn();
341 tcpsconn::process_accept()
344 socklen_t slen = sizeof(sin);
345 int s1 = accept(tcp_, (sockaddr *)&sin, &slen);
347 perror("tcpsconn::accept_conn error");
348 throw thread_exit_exception();
351 jsl_log(JSL_DBG_2, "accept_loop got connection fd=%d %s:%d\n",
352 s1, inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
353 connection *ch = new connection(mgr_, s1, lossy_);
355 // garbage collect all dead connections with refcount of 1
356 for (auto i = conns_.begin(); i != conns_.end();) {
357 if (i->second->isdead() && i->second->ref() == 1) {
358 jsl_log(JSL_DBG_2, "accept_loop garbage collected fd=%d\n",
359 i->second->channo());
361 // Careful not to reuse i right after erase. (i++) will
362 // be evaluated before the erase call because in C++,
363 // there is a sequence point before a function call.
364 // See http://en.wikipedia.org/wiki/Sequence_point.
370 conns_[ch->channo()] = ch;
374 tcpsconn::accept_conn()
377 int max_fd = pipe_[0] > tcp_ ? pipe_[0] : tcp_;
382 FD_SET(pipe_[0], &rfds);
385 int ret = select(max_fd+1, &rfds, NULL, NULL, NULL);
388 if (errno == EINTR) {
391 perror("accept_conn select:");
392 jsl_log(JSL_DBG_OFF, "tcpsconn::accept_conn failure errno %d\n",errno);
397 if (FD_ISSET(pipe_[0], &rfds)) {
402 else if (FD_ISSET(tcp_, &rfds)) {
409 catch (thread_exit_exception e)
415 connect_to_dst(const sockaddr_in &dst, chanmgr *mgr, int lossy)
417 int s = socket(AF_INET, SOCK_STREAM, 0);
419 setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes));
420 if (connect(s, (sockaddr*)&dst, sizeof(dst)) < 0) {
421 jsl_log(JSL_DBG_1, "rpcc::connect_to_dst failed to %s:%d\n",
422 inet_ntoa(dst.sin_addr), (int)ntohs(dst.sin_port));
426 jsl_log(JSL_DBG_2, "connect_to_dst fd=%d to dst %s:%d\n",
427 s, inet_ntoa(dst.sin_addr), (int)ntohs(dst.sin_port));
428 return new connection(mgr, s, lossy);