在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:embedded2016/server-framework开源软件地址:https://github.com/embedded2016/server-framework开源编程语言:C 99.4%开源软件介绍:Server Framework in Modern CIt is simple to use this framework to build your own service. It is based on Protocol structure and callbacks, so that we can dynamically change protocols and support stuff such as HTTP requests. The framework consists of the following components:
Here is a simple example: #include "protocol-server.h"
#include <stdio.h>
#include <string.h>
void on_open(server_pt server, int sockfd) {
printf("A connection was accepted on socket #%d.\n", sockfd);
}
void on_close(server_pt server, int sockfd) {
printf("Socket #%d is now disconnected.\n", sockfd);
}
// simple echo implementation: the main callback
void on_data(server_pt server, int sockfd) {
// We will assign a reading buffer on the stack
char buff[1024];
ssize_t incoming = 0;
// Read everything, this is edge triggered, `on_data` won't be called
//again until all the data was read.
while ((incoming = Server.read(server, sockfd, buff, 1024)) > 0) {
// since the data is stack allocated, we'll write a copy
// otherwise, we would avoid a copy using Server.write_move
Server.write(server, sockfd, buff, incoming);
if (!memcmp(buff, "bye", 3)) {
// close the connection automatically AFTER the buffer was sent.
Server.close(server, sockfd);
}
}
}
int main(void) {
/* create the echo protocol object */
struct Protocol protocol = { .on_open = on_open,
.on_close = on_close,
.on_data = on_data,
.service = "echo"};
// use the macro start_server, which will call Server.listen(settings)
// with the settings we provide.
start_server(.protocol = &protocol, .timeout = 10, .threads = 8);
} DocumentationThe code is heavily commented, and you can generate API documentation via
Doxygen. Check the directory Licensing
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论