From f2e9749c9340ceffe036890be485ec0a2ac128ed Mon Sep 17 00:00:00 2001 From: Vitaliy Filippov Date: Sun, 15 Dec 2019 01:52:08 +0300 Subject: [PATCH] Fix disconnection detection --- Makefile | 6 +++--- osd.cpp | 12 ++++++++++-- osd.h | 6 +++--- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 731c1224..e7c1c3a3 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ BLOCKSTORE_OBJS := allocator.o blockstore.o blockstore_init.o blockstore_open.o blockstore_journal.o blockstore_read.o \ - blockstore_write.o blockstore_sync.o blockstore_stable.o blockstore_flush.o crc32c.o ringloop.o timerfd_interval.o osd.o + blockstore_write.o blockstore_sync.o blockstore_stable.o blockstore_flush.o crc32c.o ringloop.o timerfd_interval.o CXXFLAGS := -g -O3 -Wall -Wno-sign-compare -Wno-comment -Wno-parentheses -Wno-pointer-arith -fPIC -fdiagnostics-color=always all: $(BLOCKSTORE_OBJS) test test_blockstore libfio_blockstore.so osd clean: @@ -8,8 +8,8 @@ crc32c.o: crc32c.c g++ $(CXXFLAGS) -c -o $@ $< %.o: %.cpp allocator.h blockstore_flush.h blockstore.h blockstore_init.h blockstore_journal.h crc32c.h ringloop.h xor.h timerfd_interval.h g++ $(CXXFLAGS) -c -o $@ $< -osd: $(BLOCKSTORE_OBJS) osd_main.cpp osd.h osd_ops.h - g++ $(CXXFLAGS) -ltcmalloc_minimal -luring -o osd osd_main.cpp $(BLOCKSTORE_OBJS) +osd: $(BLOCKSTORE_OBJS) osd_main.cpp osd.h osd_ops.h osd.o + g++ $(CXXFLAGS) -ltcmalloc_minimal -luring -o osd osd_main.cpp osd.o $(BLOCKSTORE_OBJS) test: test.cpp g++ $(CXXFLAGS) -o test -luring test.cpp test_blockstore: $(BLOCKSTORE_OBJS) test_blockstore.cpp diff --git a/osd.cpp b/osd.cpp index 8125304a..162844ec 100644 --- a/osd.cpp +++ b/osd.cpp @@ -83,6 +83,11 @@ osd_t::~osd_t() close(listen_fd); } +bool osd_t::shutdown() +{ + // TODO +} + void osd_t::loop() { if (wait_state == 0) @@ -132,6 +137,8 @@ int osd_t::handle_epoll_events() int peer_fd; while ((peer_fd = accept(listen_fd, (sockaddr*)&addr, &peer_addr_size)) >= 0) { + char peer_str[256]; + printf("osd: new client %d: connection from %s port %d\n", peer_fd, inet_ntop(AF_INET, &addr.sin_addr, peer_str, 256), ntohs(addr.sin_port)); fcntl(peer_fd, F_SETFL, fcntl(listen_fd, F_GETFL, 0) | O_NONBLOCK); clients[peer_fd] = { .peer_addr = addr, @@ -141,7 +148,7 @@ int osd_t::handle_epoll_events() // Add FD to epoll epoll_event ev; ev.data.fd = peer_fd; - ev.events = EPOLLIN | EPOLLHUP; + ev.events = EPOLLIN | EPOLLRDHUP; if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, peer_fd, &ev) < 0) { throw std::runtime_error(std::string("epoll_ctl: ") + strerror(errno)); @@ -157,9 +164,10 @@ int osd_t::handle_epoll_events() else { auto & cl = clients[events[i].data.fd]; - if (events[i].events & EPOLLHUP) + if (events[i].events & EPOLLRDHUP) { // Stop client + printf("osd: client %d disconnected\n", cl.peer_fd); stop_client(cl.peer_fd); } else if (!cl.read_ready) diff --git a/osd.h b/osd.h index 5e38f34a..880be859 100644 --- a/osd.h +++ b/osd.h @@ -63,6 +63,8 @@ class osd_t { // config + std::string bind_address; + int bind_port, listen_backlog; int client_queue_depth = 128; // fields @@ -75,9 +77,6 @@ class osd_t int listen_fd = 0; ring_consumer_t consumer; - std::string bind_address; - int bind_port, listen_backlog; - std::unordered_map clients; std::vector read_ready_clients; std::vector write_ready_clients; @@ -94,4 +93,5 @@ class osd_t public: osd_t(blockstore_config_t & config, blockstore *bs, ring_loop_t *ringloop); ~osd_t(); + bool shutdown(); };