some refactors.
This commit is contained in:
@@ -1,45 +1,15 @@
|
||||
#include <uv.h>
|
||||
|
||||
namespace clice {
|
||||
|
||||
/// core class responsible for starting the server
|
||||
class Server {
|
||||
private:
|
||||
uv_loop_t *loop;
|
||||
uv_pipe_t stdin_pipe;
|
||||
|
||||
private:
|
||||
static void on_read(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) {
|
||||
if (nread > 0) {
|
||||
printf("Read: %s\n", buf->base);
|
||||
} else if (nread < 0) {
|
||||
if (nread != UV_EOF)
|
||||
fprintf(stderr, "Read error: %s\n", uv_err_name(nread));
|
||||
if (!uv_is_closing((uv_handle_t *)&pipe)) {
|
||||
uv_close((uv_handle_t *)&pipe, NULL);
|
||||
}
|
||||
}
|
||||
if (buf->base) {
|
||||
free(buf->base);
|
||||
}
|
||||
}
|
||||
|
||||
static void alloc_buffer(uv_handle_t *handle, size_t suggested_size,
|
||||
uv_buf_t *buf) {
|
||||
buf->base = (char *)malloc(suggested_size);
|
||||
buf->len = suggested_size;
|
||||
}
|
||||
static uv_loop_t* loop;
|
||||
static uv_pipe_t stdin_pipe;
|
||||
|
||||
public:
|
||||
Server() {
|
||||
loop = uv_default_loop();
|
||||
uv_pipe_init(loop, &stdin_pipe, 0);
|
||||
uv_pipe_open(&stdin_pipe, 0);
|
||||
uv_read_start((uv_stream_t *)&stdin_pipe, alloc_buffer, on_read);
|
||||
}
|
||||
|
||||
~Server() { uv_loop_close(loop); }
|
||||
|
||||
int run() { return uv_run(loop, UV_RUN_DEFAULT); }
|
||||
static int Initialize();
|
||||
static int Exit();
|
||||
};
|
||||
|
||||
} // namespace clice
|
||||
} // namespace clice
|
||||
|
||||
Reference in New Issue
Block a user