It's not necessary to use EPOLL_CTL_DEL after close()

This commit is contained in:
Ian Gulliver
2015-02-05 20:57:01 +00:00
parent 524434813c
commit c91b64f892
3 changed files with 42 additions and 20 deletions

View File

@@ -1,5 +1,7 @@
#include <fstream>
#include <iostream> #include <iostream>
#include <getopt.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@@ -8,7 +10,33 @@
#include "crypto.h" #include "crypto.h"
int main() { static const struct option long_options[] = {
{"secret_key_filename", required_argument, NULL, 's'},
};
int main(int argc, char *argv[]) {
std::string secret_key_filename;
{
int option, option_index;
while ((option = getopt_long(argc, argv, "s:", long_options, &option_index)) != -1) {
switch (option) {
case 's':
secret_key_filename = optarg;
break;
}
}
}
std::string secret_key;
{
std::fstream secret_key_file(secret_key_filename, std::fstream::in);
if (secret_key_file.fail()) {
std::cerr << "Failed to open secret key file" << std::endl;
return 1;
}
secret_key_file >> secret_key;
}
int fd = socket(PF_INET6, SOCK_STREAM, 0); int fd = socket(PF_INET6, SOCK_STREAM, 0);
int optval = 1; int optval = 1;
@@ -29,6 +57,6 @@ int main() {
return 1; return 1;
} }
CryptoPubServer server(fd, "abcde"); CryptoPubServer server(fd, secret_key);
server.Loop(); server.Loop();
} }

View File

@@ -50,19 +50,19 @@ CryptoPubServer::~CryptoPubServer() {
} }
} }
int CryptoPubServer::OnReadable() { void CryptoPubServer::OnReadable() {
struct sockaddr_in6 client; struct sockaddr_in6 client;
socklen_t client_len = sizeof(client); socklen_t client_len = sizeof(client);
auto client_fd = accept(fd_, (struct sockaddr*) &client, &client_len); auto client_fd = accept(fd_, (struct sockaddr*) &client, &client_len);
if (client_fd == -1) { if (client_fd == -1) {
perror("accept"); perror("accept");
return -1; return;
} }
char buf[128]; char buf[128];
inet_ntop(AF_INET6, &client.sin6_addr, buf, 128); inet_ntop(AF_INET6, &client.sin6_addr, buf, 128);
std::cerr << "New connection from [" << buf << "]:" << ntohs(client.sin6_port) << std::endl; std::cerr << "New connection from [" << buf << "]:" << ntohs(client.sin6_port) << std::endl;
auto peer = new CryptoPubPeer(client_fd, secret_key_); auto peer = new CryptoPubServerConnection(client_fd, secret_key_);
epoll_event event = { epoll_event event = {
.events = EPOLLIN, .events = EPOLLIN,
@@ -71,8 +71,6 @@ int CryptoPubServer::OnReadable() {
}, },
}; };
epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, client_fd, &event); epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, client_fd, &event);
return -1;
} }
void CryptoPubServer::Loop() { void CryptoPubServer::Loop() {
@@ -82,26 +80,22 @@ void CryptoPubServer::Loop() {
for (int i = 0; i < num_events; i++) { for (int i = 0; i < num_events; i++) {
if (events[i].events & EPOLLIN) { if (events[i].events & EPOLLIN) {
auto obj = (CryptoBase*) events[i].data.ptr; auto obj = (CryptoBase*) events[i].data.ptr;
auto fd = obj->OnReadable(); obj->OnReadable();
if (fd >= 0) {
epoll_ctl(epoll_fd_, EPOLL_CTL_DEL, fd, &events[i]);
}
} }
} }
} }
} }
CryptoPubPeer::CryptoPubPeer(const int fd, const std::string secret_key) CryptoPubServerConnection::CryptoPubServerConnection(const int fd, const std::string secret_key)
: CryptoBase(fd), : CryptoBase(fd),
secret_key_(secret_key) { secret_key_(secret_key) {
} }
int CryptoPubPeer::OnReadable() { void CryptoPubServerConnection::OnReadable() {
char buf[128]; char buf[128];
if (read(fd_, buf, 128) == 0) { if (read(fd_, buf, 128) == 0) {
auto fd = fd_;
delete this; delete this;
return fd; return;
} }
} }

View File

@@ -9,16 +9,16 @@ class CryptoBase {
static void GenKey(std::string* key); static void GenKey(std::string* key);
static void GenKeyPair(std::string* secret_key, std::string* public_key); static void GenKeyPair(std::string* secret_key, std::string* public_key);
virtual int OnReadable() = 0; virtual void OnReadable() = 0;
protected: protected:
const int fd_; const int fd_;
}; };
class CryptoPubPeer : public CryptoBase { class CryptoPubServerConnection : public CryptoBase {
public: public:
CryptoPubPeer(const int fd, const std::string secret_key); CryptoPubServerConnection(const int fd, const std::string secret_key);
int OnReadable(); void OnReadable();
private: private:
const std::string secret_key_; const std::string secret_key_;
@@ -29,7 +29,7 @@ class CryptoPubServer : public CryptoBase {
public: public:
CryptoPubServer(const int fd, const std::string secret_key); CryptoPubServer(const int fd, const std::string secret_key);
~CryptoPubServer(); ~CryptoPubServer();
int OnReadable(); void OnReadable();
void Loop(); void Loop();
private: private: