#include <gtk/gtk.h>
#include <stdio.h>
#include <stdint.h>
#include "uv.h"
uv_loop_t *loop;
#define DEFAULT_PORT 7000
#define DEFAULT_BACKLOG 128
typedef struct{
uv_write_t req;
uv_buf_t buf;
}write_req_t;
//负责为新来的消息申请空间
void alloc_buffer(uv_handle_t* handle,size_t suggested_size,uv_buf_t* buf)
{
buf->len = suggested_size;
buf->base = (char *)malloc(suggested_size);
}
void on_close(uv_handle_t* handle)
{
if(handle != NULL)
free(handle);
}
void free_write_req(uv_write_t *req)
{
write_req_t *wr = (write_req_t*)req;
free(wr->buf.base);
free(wr);
}
void echo_write(uv_write_t* req, int status)
{
if(status)
{
fprintf(stderr, "Write error %s\n", uv_strerror(status));
}
free_write_req(req);
}
//负责处理新来的消
void echo_read(uv_stream_t* client,ssize_t nread,const uv_buf_t* buf)
{
if(nread > 0)
{
buf->base[nread] = 0;
fprintf(stdout,"recv:%s",buf->base);
write_req_t *req = (write_req_t *)malloc(sizeof(write_req_t));
req->buf = uv_buf_init(buf->base,nread);
uv_write((uv_write_t *)req,client,&req->buf,1,echo_write);
return;
}
else if(nread < 0)
{
if(nread != UV_EOF)
{
fprintf(stderr,"Read error %s\n",uv_err_name(nread));
}
else
{
fprintf(stderr,"client disconnect\n");
}
uv_close((uv_handle_t*)client,on_close);
}
if(buf->base != NULL)
{
free(buf->base);
}
}
void on_new_connection(uv_stream_t* server,int status)
{
if(status < 0)
{
fprintf(stderr,"New connection error %s\n",uv_strerror(status));
return;
}
uv_tcp_t *client = (uv_tcp_t *)malloc(sizeof(uv_tcp_t));
uv_tcp_init(loop,client);
if(uv_accept(server,(uv_stream_t*)client) == 0)
{
uv_read_start((uv_stream_t*)client,alloc_buffer,echo_read);
}
else
{
uv_close((uv_handle_t*) client, NULL);
}
}
static void
print_hello (GtkWidget *widget,
gpointer data)
{
g_print ("TCP server Test \n");
}
static void tcp_server_init(GtkWidget *widget,
gpointer data)
{
printf("libuv version: %s\n", uv_version_string());
loop = uv_default_loop();
uv_tcp_t server;
uv_tcp_init(loop,&server);
struct sockaddr_in addr;
uv_ip4_addr("0.0.0.0",DEFAULT_PORT,&addr);
uv_tcp_bind(&server,(const struct sockaddr*)&addr,0);
int r = uv_listen((uv_stream_t*)&server,DEFAULT_BACKLOG,on_new_connection);
if(r)
{
fprintf(stderr, "Listen error %s\n", uv_strerror(r));
return 1;
}
return uv_run(loop,UV_RUN_DEFAULT);
}
static void
activate (GtkApplication *app,
gpointer user_data)
{
GtkWidget *window;
GtkWidget *button;
GtkWidget *button_box;
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Window");
gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
button_box = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
gtk_container_add (GTK_CONTAINER (window), button_box);
button = gtk_button_new_with_label ("libuv Demo");
g_signal_connect (button, "clicked", G_CALLBACK (tcp_server_init), NULL);
g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy), window);
gtk_container_add (GTK_CONTAINER (button_box), button);
gtk_widget_show_all (window);
}
int
main (int argc,
char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}