읽기/쓰기 socket 비동기 구현 완료
This commit is contained in:
@@ -5,8 +5,8 @@
|
||||
|
||||
#include <functional>
|
||||
#include <queue>
|
||||
#include <vector>
|
||||
#include <random>
|
||||
#include <vector>
|
||||
|
||||
#include "socket.h"
|
||||
#include "utils/thread_pool.h"
|
||||
@@ -31,13 +31,11 @@ class IOCP;
|
||||
|
||||
enum class IOCPEVENT { QUIT, READ, WRITE };
|
||||
|
||||
struct IOCPPASSINDATA { // 얘 double free 문제 있음..
|
||||
struct IOCPPASSINDATA {
|
||||
OVERLAPPED overlapped;
|
||||
IOCPEVENT event;
|
||||
std::shared_ptr<Socket> socket;
|
||||
SSL* ssl;
|
||||
BIO* rbio; // bio는 ssl별로 달라야 하므로 분리해야 함..
|
||||
BIO* wbio;
|
||||
std::shared_ptr<SSL> ssl;
|
||||
std::uint32_t transferredbytes;
|
||||
WSABUF wsabuf;
|
||||
std::uint32_t bufsize;
|
||||
@@ -50,8 +48,7 @@ struct IOCPPASSINDATA { // 얘 double free 문제 있음..
|
||||
std::memset(&overlapped, 0, sizeof(overlapped));
|
||||
event = IOCPEVENT::QUIT;
|
||||
socket = nullptr;
|
||||
rbio = nullptr;
|
||||
wbio = nullptr;
|
||||
ssl = nullptr;
|
||||
transferredbytes = 0;
|
||||
this->bufsize = bufsize;
|
||||
IOCPInstance = nullptr;
|
||||
@@ -63,10 +60,9 @@ struct IOCPPASSINDATA { // 얘 double free 문제 있음..
|
||||
std::memset(&overlapped, 0, sizeof(overlapped));
|
||||
event = IOCPEVENT::QUIT;
|
||||
socket = nullptr;
|
||||
ssl = ::SSL_new(ctx);
|
||||
rbio = ::BIO_new(::BIO_s_mem());
|
||||
wbio = ::BIO_new(::BIO_s_mem());
|
||||
::SSL_set_bio(ssl, rbio, wbio);
|
||||
ssl = std::make_shared<SSL>(::SSL_new(ctx), ::SSL_free);
|
||||
::SSL_set_bio(ssl.get(), ::BIO_new(::BIO_s_mem()),
|
||||
::BIO_new(::BIO_s_mem()));
|
||||
transferredbytes = 0;
|
||||
this->bufsize = bufsize;
|
||||
IOCPInstance = nullptr;
|
||||
@@ -80,8 +76,6 @@ struct IOCPPASSINDATA { // 얘 double free 문제 있음..
|
||||
std::memset(&overlapped, 0, sizeof(overlapped));
|
||||
event = other.event;
|
||||
socket = other.socket;
|
||||
rbio = other.rbio;
|
||||
wbio = other.wbio;
|
||||
transferredbytes = other.transferredbytes;
|
||||
bufsize = other.bufsize;
|
||||
IOCPInstance = other.IOCPInstance;
|
||||
@@ -104,8 +98,6 @@ struct IOCPPASSINDATA { // 얘 double free 문제 있음..
|
||||
std::memset(&overlapped, 0, sizeof(overlapped));
|
||||
event = other.event;
|
||||
socket = other.socket;
|
||||
rbio = other.rbio;
|
||||
wbio = other.wbio;
|
||||
transferredbytes = other.transferredbytes;
|
||||
bufsize = other.bufsize;
|
||||
IOCPInstance = other.IOCPInstance;
|
||||
@@ -190,9 +182,11 @@ class IOCP {
|
||||
auto queue_list = GetRecvQueue_(data->socket->sock);
|
||||
if (data->event == IOCPEVENT::READ) {
|
||||
if (proto_ == SessionProtocol::TLS || proto_ == SessionProtocol::QUIC) {
|
||||
::BIO_write(data->rbio, data->wsabuf.buf, cbTransfrred);
|
||||
::BIO_write(::SSL_get_rbio(data->ssl.get()), data->wsabuf.buf,
|
||||
cbTransfrred);
|
||||
|
||||
while ((red_data = ::SSL_read(data->ssl, buf.data(), buf.size())) > 0) {
|
||||
while ((red_data =
|
||||
::SSL_read(data->ssl.get(), buf.data(), buf.size())) > 0) {
|
||||
queue_list->emplace_back(std::make_pair(
|
||||
std::vector<char>(buf.begin(), buf.begin() + red_data), 0));
|
||||
}
|
||||
@@ -204,8 +198,13 @@ class IOCP {
|
||||
0));
|
||||
}
|
||||
DWORD recvbytes = 0, flags = 0;
|
||||
::WSARecv(data->socket->sock, &data->wsabuf, 1, &recvbytes, &flags,
|
||||
&data->overlapped, NULL);
|
||||
|
||||
IOCPPASSINDATA* recv_data = new IOCPPASSINDATA(data->bufsize);
|
||||
recv_data->socket = data->socket;
|
||||
|
||||
delete data;
|
||||
::WSARecv(recv_data->socket->sock, &recv_data->wsabuf, 1, &recvbytes,
|
||||
&flags, &recv_data->overlapped, NULL);
|
||||
} else { // WRITE 시, 무시한다.
|
||||
delete data;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user