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 if (wpdu_.solong == 0) {
175 rpc_sz_t sz = hton((rpc_sz_t)(wpdu_.buf.size() - sizeof(uint32_t)));
176 copy((const char *)&sz, (const char *)(&sz+1), &wpdu_.buf[0]);
178 ssize_t n = write(fd_, &wpdu_.buf[wpdu_.solong], (wpdu_.buf.size()-wpdu_.solong));
180 if (errno != EAGAIN) {
181 IF_LEVEL(1) LOG("writepdu fd_ " << fd_ << " failure errno=" << errno);
182 wpdu_.solong = size_t_max;
185 return (errno == EAGAIN);
187 wpdu_.solong += (size_t)n;
191 bool connection::readpdu() {
192 IF_LEVEL(5) LOG("the receive buffer has length " << rpdu_.buf.size());
193 if (!rpdu_.buf.size()) {
195 ssize_t n = read(fd_, &sz1, sizeof(sz1));
202 VERIFY(errno!=EAGAIN);
206 if (n > 0 && n != sizeof(sz1)) {
207 IF_LEVEL(0) LOG("short read of sz");
211 size_t sz = ntoh(sz1);
214 IF_LEVEL(2) LOG("read pdu TOO BIG " << sz << " network order=" << hex << sz1);
218 IF_LEVEL(5) LOG("read size of datagram = " << sz);
220 VERIFY(rpdu_.buf.size() == 0);
221 rpdu_.buf = string(sz+sizeof(sz1), 0);
222 copy((const char *)&sz1, (const char *)(&sz1 + 1), &rpdu_.buf[0]);
223 rpdu_.solong = sizeof(sz1);
226 ssize_t n = read(fd_, &rpdu_.buf[rpdu_.solong], rpdu_.buf.size() - rpdu_.solong);
228 IF_LEVEL(5) LOG("read " << n << " bytes");
235 return (errno == EAGAIN);
237 rpdu_.solong += (size_t)n;
241 tcpsconn::tcpsconn(chanmgr *m1, in_port_t port, int lossytest)
242 : mgr_(m1), lossy_(lossytest)
244 struct sockaddr_in sin;
245 memset(&sin, 0, sizeof(sin));
246 sin.sin_family = AF_INET;
247 sin.sin_port = hton(port);
249 tcp_ = socket(AF_INET, SOCK_STREAM, 0);
251 perror("accept_loop socket:");
256 setsockopt(tcp_, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
257 setsockopt(tcp_, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes));
259 // careful to exactly match type signature of bind arguments so we don't
260 // get std::bind instead
261 if (bind(tcp_, (const struct sockaddr *)&sin, (socklen_t)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_ = ntoh(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) << ":" << ntoh(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("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("failed to " << inet_ntoa(dst.sin_addr) << ":" << ntoh(dst.sin_port));
379 IF_LEVEL(2) LOG_NONMEMBER("connect_to_dst fd=" << s << " to dst " << inet_ntoa(dst.sin_addr) << ":" << ntoh(dst.sin_port));
380 return new connection(mgr, s, lossy);