72 lines
1.9 KiB
C++
72 lines
1.9 KiB
C++
#include "session/session.h"
|
|
|
|
#include "socket/tcp_socket.h"
|
|
#include "socket/udp_socket.h"
|
|
#include "utils/thread_pool.h"
|
|
|
|
namespace Network {
|
|
|
|
Session::Session(gsl::not_null<Network::IOCP*> iocp, utils::ThreadPool* tp,
|
|
SessionType type, SessionProtocol proto, Network::Address addr)
|
|
: iocp_(iocp), tp_(tp), proto_(proto) {
|
|
switch (proto) {
|
|
case SessionProtocol::UDP:
|
|
case SessionProtocol::QUIC: {
|
|
Network::UDPSocket* sock = new Network::UDPSocket();
|
|
sock_ = sock;
|
|
sock->init(addr.family);
|
|
switch (type) {
|
|
case SessionType::CONNECT:
|
|
ssl_ctx_ = ::SSL_CTX_new(::OSSL_QUIC_client_method());
|
|
if (ssl_ctx_ == nullptr) {
|
|
spdlog::critical("SSL_CTX_new()");
|
|
std::exit(EXIT_FAILURE);
|
|
}
|
|
::SSL_CTX_set_verify(ssl_ctx_, SSL_VERIFY_PEER, NULL);
|
|
|
|
if (!::SSL_CTX_set_default_verify_paths(ssl_ctx_)) {
|
|
spdlog::critical("SSL_CTX_set_default_verify_paths()");
|
|
std::exit(EXIT_FAILURE);
|
|
}
|
|
break;
|
|
case SessionType::LISTEN:
|
|
sock->bind(addr);
|
|
break;
|
|
default:
|
|
std::exit(EXIT_FAILURE);
|
|
}
|
|
} break;
|
|
case SessionProtocol::TCP:
|
|
case SessionProtocol::TLS: {
|
|
Network::TCPSocket* sock = new Network::TCPSocket();
|
|
sock_ = sock;
|
|
sock->init(addr.family);
|
|
switch (type) {
|
|
case SessionType::CONNECT:
|
|
sock->connect(addr);
|
|
break;
|
|
case SessionType::LISTEN:
|
|
sock->bind(addr);
|
|
sock->listen(SOMAXCONN);
|
|
break;
|
|
default:
|
|
std::exit(EXIT_FAILURE);
|
|
}
|
|
} break;
|
|
default:
|
|
std::exit(EXIT_FAILURE);
|
|
}
|
|
|
|
ssl_ = ::SSL_new(ssl_ctx_);
|
|
if (ssl_ == nullptr) {
|
|
spdlog::critical("SSL_new()");
|
|
std::exit(EXIT_FAILURE);
|
|
}
|
|
}
|
|
|
|
Session::~Session() {
|
|
if (sock_ != nullptr) delete sock_;
|
|
}
|
|
|
|
} // namespace Network
|