Skip to content
Snippets Groups Projects
Unverified Commit d7f9371a authored by Bensong Liu's avatar Bensong Liu
Browse files

more practicial framework now

parent b1ce0d7a
No related branches found
No related tags found
No related merge requests found
#ifndef UDP_FORWARDER_DYN_FORWARDER_HPP_
#define UDP_FORWARDER_DYN_FORWARDER_HPP_
#include <unordered_map>
#include <rlib/sys/sio.hpp>
#include "utils.hpp"
......@@ -10,14 +9,7 @@
using std::string;
struct ConnectionMapping {
std::unordered_map<string, fd_t> client2server;
std::unordered_multimap<fd_t, string> server2client;
static string clientInfoAsKey(string ip, uint16_t port) {
// Also works for ipv6. We just want to eliminate duplication, rather than make it easy to read.
return ip + '@' + port;
}
};
class Forwarder {
public:
......
......@@ -34,11 +34,36 @@ namespace Protocols {
epoll_event events[MAX_EVENTS];
char buffer[DGRAM_BUFFER_SIZE];
// WARN: If you want to modify this program to work for both TCP and UDP, PLEASE use rlib::sockIO::recv instead of fixed buffer.
auto onEvent = [&](auto activeFd) {
if (activeFd == ipcPipeInboundEnd) {
// Outbound gave me a message to forward! Send it.
auto targetClientId = rlib::sockIO::recv_msg(activeFd);
auto msg = rlib::sockIO::recv_msg(activeFd);
auto [clientAddr, clientPort] = ConnectionMapping::parseClientId(targetClientId);
}
else if (activeFd == listenFd) {
SockAddr clientAddr;
auto ret = recvfrom(activeFd, buffer, sizeof(buffer), 0, &clientAddr.addr, &clientAddr.len);
if (ret == -1) throw std::runtime_error("recvfrom failed. ");
}
};
rlog.info("PlainListener listening InboundPort [{}]:{} ...", listenAddr, listenPort);
while (true) {
// ...
// epoll
auto nfds = epoll_wait(epollFd, events, MAX_EVENTS, -1);
if (nfds == -1)
throw std::runtime_error("epoll_wait failed.");
for (auto cter = 0; cter < nfds; ++cter) {
onEvent(events[cter].data.fd);
}
}
}
......
......@@ -4,6 +4,7 @@
#include <rlib/sys/os.hpp>
#include <rlib/sys/sio.hpp>
#include <thread>
#include <unordered_map>
#include "common.hpp"
#if RLIB_OS_ID == OS_LINUX
......@@ -12,6 +13,32 @@
#include <wepoll.h>
#endif
struct SockAddr {
union {
sockaddr_storage addr_storage;
sockaddr addr;
sockaddr_in in4;
sockaddr_in6 in6;
};
int len;
};
struct ConnectionMapping {
std::unordered_map<string, fd_t> client2server;
std::unordered_multimap<fd_t, string> server2client;
static string makeClientId(const SockAddr &osStrust) const {
// ClientId is a binary string.
string result(sizeof(osStruct), '\0');
std::memcpy(result.data(), &osStruct, sizeof(osStrust));
return result;
}
static void parseClientId(const string &clientId, SockAddr &output) const {
static_assert(sizeof(output) == sizeof(SockAddr), "error: programming error detected.");
if (clientId.size() != sizeof(output))
throw std::invalid_argument("parseClientId, invalid input binary string length.");
std::memcpy(&output, clientId.data(), sizeof(output));
}
};
inline void epoll_add_fd(fd_t epollFd, fd_t fd) {
epoll_event event {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment