Begin OSD
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
#include <sys/socket.h>
|
||||
#include <sys/epoll.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include "osd_ops.h"
|
||||
#include "ringloop.h"
|
||||
|
||||
class osd_t
|
||||
{
|
||||
int wait_state = 0;
|
||||
int epoll_fd = 0;
|
||||
int listen_fd = 0;
|
||||
ring_consumer_t consumer;
|
||||
|
||||
std::string bind_address;
|
||||
int bind_port, listen_backlog;
|
||||
ring_loop_t *ringloop;
|
||||
|
||||
void handle_epoll_events();
|
||||
public:
|
||||
osd_t(ring_loop_t *ringloop);
|
||||
~osd_t();
|
||||
void loop();
|
||||
};
|
||||
|
||||
class osd_client_t
|
||||
{
|
||||
int sock_fd;
|
||||
};
|
||||
|
||||
osd_t::osd_t(ring_loop_t *ringloop)
|
||||
{
|
||||
this->ringloop = ringloop;
|
||||
|
||||
listen_fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (listen_fd < 0)
|
||||
{
|
||||
throw std::runtime_error(std::string("socket: ") + strerror(errno));
|
||||
}
|
||||
|
||||
sockaddr_in addr;
|
||||
if ((int r = inet_pton(AF_INET, bind_address.c_str(), &addr.sin_addr)) != 1)
|
||||
{
|
||||
close(listen_fd);
|
||||
throw std::runtime_error("bind address "+bind_address+(r == 0 ? " is not valid" : ": no ipv4 support"));
|
||||
}
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = bind_port;
|
||||
|
||||
if (bind(listen_fd, &addr, sizeof(addr)) < 0)
|
||||
{
|
||||
close(listen_fd);
|
||||
throw std::runtime_error(std::string("bind: ") + strerror(errno));
|
||||
}
|
||||
|
||||
if (listen(listen_fd, listen_backlog) < 0)
|
||||
{
|
||||
close(listen_fd);
|
||||
throw std::runtime_error(std::string("listen: ") + strerror(errno));
|
||||
}
|
||||
|
||||
epoll_fd = epoll_create(1);
|
||||
if (epoll_fd < 0)
|
||||
{
|
||||
close(listen_fd);
|
||||
throw std::runtime_error(std::string("epoll_create: ") + strerror(errno));
|
||||
}
|
||||
|
||||
struct epoll_event ev;
|
||||
ev.data.fd = listen_fd;
|
||||
ev.events = EPOLLIN | EPOLLET | EPOLLONESHOT;
|
||||
if (epoll_ctl(epfd, EPOLL_CTL_ADD, sfd, &ev) < 0)
|
||||
{
|
||||
throw std::runtime_error(std::string("epoll_ctl: ") + strerror(errno));
|
||||
}
|
||||
|
||||
consumer.loop = [this]() { loop(); };
|
||||
ringloop->register_consumer(consumer);
|
||||
}
|
||||
|
||||
osd_t::~osd_t()
|
||||
{
|
||||
ringloop->unregister_consumer(consumer);
|
||||
close(epoll_fd);
|
||||
close(listen_fd);
|
||||
}
|
||||
|
||||
void osd_t::loop()
|
||||
{
|
||||
if (wait_state == 1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
struct io_uring_sqe *sqe = ringloop->get_sqe();
|
||||
if (!sqe)
|
||||
{
|
||||
wait_state = 0;
|
||||
return;
|
||||
}
|
||||
struct ring_data_t *data = ((ring_data_t*)sqe->user_data);
|
||||
my_uring_prep_poll_add(sqe, epoll_fd, POLLIN);
|
||||
data->callback = [&](ring_data_t *data)
|
||||
{
|
||||
if (data->res < 0)
|
||||
{
|
||||
throw std::runtime_error(std::string("epoll failed: ") + strerror(-data->res));
|
||||
}
|
||||
handle_epoll_events();
|
||||
wait_state = 0;
|
||||
};
|
||||
wait_state = 1;
|
||||
ringloop->submit();
|
||||
}
|
||||
|
||||
void osd_t::handle_epoll_events()
|
||||
{
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user