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"
8 #include <netinet/tcp.h>
10 #include <sys/socket.h>
12 #define MAX_PDU (10<<20) //maximum PDF is 10M
14 connection::connection(chanmgr *m1, int f1, int l1)
15 : mgr_(m1), fd_(f1), lossy_(l1)
17 int flags = fcntl(fd_, F_GETFL, NULL);
18 fcntl(fd_, F_SETFL, flags | O_NONBLOCK);
20 signal(SIGPIPE, SIG_IGN);
22 create_time_ = steady_clock::now();
24 PollMgr::Instance()->add_callback(fd_, CB_RDONLY, this);
27 connection::~connection() {
29 VERIFY(!wpdu_.buf.size());
33 void connection::incref() {
38 bool connection::isdead() {
43 void connection::closeconn() {
49 shutdown(fd_,SHUT_RDWR);
51 //after block_remove_fd, select will never wait on fd_
52 //and no callbacks will be active
53 PollMgr::Instance()->block_remove_fd(fd_);
56 void connection::decref() {
71 int connection::compare(connection *another) {
72 if (create_time_ > another->create_time_)
74 if (create_time_ < another->create_time_)
79 bool connection::send(const string & b) {
83 while (!dead_ && wpdu_.buf.size())
94 if ((random()%100) < lossy_) {
95 IF_LEVEL(1) LOG("connection::send LOSSY TEST shutdown fd_ " << fd_);
96 shutdown(fd_,SHUT_RDWR);
103 PollMgr::Instance()->block_remove_fd(fd_);
105 } else if (wpdu_.solong != wpdu_.buf.size()) {
106 // should be rare to need to explicitly add write callback
107 PollMgr::Instance()->add_callback(fd_, CB_WRONLY, this);
108 while (!dead_ && wpdu_.solong != size_t_max && wpdu_.solong < wpdu_.buf.size())
109 send_complete_.wait(ml);
111 bool ret = (!dead_ && wpdu_.solong == wpdu_.buf.size());
115 send_wait_.notify_all();
119 //fd_ is ready to be written
120 void connection::write_cb(int s) {
124 if (wpdu_.buf.size() == 0) {
125 PollMgr::Instance()->del_callback(fd_,CB_WRONLY);
129 PollMgr::Instance()->del_callback(fd_, CB_RDWR);
132 VERIFY(wpdu_.solong != size_t_max);
133 if (wpdu_.solong < wpdu_.buf.size()) {
137 send_complete_.notify_one();
140 //fd_ is ready to be read
141 void connection::read_cb(int s) {
148 IF_LEVEL(5) LOG("got data on fd " << s);
151 if (!rpdu_.buf.size() || rpdu_.solong < rpdu_.buf.size()) {
156 IF_LEVEL(5) LOG("readpdu on fd " << s << " failed; dying");
157 PollMgr::Instance()->del_callback(fd_,CB_RDWR);
159 send_complete_.notify_one();
162 if (rpdu_.buf.size() && rpdu_.buf.size() == rpdu_.solong) {
163 if (mgr_->got_pdu(this, rpdu_.buf)) {
164 //chanmgr has successfully consumed the pdu
171 bool connection::writepdu() {
172 VERIFY(wpdu_.solong != size_t_max);
173 if (wpdu_.solong == wpdu_.buf.size())
176 if (wpdu_.solong == 0) {
177 uint32_t sz = htonl((uint32_t)wpdu_.buf.size() - sizeof(uint32_t));
178 copy((const char *)&sz, (const char *)(&sz+1), &wpdu_.buf[0]);
180 ssize_t n = write(fd_, &wpdu_.buf[wpdu_.solong], (wpdu_.buf.size()-wpdu_.solong));
182 if (errno != EAGAIN) {
183 IF_LEVEL(1) LOG("connection::writepdu fd_ " << fd_ << " failure errno=" << errno);
184 wpdu_.solong = size_t_max;
187 return (errno == EAGAIN);
189 wpdu_.solong += (size_t)n;
193 bool connection::readpdu() {
194 IF_LEVEL(5) LOG("the receive buffer has length " << rpdu_.buf.size());
195 if (!rpdu_.buf.size()) {
197 ssize_t n = read(fd_, &sz1, sizeof(sz1));
204 VERIFY(errno!=EAGAIN);
208 if (n > 0 && n != sizeof(sz1)) {
209 IF_LEVEL(0) LOG("short read of sz");
213 size_t sz = ntohl(sz1);
216 IF_LEVEL(2) LOG("read pdu TOO BIG " << sz << " network order=" << hex << sz1);
220 IF_LEVEL(5) LOG("read size of datagram = " << sz);
222 VERIFY(rpdu_.buf.size() == 0);
223 rpdu_.buf = string(sz+sizeof(sz1), 0);
224 copy((const char *)&sz1, (const char *)(&sz1 + 1), &rpdu_.buf[0]);
225 rpdu_.solong = sizeof(sz1);
228 ssize_t n = read(fd_, &rpdu_.buf[rpdu_.solong], rpdu_.buf.size() - rpdu_.solong);
230 IF_LEVEL(5) LOG("read " << n << " bytes");
237 return (errno == EAGAIN);
239 rpdu_.solong += (size_t)n;
243 tcpsconn::tcpsconn(chanmgr *m1, unsigned int port, int lossytest)
244 : mgr_(m1), lossy_(lossytest)
246 struct sockaddr_in sin;
247 memset(&sin, 0, sizeof(sin));
248 sin.sin_family = AF_INET;
249 sin.sin_port = htons(port);
251 tcp_ = socket(AF_INET, SOCK_STREAM, 0);
253 perror("accept_loop socket:");
258 setsockopt(tcp_, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
259 setsockopt(tcp_, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes));
261 if (bind(tcp_, (sockaddr *)&sin, sizeof(sin)) < 0) {
262 perror("accept_loop tcp bind:");
266 if (listen(tcp_, 1000) < 0) {
271 socklen_t addrlen = sizeof(sin);
272 VERIFY(getsockname(tcp_, (sockaddr *)&sin, &addrlen) == 0);
273 port_ = ntohs(sin.sin_port);
275 IF_LEVEL(2) LOG("listen on " << port_ << " " << sin.sin_port);
277 if (pipe(pipe_) < 0) {
278 perror("accept_loop pipe:");
282 int flags = fcntl(pipe_[0], F_GETFL, NULL);
284 fcntl(pipe_[0], F_SETFL, flags);
286 th_ = thread(&tcpsconn::accept_conn, this);
289 tcpsconn::~tcpsconn()
291 VERIFY(close(pipe_[1]) == 0);
294 //close all the active connections
295 map<int, connection *>::iterator i;
296 for (i = conns_.begin(); i != conns_.end(); i++) {
297 i->second->closeconn();
302 void tcpsconn::process_accept() {
304 socklen_t slen = sizeof(sin);
305 int s1 = accept(tcp_, (sockaddr *)&sin, &slen);
307 perror("tcpsconn::accept_conn error");
308 throw thread_exit_exception();
311 IF_LEVEL(2) LOG("accept_loop got connection fd=" << s1 << " " << inet_ntoa(sin.sin_addr) << ":" << ntohs(sin.sin_port));
312 connection *ch = new connection(mgr_, s1, lossy_);
314 // garbage collect all dead connections with refcount of 1
315 for (auto i = conns_.begin(); i != conns_.end();) {
316 if (i->second->isdead() && i->second->ref() == 1) {
317 IF_LEVEL(2) LOG("accept_loop garbage collected fd=" << i->second->channo());
319 // Careful not to reuse i right after erase. (i++) will
320 // be evaluated before the erase call because in C++,
321 // there is a sequence point before a function call.
322 // See http://en.wikipedia.org/wiki/Sequence_point.
328 conns_[ch->channo()] = ch;
331 void tcpsconn::accept_conn() {
333 int max_fd = pipe_[0] > tcp_ ? pipe_[0] : tcp_;
338 FD_SET(pipe_[0], &rfds);
341 int ret = select(max_fd+1, &rfds, NULL, NULL, NULL);
344 if (errno == EINTR) {
347 perror("accept_conn select:");
348 IF_LEVEL(0) LOG("tcpsconn::accept_conn failure errno " << errno);
353 if (FD_ISSET(pipe_[0], &rfds)) {
358 else if (FD_ISSET(tcp_, &rfds)) {
365 catch (thread_exit_exception e)
370 connection * connect_to_dst(const sockaddr_in &dst, chanmgr *mgr, int lossy) {
371 int s = socket(AF_INET, SOCK_STREAM, 0);
373 setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes));
374 if (connect(s, (sockaddr*)&dst, sizeof(dst)) < 0) {
375 IF_LEVEL(1) LOG_NONMEMBER("rpcc::connect_to_dst failed to " << inet_ntoa(dst.sin_addr) << ":" << ntohs(dst.sin_port));
379 IF_LEVEL(2) LOG_NONMEMBER("connect_to_dst fd=" << s << " to dst " << inet_ntoa(dst.sin_addr) << ":" << ntohs(dst.sin_port));
380 return new connection(mgr, s, lossy);