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 VERIFY(gettimeofday(&create_time_, NULL) == 0);
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_.tv_sec > another->create_time_.tv_sec)
103 if (create_time_.tv_sec < another->create_time_.tv_sec)
105 if (create_time_.tv_usec > another->create_time_.tv_usec)
107 if (create_time_.tv_usec < another->create_time_.tv_usec)
113 connection::send(char *b, int sz)
117 while (!dead_ && wpdu_.buf) {
129 if ((random()%100) < lossy_) {
130 jsl_log(JSL_DBG_1, "connection::send LOSSY TEST shutdown fd_ %d\n", fd_);
131 shutdown(fd_,SHUT_RDWR);
138 PollMgr::Instance()->block_remove_fd(fd_);
141 if (wpdu_.solong == wpdu_.sz) {
143 //should be rare to need to explicitly add write callback
144 PollMgr::Instance()->add_callback(fd_, CB_WRONLY, this);
145 while (!dead_ && wpdu_.solong >= 0 && wpdu_.solong < wpdu_.sz) {
146 send_complete_.wait(ml);
150 bool ret = (!dead_ && wpdu_.solong == wpdu_.sz);
151 wpdu_.solong = wpdu_.sz = 0;
154 send_wait_.notify_all();
158 //fd_ is ready to be written
160 connection::write_cb(int s)
166 PollMgr::Instance()->del_callback(fd_,CB_WRONLY);
170 PollMgr::Instance()->del_callback(fd_, CB_RDWR);
173 VERIFY(wpdu_.solong >= 0);
174 if (wpdu_.solong < wpdu_.sz) {
178 send_complete_.notify_one();
181 //fd_ is ready to be read
183 connection::read_cb(int s)
192 if (!rpdu_.buf || rpdu_.solong < rpdu_.sz) {
197 PollMgr::Instance()->del_callback(fd_,CB_RDWR);
199 send_complete_.notify_one();
202 if (rpdu_.buf && rpdu_.sz == rpdu_.solong) {
203 if (mgr_->got_pdu(this, rpdu_.buf, rpdu_.sz)) {
204 //chanmgr has successfully consumed the pdu
206 rpdu_.sz = rpdu_.solong = 0;
212 connection::writepdu()
214 VERIFY(wpdu_.solong >= 0);
215 if (wpdu_.solong == wpdu_.sz)
218 if (wpdu_.solong == 0) {
219 int sz = htonl(wpdu_.sz);
220 bcopy(&sz,wpdu_.buf,sizeof(sz));
222 int n = write(fd_, wpdu_.buf + wpdu_.solong, (wpdu_.sz-wpdu_.solong));
224 if (errno != EAGAIN) {
225 jsl_log(JSL_DBG_1, "connection::writepdu fd_ %d failure errno=%d\n", fd_, errno);
229 return (errno == EAGAIN);
236 connection::readpdu()
240 int n = read(fd_, &sz1, sizeof(sz1));
247 VERIFY(errno!=EAGAIN);
251 if (n >0 && n!= sizeof(sz)) {
252 jsl_log(JSL_DBG_OFF, "connection::readpdu short read of sz\n");
259 char *tmpb = (char *)&sz1;
260 jsl_log(JSL_DBG_2, "connection::readpdu read pdu TOO BIG %d network order=%x %x %x %x %x\n", sz,
261 sz1, tmpb[0],tmpb[1],tmpb[2],tmpb[3]);
266 VERIFY(rpdu_.buf == NULL);
267 rpdu_.buf = (char *)malloc(sz+sizeof(sz));
269 bcopy(&sz1,rpdu_.buf,sizeof(sz));
270 rpdu_.solong = sizeof(sz);
273 int n = read(fd_, rpdu_.buf + rpdu_.solong, rpdu_.sz - rpdu_.solong);
280 rpdu_.sz = rpdu_.solong = 0;
281 return (errno == EAGAIN);
287 tcpsconn::tcpsconn(chanmgr *m1, int port, int lossytest)
288 : mgr_(m1), lossy_(lossytest)
290 struct sockaddr_in sin;
291 memset(&sin, 0, sizeof(sin));
292 sin.sin_family = AF_INET;
293 sin.sin_port = htons(port);
295 tcp_ = socket(AF_INET, SOCK_STREAM, 0);
297 perror("tcpsconn::tcpsconn accept_loop socket:");
302 setsockopt(tcp_, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
303 setsockopt(tcp_, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes));
305 if(bind(tcp_, (sockaddr *)&sin, sizeof(sin)) < 0){
306 perror("accept_loop tcp bind:");
310 if(listen(tcp_, 1000) < 0) {
311 perror("tcpsconn::tcpsconn listen:");
315 socklen_t addrlen = sizeof(sin);
316 VERIFY(getsockname(tcp_, (sockaddr *)&sin, &addrlen) == 0);
317 port_ = ntohs(sin.sin_port);
319 jsl_log(JSL_DBG_2, "tcpsconn::tcpsconn listen on %d %d\n", port_,
322 if (pipe(pipe_) < 0) {
323 perror("accept_loop pipe:");
327 int flags = fcntl(pipe_[0], F_GETFL, NULL);
329 fcntl(pipe_[0], F_SETFL, flags);
331 th_ = std::thread(&tcpsconn::accept_conn, this);
334 tcpsconn::~tcpsconn()
336 VERIFY(close(pipe_[1]) == 0);
339 //close all the active connections
340 std::map<int, connection *>::iterator i;
341 for (i = conns_.begin(); i != conns_.end(); i++) {
342 i->second->closeconn();
348 tcpsconn::process_accept()
351 socklen_t slen = sizeof(sin);
352 int s1 = accept(tcp_, (sockaddr *)&sin, &slen);
354 perror("tcpsconn::accept_conn error");
355 throw thread_exit_exception();
358 jsl_log(JSL_DBG_2, "accept_loop got connection fd=%d %s:%d\n",
359 s1, inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
360 connection *ch = new connection(mgr_, s1, lossy_);
362 // garbage collect all dead connections with refcount of 1
363 std::map<int, connection *>::iterator i;
364 for (i = conns_.begin(); i != conns_.end();) {
365 if (i->second->isdead() && i->second->ref() == 1) {
366 jsl_log(JSL_DBG_2, "accept_loop garbage collected fd=%d\n",
367 i->second->channo());
369 // Careful not to reuse i right after erase. (i++) will
370 // be evaluated before the erase call because in C++,
371 // there is a sequence point before a function call.
372 // See http://en.wikipedia.org/wiki/Sequence_point.
378 conns_[ch->channo()] = ch;
382 tcpsconn::accept_conn()
385 int max_fd = pipe_[0] > tcp_ ? pipe_[0] : tcp_;
391 FD_SET(pipe_[0], &rfds);
394 int ret = select(max_fd+1, &rfds, NULL, NULL, NULL);
397 if (errno == EINTR) {
400 perror("accept_conn select:");
401 jsl_log(JSL_DBG_OFF, "tcpsconn::accept_conn failure errno %d\n",errno);
406 if (FD_ISSET(pipe_[0], &rfds)) {
411 else if (FD_ISSET(tcp_, &rfds)) {
418 catch (thread_exit_exception e)
425 connect_to_dst(const sockaddr_in &dst, chanmgr *mgr, int lossy)
427 int s= socket(AF_INET, SOCK_STREAM, 0);
429 setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes));
430 if(connect(s, (sockaddr*)&dst, sizeof(dst)) < 0) {
431 jsl_log(JSL_DBG_1, "rpcc::connect_to_dst failed to %s:%d\n",
432 inet_ntoa(dst.sin_addr), (int)ntohs(dst.sin_port));
436 jsl_log(JSL_DBG_2, "connect_to_dst fd=%d to dst %s:%d\n",
437 s, inet_ntoa(dst.sin_addr), (int)ntohs(dst.sin_port));
438 return new connection(mgr, s, lossy);