Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Commit

Permalink
fix: static variable bug
Browse files Browse the repository at this point in the history
  • Loading branch information
gaowanlu committed Feb 5, 2024
1 parent a24b362 commit 6fbc57f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 20 deletions.
22 changes: 12 additions & 10 deletions src/connection/stream_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ using tubekit::utility::singleton;
stream_connection::stream_connection(tubekit::socket::socket *socket_ptr) : connection(socket_ptr),
m_send_buffer(204800),
m_recv_buffer(204800),
m_wating_send_pack(204800)
m_wating_send_pack(204800),
should_send_idx(-1),
should_send_size(0)
{
}

Expand All @@ -20,11 +22,11 @@ stream_connection::~stream_connection()

bool stream_connection::sock2buf()
{
static char buffer[1024] = {0};
static char inner_buffer[1024] = {0};
while (true)
{
int oper_errno = 0;
int len = socket_ptr->recv(buffer, 1024, oper_errno);
int len = socket_ptr->recv(inner_buffer, 1024, oper_errno);
if (len == -1 && oper_errno == EAGAIN)
{
return true;
Expand All @@ -37,11 +39,11 @@ bool stream_connection::sock2buf()
{
try
{
m_recv_buffer.write(buffer, len);
m_recv_buffer.write(inner_buffer, len);
}
catch (const std::runtime_error &e)
{
// LOG_ERROR(e.what());
LOG_ERROR(e.what());
}
}
else
Expand All @@ -54,9 +56,7 @@ bool stream_connection::sock2buf()

bool stream_connection::buf2sock()
{
static char buffer[1024];
static int should_send_idx = -1;
static int should_send_size = 0;
static char inner_buffer[1024];
while (true)
{
// static char buffer no data
Expand All @@ -65,7 +65,7 @@ bool stream_connection::buf2sock()
int len = -1;
try
{
len = m_send_buffer.read(buffer, 1024);
len = m_send_buffer.read(inner_buffer, 1024);
}
catch (const std::runtime_error &e)
{
Expand Down Expand Up @@ -120,7 +120,7 @@ bool stream_connection::buf2sock()
}
}
int oper_errno = 0;
int len = socket_ptr->send(&buffer[should_send_idx], should_send_size, oper_errno);
int len = socket_ptr->send(&inner_buffer[should_send_idx], should_send_size, oper_errno);
if (0 > len)
{
if (oper_errno == EINTR)
Expand Down Expand Up @@ -204,4 +204,6 @@ void stream_connection::reuse()
m_send_buffer.clear();
m_recv_buffer.clear();
m_wating_send_pack.clear();
this->should_send_idx = -1;
this->should_send_size = 0;
}
4 changes: 4 additions & 0 deletions src/connection/stream_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ namespace tubekit
buffer::buffer m_send_buffer;
buffer::buffer m_recv_buffer;
buffer::buffer m_wating_send_pack;

private:
int should_send_idx{-1};
int should_send_size{0};
};
}
}
23 changes: 13 additions & 10 deletions src/connection/websocket_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ websocket_connection::websocket_connection(tubekit::socket::socket *socket_ptr)
buffer_start_use(0),
m_recv_buffer(204800),
m_send_buffer(204800),
m_wating_send_pack(204700)
m_wating_send_pack(204700),
should_send_idx(-1),
should_send_size(0)
{
http_parser_init(&m_http_parser, HTTP_REQUEST);
m_http_parser.data = this;
Expand Down Expand Up @@ -58,6 +60,8 @@ void websocket_connection::reuse()
http_parser_init(&m_http_parser, HTTP_REQUEST);
m_http_parser.data = this;
this->connected = false;
this->should_send_idx = -1;
this->should_send_size = 0;
}

http_parser *websocket_connection::get_parser()
Expand All @@ -78,11 +82,11 @@ void websocket_connection::add_header(const std::string &key, const std::string

bool websocket_connection::sock2buf()
{
static char buffer[1024] = {0};
static char inner_buffer[1024] = {0};
while (true)
{
int oper_errno = 0;
int len = socket_ptr->recv(buffer, 1024, oper_errno);
int len = socket_ptr->recv(inner_buffer, 1024, oper_errno);
if (len == -1 && oper_errno == EAGAIN)
{
return true;
Expand All @@ -95,11 +99,11 @@ bool websocket_connection::sock2buf()
{
try
{
m_recv_buffer.write(buffer, len);
m_recv_buffer.write(inner_buffer, len);
}
catch (const std::runtime_error &e)
{
// LOG_ERROR(e.what());
LOG_ERROR(e.what());
}
}
else
Expand All @@ -112,17 +116,16 @@ bool websocket_connection::sock2buf()

bool websocket_connection::buf2sock()
{
static char buffer[1024] = {0};
static int should_send_idx = -1;
static int should_send_size = 0;
static char inner_buffer[1024] = {0};

while (true)
{
if (should_send_idx < 0)
{
int len = -1;
try
{
len = m_send_buffer.read(buffer, 1024);
len = m_send_buffer.read(inner_buffer, 1024);
}
catch (const std::runtime_error &e)
{
Expand Down Expand Up @@ -178,7 +181,7 @@ bool websocket_connection::buf2sock()
}

int oper_errno = 0;
int len = socket_ptr->send(&buffer[should_send_idx], should_send_size, oper_errno);
int len = socket_ptr->send(&inner_buffer[should_send_idx], should_send_size, oper_errno);
if (0 > len)
{
if (oper_errno == EINTR)
Expand Down
4 changes: 4 additions & 0 deletions src/connection/websocket_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ namespace tubekit

std::function<void(websocket_connection &connection)> destory_callback;

private:
int should_send_idx{-1};
int should_send_size{0};

private:
http_parser m_http_parser;
bool connected{false};
Expand Down

0 comments on commit 6fbc57f

Please sign in to comment.