纯C语言版本的socket.io服务器端实现

开发 后端 开发工具
哈,这又是一个socket.io服务端实现,本意是,拿C练练手,加强对C和linux系统的理解,写着写着,就写成了一个socket.io服务器端实现了。以为半成品,那就正式托管在github站点上吧,以便记录一下,可让大家批评与指正,加强内功的修炼等。

前言

哈,这又是一个socket.io服务端实现,本意是,拿C练练手,加强对C和linux系统的理解,写着写着,就写成了一个socket.io服务器端实现了。以为半成品,那就正式托管在github站点上吧,以便记录一下,可让大家批评与指正,加强内功的修炼等。

项目地址为:yongboy/c_socket.io_server

以下部分文字,偷懒,摘录自项目的README.md文件

说明

这是一个纯C语言版本的socket.io服务器端实现,目前仅支持linux系统,严重依赖libev and glib等基础库。

在运行socket.io_server之前,需要安装以下依赖:

  1. sudo apt-get install uuid-dev  
  2. sudo apt-get install libglib2.0-dev  

如何运行

  1. 编写实现代码(eg:chatroom.c),需要包含头文件 endpoint_impl.h
  2. 把实现代码(eg:chatroom.c)放入examples目录
  3. 编写对应的html文件,放入static目录
  4. 编辑Makefile文件
  5. 终端下运行make命令
  6. 然后敲入 ./socket.io_server 接口运行
  7. 打开浏览器即可访问 (eg:http://localhost:8000/chatroom.html)

API说明

对外的API,可以在头文件 endpoint_impl.h 看到其定义,其继承了另外一个公用的头文件 endpoint.h, 其完整定义为:

  1. #include <string.h>  
  2. #include <stdio.h>  
  3. #include <stdlib.h>  
  4.  
  5. typedef struct {  
  6.     char *message_type;  
  7.     char *message_id;  
  8.     char *endpoint;  
  9.     char *message_data;  
  10.     char *ori_data;  
  11. } message_fields;  
  12.  
  13. typedef struct {  
  14.     char *endpoint;  
  15.     void (*on_init)(const char *endpoint);  
  16.     void (*on_connect)(const char *sessionid);  
  17.     void (*on_message)(const char *sessionid, const message_fields *msg_fields);  
  18.     void (*on_json_message)(const char *sessionid, const message_fields *msg_fields);  
  19.     void (*on_event)(const char *sessionid, const message_fields *msg_fields);  
  20.     void (*on_other)(const char *sessionid, const message_fields *msg_fields);  
  21.     void (*on_disconnect)(const char *sessionid, const message_fields *msg_fields);  
  22.     void (*on_destroy)(const char *endpoint);  
  23. } endpoint_implement;  
  24.  
  25. extern void send_msg(char *sessionid, char *message);  
  26. extern void broadcast_clients(char *except_sessionid, char *message);  
  27.  
  28. static void on_init(const char *endpoint);  
  29. static void on_connect(const char *sessionid);  
  30. static void on_message(const char *sessionid, const message_fields *msg_fields) {  
  31.     printf("on_message recevie ori msg is %s\n", msg_fields->ori_data);  
  32. }  
  33. static void on_json_message(const char *sessionid, const message_fields *msg_fields) {  
  34.     printf("on_json_message recevie ori msg is %s\n", msg_fields->ori_data);  
  35. }  
  36. static void on_other(const char *sessionid, const message_fields *msg_fields) {  
  37.     printf("on_other recevie ori msg is %s\n", msg_fields->ori_data);  
  38. }  
  39. static void on_event(const char *sessionid, const message_fields *msg_fields);  
  40. static void on_disconnect(const char *sessionid, const message_fields *msg_fields);  
  41. static void on_destroy(const char *endpoint);  
  42.  
  43. static endpoint_implement *init_default_endpoint_implement(char *endpoint_name) {  
  44.     endpoint_implement *impl_point = (endpoint_implement *)malloc(sizeof(endpoint_implement));  
  45.     impl_point->endpoint = strdup(endpoint_name);  
  46.  
  47.     impl_point->on_init = on_init;  
  48.     impl_point->on_connect = on_connect;  
  49.     impl_point->on_message = on_message;  
  50.     impl_point->on_json_message = on_json_message;  
  51.     impl_point->on_event = on_event;  
  52.     impl_point->on_other = on_other;  
  53.     impl_point->on_disconnect = on_disconnect;  
  54.     impl_point->on_destroy = on_destroy;  
  55.  
  56.     return impl_point;  

完整定义.

在example目录中,你可以看到聊天室演示chatroom 和在线白板示范whiteboard .

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4.    
  5. #include <glib.h>  
  6.    
  7. #include "../endpoint_impl.h"  
  8.    
  9. typedef struct {  
  10. char *event_name;  
  11. char *event_args;  
  12. } event_message;  
  13.    
  14. static char *event_message_reg = "{\"name\":\"(.*?)\",\"args\":\\[([^\\]]*?)\\]}";  
  15.    
  16. static gchar *get_match_result(GMatchInfo *match_info, gint index) {  
  17. gchar *match = g_match_info_fetch(match_info, index);  
  18. gchar *result = g_strdup(match);  
  19. g_free(match);  
  20.    
  21. return result;  
  22. }  
  23.    
  24. static void *message_2_struct(gchar *post_string, event_message *event_msg) {  
  25. GError *error = NULL;  
  26. GRegex *regex;  
  27. GMatchInfo *match_info;  
  28.    
  29. regex = g_regex_new(event_message_reg, 0, 0, &error );  
  30. g_regex_match( regex, post_string, 0, &match_info );  
  31.    
  32. if (g_match_info_matches(match_info)) {  
  33. event_msg->event_name = get_match_result(match_info, 1);  
  34. event_msg->event_args = get_match_result(match_info, 2);  
  35. else {  
  36. event_msg = NULL;  
  37. }  
  38.    
  39. g_match_info_free( match_info );  
  40. g_regex_unref( regex );  
  41.    
  42. return event_msg;  
  43. }  
  44.    
  45. static GHashTable *hashtable;  
  46.    
  47. static void hashtable_init(void) {  
  48. hashtable = g_hash_table_new(g_str_hash, g_str_equal);  
  49. }  
  50.    
  51. static void hashtable_add(const char *key, void *value) {  
  52. if (key) {  
  53. g_hash_table_insert(hashtable, g_strdup(key), value);  
  54. }  
  55. }  
  56.    
  57. static gboolean hashtable_remove(const char *key) {  
  58. if (key)  
  59. return g_hash_table_remove(hashtable, key);  
  60.    
  61. return 0;  
  62. }  
  63.    
  64. static void *hashtable_lookup(const char *key) {  
  65. if (key == NULL)  
  66. return NULL;  
  67. return g_hash_table_lookup(hashtable, key);  
  68. }  
  69.    
  70. /*static gint hashtable_size(void) {  
  71. return g_hash_table_size(hashtable);  
  72. }*/ 
  73.    
  74. static void hashtable_destroy(void) {  
  75. g_hash_table_destroy(hashtable);  
  76. }  
  77.    
  78. /**  
  79. ** use the struct to warpper the demo implment  
  80. **/ 
  81. static char *endpoint_name;  
  82. static void on_init(const char *endpoint) {  
  83. hashtable_init();  
  84. printf("%s has been inited now\n", endpoint);  
  85. endpoint_name = g_strdup(endpoint);  
  86. }  
  87.    
  88. static void on_connect(const char *sessionid) {  
  89. char messages[strlen(sessionid) + 50];  
  90. sprintf(messages, "5::%s:{\"name\":\"clientId\",\"args\":[{\"id\":\"%s\"}]}", endpoint_name, sessionid);  
  91.    
  92. send_msg(sessionid, messages);  
  93. }  
  94.    
  95. static void send_it(char *session_id, char *messaage) {  
  96. send_msg(session_id, messaage);  
  97. }  
  98.    
  99. static void free_event_msg(event_message *event_msg) {  
  100. free(event_msg->event_name);  
  101. free(event_msg->event_args);  
  102. }  
  103.    
  104. static void on_event(const char *sessionid, const message_fields *msg_fields) {  
  105. event_message event_msg;  
  106. if (!message_2_struct(msg_fields->message_data, &event_msg)) {  
  107. fprintf(stderr, "%s Parse Message Error !\n", msg_fields->ori_data);  
  108. return;  
  109. }  
  110.    
  111. if (!strcmp(event_msg.event_name, "roomNotice")) {  
  112. /*5::/whiteboard:{"name":"roomNotice","args":[{"room":"myRoom"}]}*/ 
  113. char target_room_id[strlen(event_msg.event_args) - 10];// = event_msg.event_args + 9;  
  114. strncpy(target_room_id, event_msg.event_args + 9, strlen(event_msg.event_args) - 11);  
  115.    
  116. GPtrArray *list = (GPtrArray *)hashtable_lookup(target_room_id);  
  117. if (list == NULL) {  
  118. list = g_ptr_array_new();  
  119. hashtable_add(target_room_id, list);  
  120. }  
  121. g_ptr_array_add(list, g_strdup(sessionid));  
  122.    
  123. int room_count = list->len;  
  124. char messages[strlen(sessionid) + 200];  
  125. sprintf(messages, "5::%s:{\"name\":\"roomCount\",\"args\":[{\"room\":\"%s\",\"num\":%d}]}", endpoint_name, target_room_id, room_count);  
  126.    
  127. hashtable_add(g_strdup(sessionid), g_strdup(target_room_id));  
  128. g_ptr_array_foreach(list, (GFunc)send_it, messages);  
  129. free_event_msg(&event_msg);  
  130. return;  
  131. }  
  132.    
  133. char messages[strlen(msg_fields->ori_data) + 200];  
  134. sprintf(messages, "5::%s:{\"name\":\"%s\",\"args\":[%s]}", endpoint_name, event_msg.event_name, event_msg.event_args);  
  135. free_event_msg(&event_msg);  
  136. char *target_room_id = (char *)hashtable_lookup(sessionid);  
  137. GPtrArray *list = (GPtrArray *)hashtable_lookup(target_room_id);  
  138. if(list == NULL){  
  139. return;  
  140. }  
  141.    
  142. int i;  
  143. for (i = 0; i < list->len; i++) {  
  144. char *session_id = g_ptr_array_index(list, i);  
  145. if (strcmp(session_id, sessionid) == 0)  
  146. continue;  
  147.    
  148. send_msg(session_id, messages);  
  149. }  
  150.    
  151. }  
  152.    
  153. static void on_disconnect(const char *sessionid, const message_fields *msg_fields) {  
  154. char *room_id = (char *)hashtable_lookup(sessionid);  
  155. if (room_id == NULL) {  
  156. fprintf(stderr, "the room_id is NULL\n");  
  157. return;  
  158. }  
  159.    
  160. char notice_msg[strlen(endpoint_name) + strlen(room_id) + 70];  
  161. GPtrArray *list = (GPtrArray *)hashtable_lookup(room_id);  
  162. sprintf(notice_msg, "5::%s:{\"name\":\"roomCount\",\"args\":[{\"room\":\"%s\",\"num\":%d}]}", endpoint_name, room_id, list->len - 1);  
  163. int i, remove_index;  
  164. for (i = 0; i < list->len; i++) {  
  165. char *session_id = g_ptr_array_index(list, i);  
  166. if (strcmp(session_id, sessionid) == 0) {  
  167. remove_index = i;  
  168. continue;  
  169. }  
  170.    
  171. send_msg(session_id, notice_msg);  
  172. }  
  173. g_ptr_array_remove_index(list, remove_index);  
  174.    
  175. hashtable_remove(sessionid);  
  176. free(room_id);  
  177. }  
  178.    
  179. static void on_destroy(const char *endpoint) {  
  180. printf("%s has been destroy now\n", endpoint);  
  181. hashtable_destroy();  
  182. free(endpoint_name);  
  183. }  
  184.    
  185. extern endpoint_implement *init_whiteboard_endpoint_implement(char *endpoint_name) {  
  186. return init_default_endpoint_implement(endpoint_name);  

因为C语言中没有散列表,只好借助于成熟的glib库实现。

其它

项目不太成熟,期待大家的参与,您的建议、批评和指正,都是一种激励,再次表示感谢。

原文链接:http://www.blogjava.net/yongboy/archive/2013/03/15/396493.html

责任编辑:林师授 来源: 聂永的博客
相关推荐

2017-09-05 15:30:00

JavascriptSocket.ioNode.js

2012-08-01 14:16:27

IBMdW

2009-08-21 17:39:20

服务器端程序C#网络编程

2018-03-15 09:54:45

php服务器端语言

2010-03-23 10:04:00

JavaScript

2009-08-21 14:34:34

C#服务器端表达式

2013-12-25 11:01:16

JavaScript

2012-10-15 13:40:15

IBMdw

2014-01-15 10:06:30

vFlash

2014-07-22 10:29:04

背包算法coffee

2022-06-13 07:33:57

socketReact组件

2010-06-02 19:40:55

搭建SVN版本库服务器

2015-10-27 09:40:31

TCPIP网络协议

2010-08-27 10:23:26

DHCP服务器

2014-11-14 11:03:56

微软.NET

2023-06-30 08:00:00

漏洞网络安全SSTI

2011-06-07 16:01:46

Android 服务器 数据交互

2017-12-06 22:29:53

2015-11-04 14:14:56

HTTP网络协议

2010-06-02 19:26:41

搭建SVN版本库服务器
点赞
收藏

51CTO技术栈公众号