Migrate connection_t from storing a huge buffer to a std::vector

This commit is contained in:
ridiculousfish 2013-02-16 01:28:46 -08:00
parent b682c4102e
commit dc54ec5b2b
3 changed files with 16 additions and 18 deletions

View file

@ -259,7 +259,8 @@ static void reconnect()
debug(3, L"Get new fishd connection");
s_env_univeral_inited = false;
env_universal_server.buffer_consumed = env_universal_server.buffer_used = 0;
env_universal_server.buffer_consumed = 0;
env_universal_server.read_buffer.clear();
env_universal_server.fd = get_socket();
s_env_univeral_inited = true;
if (env_universal_server.fd >= 0)

View file

@ -422,10 +422,11 @@ void env_universal_common_init(void (*cb)(fish_message_type_t type, const wchar_
static int read_byte(connection_t *src)
{
if (src->buffer_consumed >= src->buffer_used)
if (src->buffer_consumed >= src->read_buffer.size())
{
char local[ENV_UNIVERSAL_BUFFER_SIZE];
ssize_t res = read(src->fd, src->buffer, ENV_UNIVERSAL_BUFFER_SIZE);
ssize_t res = read(src->fd, local, sizeof local);
// debug(4, L"Read chunk '%.*s'", res, src->buffer );
@ -441,17 +442,19 @@ static int read_byte(connection_t *src)
return ENV_UNIVERSAL_ERROR;
}
if (res == 0)
else if (res == 0)
{
return ENV_UNIVERSAL_EOF;
}
src->buffer_consumed = 0;
src->buffer_used = res;
else
{
src->read_buffer.clear();
src->read_buffer.insert(src->read_buffer.begin(), local, local + res);
src->buffer_consumed = 0;
}
}
return src->buffer[src->buffer_consumed++];
return src->read_buffer.at(src->buffer_consumed++);
}
@ -934,8 +937,7 @@ void enqueue_all(connection_t *c)
connection_t::connection_t(int input_fd) :
fd(input_fd),
killme(false),
buffer_consumed(0),
buffer_used(0)
buffer_consumed(0)
{
}

View file

@ -107,18 +107,13 @@ class connection_t
/**
The read buffer.
*/
char buffer[ENV_UNIVERSAL_BUFFER_SIZE];
std::vector<char> read_buffer;
/**
Number of bytes that have already been consumed.
*/
size_t buffer_consumed;
/**
Number of bytes that have been read into the buffer.
*/
size_t buffer_used;
/* Constructor */
connection_t(int input_fd);
};