随想录(cloud 网络库)
2023-09-27 14:27:11 时间
【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】
关于网络编程,我一直没有怎么学好。倒不是api不熟悉,主要是没有合适顺手的socket库,libevent什么也不熟悉。所以,乘着这一次学习的机会,自己写了一个网络库代码,基本上做到了异步触发的机制,每个用户只要做好自己的事件回调就可以了。目前代码已经放到github上,即https://github.com/feixiaoxing/socket。我把它称之为cloud,主要就是想让使用的同学尽可能忽略它的存在,用的简单但是特别的爽。
为了教大家怎么使用,我自己也写了一些demo。这里就列举一个简单的应用。
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "type.h"
#include "socket.h"
#include "handle.h"
void is_connected(SOCK_HANDLE* p_hand) {
return;
}
// on close
void is_close(SOCK_HANDLE* p_hand) {
return;
}
// on read
void is_read(SOCK_HANDLE* p_hand) {
s8* buf;
u32* len;
STATUS result;
result = get_rcv_buf(p_hand->sock, &buf, &len);
assert(TRUE == result);
send_buf(buf, len);
free(buf);
}
// entry of executive file
int main(int argc, char *argv[]) {
STATUS result;
int sock;
if(argc < 2) {
printf("No port parameter. \n");
exit(1);
}
// init cloud
cloud_init();
// register call back
register_callback_func(is_connected, is_close, is_read);
// new socket
result = create_server_socket(&sock, atoi(argv[1]));
assert(result == TRUE);
// run cloud
cloud_run(sock);
return 0;
}
ps:
github上面的代码已经删除,自从用了python的twisted和libevent,发现我这个库太弱了。
相关文章
- 使用 Amazon Cloud WAN 构建您的全球网络(内含免费套餐申请入口)
- spring cloud hystrix Health Indicator健康指数监控和Metrics Stream
- 16.SpringCloud实战项目-Spring Cloud Alibaba 组件简介
- 面试官:谈谈 Spring Cloud 与 Dubbo 有什么区别?
- Spring Cloud 2020.0.5 发布,新特性一览,别掉队了!
- Spring Cloud Greenwich 正式发布,Hystrix 即将寿终正寝。。
- spring cloud Alibaba nacos 整合Neo4j pom 配置
- Spring cloud:熔断器-客户端降级
- Adobe Analytics Cloud推动由语音驱动的客户体验
- 推荐9个大气美观的前后端分离项目:前端react,vue,ts,ElementUI,Angular等,后端mysql,springBoot,myBatis,springSecurity,cloud等
- 【springcloud 微服务】Spring Cloud 微服务网关Gateway使用详解
- 客快物流大数据项目(一百一十六):远程调用 Spring Cloud Feign
- 跟我学Spring Cloud(Finchley版)-05-服务注册与服务发现-Eureka入门