CTEST(buffer, alloc_buffer) { struct buffer* buf; buf = alloc_buffer(0); ASSERT_EQUAL(16, buf->cap); ASSERT_EQUAL(0, buf->used); ASSERT_NOT_NULL(buf->data); dealloc_buffer(buf); buf = alloc_buffer(128); ASSERT_EQUAL(128, buf->cap); ASSERT_EQUAL(0, buf->used); ASSERT_NOT_NULL(buf->data); dealloc_buffer(buf); }
void handle_client(int cfd){ char name[1024]; bzero(name, 1024); read (cfd, &name, 1024); printf("%s\n", name); int fret = open(name, O_RDONLY); if (fret < 0){ //perror("file error"); write(cfd, "ER", 2); char* err = strerror(errno); write(cfd, err, strlen(err)); printf("%s", err); close(cfd); return; } printf("asdfasdfsaf\n"); write(cfd, "OK", 2); struct stat stat_src; fstat(fret, &stat_src); size_t num = htons(stat_src.st_size); printf("sending file size: %d\n", stat_src.st_size); write(cfd, &num, sizeof(num)); s_buffer* buf = alloc_buffer(256, fret, cfd); int bufret = 1; while(bufret > 0){ bufret = read_to_buf(buf, fret); bufret = read_from_buffer(buf, cfd); } dealloc_buffer(buf); close(cfd); }
CTEST(buffer, write_int8_buffer) { struct buffer* buf; int8_t i8 = 0xde; buf = alloc_buffer(16); write_int8_buffer(buf, i8); ASSERT_EQUAL(1, buf->used); ASSERT_EQUAL(i8, buf->data[0]); dealloc_buffer(buf); }
CTEST(buffer, write_int16_buffer) { struct buffer* buf; int16_t i16 = 0xdece; buf = alloc_buffer(16); write_int16_buffer(buf, i16); ASSERT_EQUAL(2, buf->used); ASSERT_EQUAL(0xde, (uint8_t)buf->data[0]); ASSERT_EQUAL(0xce, (uint8_t)buf->data[1]); dealloc_buffer(buf); }
CTEST(buffer, write_int32_buffer) { struct buffer* buf; int32_t i32 = 0xdecebeae; buf = alloc_buffer(16); write_int32_buffer(buf, i32); ASSERT_EQUAL(4, buf->used); ASSERT_EQUAL(0xde, (uint8_t)buf->data[0]); ASSERT_EQUAL(0xce, (uint8_t)buf->data[1]); ASSERT_EQUAL(0xbe, (uint8_t)buf->data[2]); ASSERT_EQUAL(0xae, (uint8_t)buf->data[3]); dealloc_buffer(buf); }
struct buffer *wait_response(int cfd) { int rbytes = 0, rc, r, remain, resp_size; struct buffer *response; rc = wait_socket_data(cfd, 3000, CR_READ); if (rc <= 0) { // timeout or error logger(DEBUG, "wait response error, as %s!", strerror(errno)); return NULL; } response = alloc_buffer(128); remain = get_buffer_cap(response); TIME_START(); while (rbytes < 4) { r = read(cfd, get_buffer_data(response) + rbytes, remain); if (r <= 0) { if (r == -1 && (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK)) continue; if (r == 0) close(cfd); logger(DEBUG, "wait response error, as %s!", strerror(errno)); goto err_cleanup; } rbytes += r; remain -= r; incr_buffer_used(response, r); } resp_size = read_int32_buffer(response) + 4; need_expand(response, resp_size); while (rbytes < resp_size) { r = read(cfd, get_buffer_data(response) + rbytes, resp_size - rbytes); if (r <= 0) { if (r == -1 && (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK)) continue; if (r == 0) close(cfd); logger(DEBUG, "wait response error, as %s!", strerror(errno)); goto err_cleanup; } rbytes += r; incr_buffer_used(response, r); } TIME_END(); logger(DEBUG, "Total time cost %lldus in wait response", TIME_COST()); return response; err_cleanup: dealloc_buffer(response); return NULL; }
CTEST(buffer, write_int64_buffer) { struct buffer* buf; int64_t i64 = 0xdecebeaedecebeae; buf = alloc_buffer(16); write_int64_buffer(buf, i64); ASSERT_EQUAL(8, buf->used); ASSERT_EQUAL(0xde, (uint8_t)buf->data[0]); ASSERT_EQUAL(0xce, (uint8_t)buf->data[1]); ASSERT_EQUAL(0xbe, (uint8_t)buf->data[2]); ASSERT_EQUAL(0xae, (uint8_t)buf->data[3]); ASSERT_EQUAL(0xde, (uint8_t)buf->data[4]); ASSERT_EQUAL(0xce, (uint8_t)buf->data[5]); ASSERT_EQUAL(0xbe, (uint8_t)buf->data[6]); ASSERT_EQUAL(0xae, (uint8_t)buf->data[7]); dealloc_buffer(buf); }
int thread_term() { for (int i = 0; i < wet->evno; ++i) { WEVENT_T e = wet->ev[i]; dealloc_buffer(&(e.buffer)); } event_base_free(wet->base); close_stream(&wet->stream); free(wet); void* retval = NULL; pthread_join(ret->id, &retval); event_base_free(ret->base); for (int i = 0; i < ret->evno; ++i) { close_stream(&ret->evs[i].stream); } free(ret); return 0; }