mirror of
https://github.com/ysoftdevs/cpp-examples.git
synced 2026-01-11 22:40:49 +01:00
Add libuv examples
This commit is contained in:
34
README.md
34
README.md
@@ -1,11 +1,14 @@
|
||||
fimuni-c-cpp-examples
|
||||
=====================
|
||||
FI MUNI - C and C++ examples
|
||||
============================
|
||||
|
||||
Examples related to talks at FI MUNI.
|
||||
Georgik's blog: http://georgik.sinusgear.com
|
||||
|
||||
Allegro
|
||||
-------
|
||||
|
||||
Allegro is library for interactive applications like games written in C.
|
||||
|
||||
Three examples for Allegro 5.0 library.
|
||||
- example-01 - display smiley
|
||||
- example-02 - display smiley when pressed space
|
||||
@@ -25,6 +28,9 @@ How to run:
|
||||
GTK+
|
||||
----
|
||||
|
||||
GTK+ is library for GUI applications written in C.
|
||||
It has also bindings for many other languages.
|
||||
|
||||
Helloworld example from http://en.wikipedia.org/wiki/Gtk.
|
||||
Requires: GTK+-3
|
||||
Package for Debian: libgtk-3-dev
|
||||
@@ -34,3 +40,27 @@ How to run:
|
||||
cd gtk
|
||||
make
|
||||
./helloworld
|
||||
|
||||
uv
|
||||
--
|
||||
|
||||
libuv is library which has support for event loops, networking and many other
|
||||
features required by cloud computing. This library is core part of Node.js.
|
||||
|
||||
Requires: libuv (part of Node.js)
|
||||
Package for Debian: none, you need to build it from sources. You can use Node.js package.
|
||||
|
||||
How to run hello example:
|
||||
|
||||
cd libuv/hello
|
||||
make
|
||||
./hello
|
||||
|
||||
How to run echo_server example:
|
||||
|
||||
cd libuv/echo_server
|
||||
make
|
||||
./echo_server
|
||||
|
||||
Note: Do not forget to set LD_LIBRARY_PATH so the linker will be able to locate libuv
|
||||
E.g.: export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
|
||||
|
||||
13
libuv/echo_server/Makefile
Normal file
13
libuv/echo_server/Makefile
Normal file
@@ -0,0 +1,13 @@
|
||||
PACKAGE = echo_server
|
||||
OBJECTS = echo_server.o
|
||||
CFLAGS = -I/usr/local/include/uv
|
||||
LDADD = -luv
|
||||
|
||||
hello: $(OBJECTS)
|
||||
${CC} -o ${PACKAGE} ${OBJECTS} ${LDADD}
|
||||
|
||||
.c.o:
|
||||
${CC} ${CFLAGS} -c $<
|
||||
|
||||
clean:
|
||||
rm -f hello *.o
|
||||
59
libuv/echo_server/echo_server.c
Normal file
59
libuv/echo_server/echo_server.c
Normal file
@@ -0,0 +1,59 @@
|
||||
#include <uv.h>
|
||||
#include <stdio.h>
|
||||
|
||||
uv_loop_t *loop;
|
||||
|
||||
uv_buf_t alloc_buffer(uv_handle_t *handle, size_t suggested_size) {
|
||||
return uv_buf_init((char*) malloc(suggested_size), suggested_size);
|
||||
}
|
||||
|
||||
static void close_cb(uv_handle_t* handle) {
|
||||
printf("Connection closed\n");
|
||||
}
|
||||
|
||||
void echo_read(uv_stream_t* tcp, ssize_t nread, uv_buf_t buf) {
|
||||
uv_err_t err = uv_last_error(uv_default_loop());
|
||||
|
||||
if (nread > 0) {
|
||||
printf("%.*s", nread, buf.base);
|
||||
} else if (nread < 0) {
|
||||
if (err.code == UV_EOF) {
|
||||
uv_close((uv_handle_t*)tcp, close_cb);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void on_new_connection(uv_stream_t *server, int status) {
|
||||
uv_tcp_t *client;
|
||||
if (status == -1) {
|
||||
// error!
|
||||
return;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
uv_tcp_t server;
|
||||
int r;
|
||||
struct sockaddr_in bind_addr;
|
||||
loop = uv_default_loop();
|
||||
uv_tcp_init(loop, &server);
|
||||
|
||||
bind_addr = uv_ip4_addr("0.0.0.0", 7000);
|
||||
uv_tcp_bind(&server, bind_addr);
|
||||
r = uv_listen((uv_stream_t*) &server, 128, on_new_connection);
|
||||
if (r) {
|
||||
fprintf(stderr, "Listen error %s\n", uv_err_name(uv_last_error(loop)));
|
||||
return 1;
|
||||
}
|
||||
return uv_run(loop);
|
||||
}
|
||||
13
libuv/hello/Makefile
Normal file
13
libuv/hello/Makefile
Normal file
@@ -0,0 +1,13 @@
|
||||
PACKAGE = hello
|
||||
OBJECTS = hello.o
|
||||
CFLAGS = -I/usr/local/include/uv
|
||||
LDADD = -luv
|
||||
|
||||
hello: $(OBJECTS)
|
||||
${CC} -o ${PACKAGE} ${OBJECTS} ${LDADD}
|
||||
|
||||
.c.o:
|
||||
${CC} ${CFLAGS} -c $<
|
||||
|
||||
clean:
|
||||
rm -f hello *.o
|
||||
11
libuv/hello/hello.c
Normal file
11
libuv/hello/hello.c
Normal file
@@ -0,0 +1,11 @@
|
||||
#include <stdio.h>
|
||||
#include <uv.h>
|
||||
|
||||
int main() {
|
||||
uv_loop_t *loop = uv_loop_new();
|
||||
|
||||
printf("Now quitting.\n");
|
||||
uv_run(loop);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user