1 #include "connection.h"
2 #include "rpc_protocol.h"
6 #include <netinet/tcp.h>
10 connection::connection(chanmgr *m1, int f1, int l1)
11 : mgr_(m1), fd_(f1), lossy_(l1)
13 fd_.flags() |= O_NONBLOCK;
15 signal(SIGPIPE, SIG_IGN);
17 create_time_ = steady_clock::now();
19 PollMgr::Instance().add_callback(fd_, CB_RDONLY, this);
22 connection::~connection() {
24 VERIFY(!wpdu_.buf.size());
27 void connection::incref() {
32 bool connection::isdead() {
37 void connection::closeconn() {
43 shutdown(fd_,SHUT_RDWR);
45 //after block_remove_fd, select will never wait on fd_
46 //and no callbacks will be active
47 PollMgr::Instance().block_remove_fd(fd_);
50 void connection::decref() {
65 int connection::compare(connection *another) {
66 if (create_time_ > another->create_time_)
68 if (create_time_ < another->create_time_)
73 bool connection::send(const string & b) {
77 while (!dead_ && wpdu_.buf.size())
88 if ((random()%100) < lossy_) {
89 IF_LEVEL(1) LOG("send LOSSY TEST shutdown fd_ " << fd_);
90 shutdown(fd_,SHUT_RDWR);
97 PollMgr::Instance().block_remove_fd(fd_);
99 } else if (wpdu_.solong != wpdu_.buf.size()) {
100 // should be rare to need to explicitly add write callback
101 PollMgr::Instance().add_callback(fd_, CB_WRONLY, this);
102 while (!dead_ && wpdu_.solong != size_t_max && wpdu_.solong < wpdu_.buf.size())
103 send_complete_.wait(ml);
105 bool ret = (!dead_ && wpdu_.solong == wpdu_.buf.size());
109 send_wait_.notify_all();
113 //fd_ is ready to be written
114 void connection::write_cb(int s) {
118 if (wpdu_.buf.size() == 0) {
119 PollMgr::Instance().del_callback(fd_,CB_WRONLY);
123 PollMgr::Instance().del_callback(fd_, CB_RDWR);
126 VERIFY(wpdu_.solong != size_t_max);
127 if (wpdu_.solong < wpdu_.buf.size()) {
131 send_complete_.notify_one();
134 //fd_ is ready to be read
135 void connection::read_cb(int s) {
142 IF_LEVEL(5) LOG("got data on fd " << s);
145 if (!rpdu_.buf.size() || rpdu_.solong < rpdu_.buf.size()) {
150 IF_LEVEL(5) LOG("readpdu on fd " << s << " failed; dying");
151 PollMgr::Instance().del_callback(fd_,CB_RDWR);
153 send_complete_.notify_one();
156 if (rpdu_.buf.size() && rpdu_.buf.size() == rpdu_.solong) {
157 if (mgr_->got_pdu(this, rpdu_.buf)) {
158 //chanmgr has successfully consumed the pdu
165 bool connection::writepdu() {
166 VERIFY(wpdu_.solong != size_t_max);
167 if (wpdu_.solong == wpdu_.buf.size())
170 ssize_t n = write(fd_, &wpdu_.buf[wpdu_.solong], (wpdu_.buf.size()-wpdu_.solong));
172 if (errno != EAGAIN) {
173 IF_LEVEL(1) LOG("writepdu fd_ " << fd_ << " failure errno=" << errno);
174 wpdu_.solong = size_t_max;
177 return (errno == EAGAIN);
179 wpdu_.solong += (size_t)n;
183 bool connection::readpdu() {
184 IF_LEVEL(5) LOG("the receive buffer has length " << rpdu_.buf.size());
185 if (!rpdu_.buf.size()) {
187 ssize_t n = fd_.read(sz1);
193 VERIFY(errno!=EAGAIN);
197 if (n > 0 && n != sizeof(sz1)) {
198 IF_LEVEL(0) LOG("short read of sz");
202 size_t sz = ntoh(sz1);
205 IF_LEVEL(2) LOG("read pdu TOO BIG " << sz << " network order=" << hex << sz1);
209 IF_LEVEL(5) LOG("read size of datagram = " << sz);
211 VERIFY(rpdu_.buf.size() == 0);
212 rpdu_.buf = string(sz+sizeof(sz1), 0);
213 rpdu_.solong = sizeof(sz1);
216 ssize_t n = fd_.read(&rpdu_.buf[rpdu_.solong], rpdu_.buf.size() - rpdu_.solong);
218 IF_LEVEL(5) LOG("read " << n << " bytes");
225 return (errno == EAGAIN);
227 rpdu_.solong += (size_t)n;
231 tcpsconn::tcpsconn(chanmgr *m1, in_port_t port, int lossytest)
232 : tcp_(socket(AF_INET, SOCK_STREAM, 0)), mgr_(m1), lossy_(lossytest)
234 struct sockaddr_in sin;
235 memset(&sin, 0, sizeof(sin));
236 sin.sin_family = AF_INET;
237 sin.sin_port = hton(port);
239 tcp_.setsockopt(SOL_SOCKET, SO_REUSEADDR, (int)1);
240 tcp_.setsockopt(IPPROTO_TCP, TCP_NODELAY, (int)1);
242 struct timeval timeout = {0, 50000};
244 if (tcp_.setsockopt(SOL_SOCKET, SO_RCVTIMEO, timeout) < 0)
245 perror("accept_loop setsockopt");
247 if (tcp_.setsockopt(SOL_SOCKET, SO_SNDTIMEO, timeout) < 0)
248 perror("accept_loop setsockopt");
250 // careful to exactly match type signature of bind arguments so we don't
251 // get std::bind instead
252 if (bind((int)tcp_, (const struct sockaddr *)&sin, (socklen_t)sizeof(sin)) < 0) {
253 perror("accept_loop bind");
257 if (listen(tcp_, 1000) < 0) {
258 perror("accept_loop listen");
262 socklen_t addrlen = sizeof(sin);
263 VERIFY(getsockname(tcp_, (sockaddr *)&sin, &addrlen) == 0);
264 port_ = ntoh(sin.sin_port);
266 IF_LEVEL(2) LOG("listen on " << port_ << " " << sin.sin_port);
270 pipe_[0].flags() |= O_NONBLOCK;
272 th_ = thread(&tcpsconn::accept_conn, this);
275 tcpsconn::~tcpsconn()
280 // close all the active connections
281 map<int, connection *>::iterator i;
282 for (i = conns_.begin(); i != conns_.end(); i++) {
283 i->second->closeconn();
288 void tcpsconn::process_accept() {
290 socklen_t slen = sizeof(sin);
291 int s1 = accept(tcp_, (sockaddr *)&sin, &slen);
293 perror("tcpsconn::accept_conn error");
294 throw thread_exit_exception();
297 IF_LEVEL(2) LOG("accept_loop got connection fd=" << s1 << " " << inet_ntoa(sin.sin_addr) << ":" << ntoh(sin.sin_port));
298 connection *ch = new connection(mgr_, s1, lossy_);
300 // garbage collect all dead connections with refcount of 1
301 for (auto i = conns_.begin(); i != conns_.end();) {
302 if (i->second->isdead() && i->second->ref() == 1) {
303 IF_LEVEL(2) LOG("accept_loop garbage collected fd=" << i->second->channo());
305 // Careful not to reuse i right after erase. (i++) will
306 // be evaluated before the erase call because in C++,
307 // there is a sequence point before a function call.
308 // See http://en.wikipedia.org/wiki/Sequence_point.
314 conns_[ch->channo()] = ch;
317 void tcpsconn::accept_conn() {
319 int max_fd = max((int)pipe_[0], (int)tcp_);
323 FD_SET(pipe_[0], &rfds);
326 int ret = select(max_fd+1, &rfds, NULL, NULL, NULL);
328 if (ret < 0 && errno == EINTR)
331 perror("accept_conn select:");
332 IF_LEVEL(0) LOG("accept_conn failure errno " << errno);
336 if (FD_ISSET(pipe_[0], &rfds))
339 if (!FD_ISSET(tcp_, &rfds))
344 } catch (thread_exit_exception e) {
350 connection * connect_to_dst(const sockaddr_in &dst, chanmgr *mgr, int lossy) {
351 int s = socket(AF_INET, SOCK_STREAM, 0);
353 setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes));
354 if (connect(s, (sockaddr*)&dst, sizeof(dst)) < 0) {
355 IF_LEVEL(1) LOG_NONMEMBER("failed to " << inet_ntoa(dst.sin_addr) << ":" << ntoh(dst.sin_port));
359 IF_LEVEL(2) LOG_NONMEMBER("connect_to_dst fd=" << s << " to dst " << inet_ntoa(dst.sin_addr) << ":" << ntoh(dst.sin_port));
360 return new connection(mgr, s, lossy);