Optimise the read proc
This commit is contained in:
parent
4316fb2a73
commit
83b39b15f6
2 changed files with 10 additions and 10 deletions
12
server.odin
12
server.odin
|
|
@ -26,11 +26,6 @@ Server :: struct($Error_Type: typeid) {
|
|||
}
|
||||
|
||||
@(private)
|
||||
request_ended :: proc(buf: []byte) -> bool {
|
||||
HTTP_END_SEQUENCE :: []byte{'\r', '\n', '\r', '\n'}
|
||||
return bytes.index(buf, HTTP_END_SEQUENCE) != -1
|
||||
}
|
||||
|
||||
read_connection :: proc(
|
||||
client_socket: net.TCP_Socket,
|
||||
allocator := context.temp_allocator,
|
||||
|
|
@ -39,6 +34,8 @@ read_connection :: proc(
|
|||
err: http.RequestError,
|
||||
) {
|
||||
TCP_CHUNK_SIZE :: 1024
|
||||
HTTP_END_SEQUENCE :: []byte{'\r', '\n', '\r', '\n'}
|
||||
|
||||
buffer: bytes.Buffer
|
||||
bytes.buffer_init_allocator(&buffer, 0, 0, allocator)
|
||||
|
||||
|
|
@ -48,9 +45,12 @@ read_connection :: proc(
|
|||
if net_err != nil do return data, .NetworkError
|
||||
|
||||
if bytes_read == 0 do break
|
||||
|
||||
prev_len := len(buffer.buf)
|
||||
bytes.buffer_write(&buffer, chunk[:bytes_read])
|
||||
|
||||
if request_ended(buffer.buf[:]) do break
|
||||
search_start := max(0, prev_len - 3)
|
||||
if bytes.index(buffer.buf[search_start:], HTTP_END_SEQUENCE) != -1 do break
|
||||
}
|
||||
|
||||
return buffer.buf[:], nil
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue