1 #include "connection.h"
2 #include "rpc_protocol.h"
7 #include <netinet/tcp.h>
9 #include <sys/socket.h>
12 connection::connection(chanmgr *m1, int f1, int l1)
13 : mgr_(m1), fd_(f1), lossy_(l1)
15 int flags = fcntl(fd_, F_GETFL, NULL);
16 fcntl(fd_, F_SETFL, flags | O_NONBLOCK);
18 signal(SIGPIPE, SIG_IGN);
20 create_time_ = steady_clock::now();
22 PollMgr::Instance()->add_callback(fd_, CB_RDONLY, this);
25 connection::~connection() {
27 VERIFY(!wpdu_.buf.size());
31 void connection::incref() {
36 bool connection::isdead() {
41 void connection::closeconn() {
47 shutdown(fd_,SHUT_RDWR);
49 //after block_remove_fd, select will never wait on fd_
50 //and no callbacks will be active
51 PollMgr::Instance()->block_remove_fd(fd_);
54 void connection::decref() {
69 int connection::compare(connection *another) {
70 if (create_time_ > another->create_time_)
72 if (create_time_ < another->create_time_)
77 bool connection::send(const string & b) {
81 while (!dead_ && wpdu_.buf.size())
92 if ((random()%100) < lossy_) {
93 IF_LEVEL(1) LOG("send LOSSY TEST shutdown fd_ " << fd_);
94 shutdown(fd_,SHUT_RDWR);
101 PollMgr::Instance()->block_remove_fd(fd_);
103 } else if (wpdu_.solong != wpdu_.buf.size()) {
104 // should be rare to need to explicitly add write callback
105 PollMgr::Instance()->add_callback(fd_, CB_WRONLY, this);
106 while (!dead_ && wpdu_.solong != size_t_max && wpdu_.solong < wpdu_.buf.size())
107 send_complete_.wait(ml);
109 bool ret = (!dead_ && wpdu_.solong == wpdu_.buf.size());
113 send_wait_.notify_all();
117 //fd_ is ready to be written
118 void connection::write_cb(int s) {
122 if (wpdu_.buf.size() == 0) {
123 PollMgr::Instance()->del_callback(fd_,CB_WRONLY);
127 PollMgr::Instance()->del_callback(fd_, CB_RDWR);
130 VERIFY(wpdu_.solong != size_t_max);
131 if (wpdu_.solong < wpdu_.buf.size()) {
135 send_complete_.notify_one();
138 //fd_ is ready to be read
139 void connection::read_cb(int s) {
146 IF_LEVEL(5) LOG("got data on fd " << s);
149 if (!rpdu_.buf.size() || rpdu_.solong < rpdu_.buf.size()) {
154 IF_LEVEL(5) LOG("readpdu on fd " << s << " failed; dying");
155 PollMgr::Instance()->del_callback(fd_,CB_RDWR);
157 send_complete_.notify_one();
160 if (rpdu_.buf.size() && rpdu_.buf.size() == rpdu_.solong) {
161 if (mgr_->got_pdu(this, rpdu_.buf)) {
162 //chanmgr has successfully consumed the pdu
169 bool connection::writepdu() {
170 VERIFY(wpdu_.solong != size_t_max);
171 if (wpdu_.solong == wpdu_.buf.size())
174 ssize_t n = write(fd_, &wpdu_.buf[wpdu_.solong], (wpdu_.buf.size()-wpdu_.solong));
176 if (errno != EAGAIN) {
177 IF_LEVEL(1) LOG("writepdu fd_ " << fd_ << " failure errno=" << errno);
178 wpdu_.solong = size_t_max;
181 return (errno == EAGAIN);
183 wpdu_.solong += (size_t)n;
187 bool connection::readpdu() {
188 IF_LEVEL(5) LOG("the receive buffer has length " << rpdu_.buf.size());
189 if (!rpdu_.buf.size()) {
191 ssize_t n = read(fd_, &sz1, sizeof(sz1));
198 VERIFY(errno!=EAGAIN);
202 if (n > 0 && n != sizeof(sz1)) {
203 IF_LEVEL(0) LOG("short read of sz");
207 size_t sz = ntoh(sz1);
210 IF_LEVEL(2) LOG("read pdu TOO BIG " << sz << " network order=" << hex << sz1);
214 IF_LEVEL(5) LOG("read size of datagram = " << sz);
216 VERIFY(rpdu_.buf.size() == 0);
217 rpdu_.buf = string(sz+sizeof(sz1), 0);
218 rpdu_.solong = sizeof(sz1);
221 ssize_t n = read(fd_, &rpdu_.buf[rpdu_.solong], rpdu_.buf.size() - rpdu_.solong);
223 IF_LEVEL(5) LOG("read " << n << " bytes");
230 return (errno == EAGAIN);
232 rpdu_.solong += (size_t)n;
236 tcpsconn::tcpsconn(chanmgr *m1, in_port_t port, int lossytest)
237 : mgr_(m1), lossy_(lossytest)
239 struct sockaddr_in sin;
240 memset(&sin, 0, sizeof(sin));
241 sin.sin_family = AF_INET;
242 sin.sin_port = hton(port);
244 tcp_ = socket(AF_INET, SOCK_STREAM, 0);
246 perror("accept_loop socket:");
251 setsockopt(tcp_, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
252 setsockopt(tcp_, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes));
254 // careful to exactly match type signature of bind arguments so we don't
255 // get std::bind instead
256 if (bind(tcp_, (const struct sockaddr *)&sin, (socklen_t)sizeof(sin)) < 0) {
257 perror("accept_loop tcp bind:");
261 if (listen(tcp_, 1000) < 0) {
266 socklen_t addrlen = sizeof(sin);
267 VERIFY(getsockname(tcp_, (sockaddr *)&sin, &addrlen) == 0);
268 port_ = ntoh(sin.sin_port);
270 IF_LEVEL(2) LOG("listen on " << port_ << " " << sin.sin_port);
272 if (pipe(pipe_) < 0) {
273 perror("accept_loop pipe:");
277 int flags = fcntl(pipe_[0], F_GETFL, NULL);
279 fcntl(pipe_[0], F_SETFL, flags);
281 th_ = thread(&tcpsconn::accept_conn, this);
284 tcpsconn::~tcpsconn()
286 VERIFY(close(pipe_[1]) == 0);
289 //close all the active connections
290 map<int, connection *>::iterator i;
291 for (i = conns_.begin(); i != conns_.end(); i++) {
292 i->second->closeconn();
297 void tcpsconn::process_accept() {
299 socklen_t slen = sizeof(sin);
300 int s1 = accept(tcp_, (sockaddr *)&sin, &slen);
302 perror("tcpsconn::accept_conn error");
303 throw thread_exit_exception();
306 IF_LEVEL(2) LOG("accept_loop got connection fd=" << s1 << " " << inet_ntoa(sin.sin_addr) << ":" << ntoh(sin.sin_port));
307 connection *ch = new connection(mgr_, s1, lossy_);
309 // garbage collect all dead connections with refcount of 1
310 for (auto i = conns_.begin(); i != conns_.end();) {
311 if (i->second->isdead() && i->second->ref() == 1) {
312 IF_LEVEL(2) LOG("accept_loop garbage collected fd=" << i->second->channo());
314 // Careful not to reuse i right after erase. (i++) will
315 // be evaluated before the erase call because in C++,
316 // there is a sequence point before a function call.
317 // See http://en.wikipedia.org/wiki/Sequence_point.
323 conns_[ch->channo()] = ch;
326 void tcpsconn::accept_conn() {
328 int max_fd = pipe_[0] > tcp_ ? pipe_[0] : tcp_;
333 FD_SET(pipe_[0], &rfds);
336 int ret = select(max_fd+1, &rfds, NULL, NULL, NULL);
339 if (errno == EINTR) {
342 perror("accept_conn select:");
343 IF_LEVEL(0) LOG("accept_conn failure errno " << errno);
348 if (FD_ISSET(pipe_[0], &rfds)) {
353 else if (FD_ISSET(tcp_, &rfds)) {
360 catch (thread_exit_exception e)
365 connection * connect_to_dst(const sockaddr_in &dst, chanmgr *mgr, int lossy) {
366 int s = socket(AF_INET, SOCK_STREAM, 0);
368 setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes));
369 if (connect(s, (sockaddr*)&dst, sizeof(dst)) < 0) {
370 IF_LEVEL(1) LOG_NONMEMBER("failed to " << inet_ntoa(dst.sin_addr) << ":" << ntoh(dst.sin_port));
374 IF_LEVEL(2) LOG_NONMEMBER("connect_to_dst fd=" << s << " to dst " << inet_ntoa(dst.sin_addr) << ":" << ntoh(dst.sin_port));
375 return new connection(mgr, s, lossy);