4 #include <netinet/tcp.h>
9 #include "connection.h"
12 #include "lang/verify.h"
15 #define MAX_PDU (10<<20) //maximum PDF is 10M
18 connection::connection(chanmgr *m1, int f1, int l1)
19 : mgr_(m1), fd_(f1), dead_(false),waiters_(0), refno_(1),lossy_(l1)
22 int flags = fcntl(fd_, F_GETFL, NULL);
24 fcntl(fd_, F_SETFL, flags);
26 signal(SIGPIPE, SIG_IGN);
28 create_time_ = std::chrono::steady_clock::now();
30 PollMgr::Instance()->add_callback(fd_, CB_RDONLY, this);
33 connection::~connection()
57 connection::closeconn()
63 shutdown(fd_,SHUT_RDWR);
68 //after block_remove_fd, select will never wait on fd_
69 //and no callbacks will be active
70 PollMgr::Instance()->block_remove_fd(fd_);
99 connection::compare(connection *another)
101 if (create_time_ > another->create_time_)
103 if (create_time_ < another->create_time_)
109 connection::send(char *b, int sz)
113 while (!dead_ && wpdu_.buf) {
125 if ((random()%100) < lossy_) {
126 jsl_log(JSL_DBG_1, "connection::send LOSSY TEST shutdown fd_ %d\n", fd_);
127 shutdown(fd_,SHUT_RDWR);
134 PollMgr::Instance()->block_remove_fd(fd_);
137 if (wpdu_.solong == wpdu_.sz) {
139 //should be rare to need to explicitly add write callback
140 PollMgr::Instance()->add_callback(fd_, CB_WRONLY, this);
141 while (!dead_ && wpdu_.solong >= 0 && wpdu_.solong < wpdu_.sz) {
142 send_complete_.wait(ml);
146 bool ret = (!dead_ && wpdu_.solong == wpdu_.sz);
147 wpdu_.solong = wpdu_.sz = 0;
150 send_wait_.notify_all();
154 //fd_ is ready to be written
156 connection::write_cb(int s)
162 PollMgr::Instance()->del_callback(fd_,CB_WRONLY);
166 PollMgr::Instance()->del_callback(fd_, CB_RDWR);
169 VERIFY(wpdu_.solong >= 0);
170 if (wpdu_.solong < wpdu_.sz) {
174 send_complete_.notify_one();
177 //fd_ is ready to be read
179 connection::read_cb(int s)
188 if (!rpdu_.buf || rpdu_.solong < rpdu_.sz) {
193 PollMgr::Instance()->del_callback(fd_,CB_RDWR);
195 send_complete_.notify_one();
198 if (rpdu_.buf && rpdu_.sz == rpdu_.solong) {
199 if (mgr_->got_pdu(this, rpdu_.buf, rpdu_.sz)) {
200 //chanmgr has successfully consumed the pdu
202 rpdu_.sz = rpdu_.solong = 0;
208 connection::writepdu()
210 VERIFY(wpdu_.solong >= 0);
211 if (wpdu_.solong == wpdu_.sz)
214 if (wpdu_.solong == 0) {
215 int sz = htonl(wpdu_.sz);
216 bcopy(&sz,wpdu_.buf,sizeof(sz));
218 int n = write(fd_, wpdu_.buf + wpdu_.solong, (wpdu_.sz-wpdu_.solong));
220 if (errno != EAGAIN) {
221 jsl_log(JSL_DBG_1, "connection::writepdu fd_ %d failure errno=%d\n", fd_, errno);
225 return (errno == EAGAIN);
232 connection::readpdu()
236 int n = read(fd_, &sz1, sizeof(sz1));
243 VERIFY(errno!=EAGAIN);
247 if (n >0 && n!= sizeof(sz)) {
248 jsl_log(JSL_DBG_OFF, "connection::readpdu short read of sz\n");
255 char *tmpb = (char *)&sz1;
256 jsl_log(JSL_DBG_2, "connection::readpdu read pdu TOO BIG %d network order=%x %x %x %x %x\n", sz,
257 sz1, tmpb[0],tmpb[1],tmpb[2],tmpb[3]);
262 VERIFY(rpdu_.buf == NULL);
263 rpdu_.buf = (char *)malloc(sz+sizeof(sz));
265 bcopy(&sz1,rpdu_.buf,sizeof(sz));
266 rpdu_.solong = sizeof(sz);
269 int n = read(fd_, rpdu_.buf + rpdu_.solong, rpdu_.sz - rpdu_.solong);
276 rpdu_.sz = rpdu_.solong = 0;
277 return (errno == EAGAIN);
283 tcpsconn::tcpsconn(chanmgr *m1, int port, int lossytest)
284 : mgr_(m1), lossy_(lossytest)
286 struct sockaddr_in sin;
287 memset(&sin, 0, sizeof(sin));
288 sin.sin_family = AF_INET;
289 sin.sin_port = htons(port);
291 tcp_ = socket(AF_INET, SOCK_STREAM, 0);
293 perror("tcpsconn::tcpsconn accept_loop socket:");
298 setsockopt(tcp_, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
299 setsockopt(tcp_, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes));
301 if (bind(tcp_, (sockaddr *)&sin, sizeof(sin)) < 0) {
302 perror("accept_loop tcp bind:");
306 if (listen(tcp_, 1000) < 0) {
307 perror("tcpsconn::tcpsconn listen:");
311 socklen_t addrlen = sizeof(sin);
312 VERIFY(getsockname(tcp_, (sockaddr *)&sin, &addrlen) == 0);
313 port_ = ntohs(sin.sin_port);
315 jsl_log(JSL_DBG_2, "tcpsconn::tcpsconn listen on %d %d\n", port_,
318 if (pipe(pipe_) < 0) {
319 perror("accept_loop pipe:");
323 int flags = fcntl(pipe_[0], F_GETFL, NULL);
325 fcntl(pipe_[0], F_SETFL, flags);
327 th_ = std::thread(&tcpsconn::accept_conn, this);
330 tcpsconn::~tcpsconn()
332 VERIFY(close(pipe_[1]) == 0);
335 //close all the active connections
336 std::map<int, connection *>::iterator i;
337 for (i = conns_.begin(); i != conns_.end(); i++) {
338 i->second->closeconn();
344 tcpsconn::process_accept()
347 socklen_t slen = sizeof(sin);
348 int s1 = accept(tcp_, (sockaddr *)&sin, &slen);
350 perror("tcpsconn::accept_conn error");
351 throw thread_exit_exception();
354 jsl_log(JSL_DBG_2, "accept_loop got connection fd=%d %s:%d\n",
355 s1, inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
356 connection *ch = new connection(mgr_, s1, lossy_);
358 // garbage collect all dead connections with refcount of 1
359 std::map<int, connection *>::iterator i;
360 for (i = conns_.begin(); i != conns_.end();) {
361 if (i->second->isdead() && i->second->ref() == 1) {
362 jsl_log(JSL_DBG_2, "accept_loop garbage collected fd=%d\n",
363 i->second->channo());
365 // Careful not to reuse i right after erase. (i++) will
366 // be evaluated before the erase call because in C++,
367 // there is a sequence point before a function call.
368 // See http://en.wikipedia.org/wiki/Sequence_point.
374 conns_[ch->channo()] = ch;
378 tcpsconn::accept_conn()
381 int max_fd = pipe_[0] > tcp_ ? pipe_[0] : tcp_;
386 FD_SET(pipe_[0], &rfds);
389 int ret = select(max_fd+1, &rfds, NULL, NULL, NULL);
392 if (errno == EINTR) {
395 perror("accept_conn select:");
396 jsl_log(JSL_DBG_OFF, "tcpsconn::accept_conn failure errno %d\n",errno);
401 if (FD_ISSET(pipe_[0], &rfds)) {
406 else if (FD_ISSET(tcp_, &rfds)) {
413 catch (thread_exit_exception e)
419 connect_to_dst(const sockaddr_in &dst, chanmgr *mgr, int lossy)
421 int s = socket(AF_INET, SOCK_STREAM, 0);
423 setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes));
424 if (connect(s, (sockaddr*)&dst, sizeof(dst)) < 0) {
425 jsl_log(JSL_DBG_1, "rpcc::connect_to_dst failed to %s:%d\n",
426 inet_ntoa(dst.sin_addr), (int)ntohs(dst.sin_port));
430 jsl_log(JSL_DBG_2, "connect_to_dst fd=%d to dst %s:%d\n",
431 s, inet_ntoa(dst.sin_addr), (int)ntohs(dst.sin_port));
432 return new connection(mgr, s, lossy);