Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

204 linhas
6.8KB

  1. /*
  2. * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
  3. *
  4. * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
  5. *
  6. * Use of this source code is governed by MIT license that can be found in the
  7. * LICENSE file in the root of the source tree. All contributing project authors
  8. * may be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include "mk_mediakit.h"
  14. #define LOG_LEV 4
  15. //修改此宏,可以选择协议类型
  16. #define TCP_TYPE mk_type_ws
  17. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  18. typedef struct {
  19. mk_tcp_session _session;
  20. //下面你可以夹杂你的私货数据
  21. char your_some_useful_data[1024];
  22. } tcp_session_user_data;
  23. /**
  24. * 当tcp客户端连接服务器时触发
  25. * @param session 会话处理对象
  26. */
  27. void API_CALL on_mk_tcp_session_create(uint16_t server_port,mk_tcp_session session){
  28. char ip[64];
  29. log_printf(LOG_LEV,"%s %d",mk_tcp_session_peer_ip(session,ip),(int)mk_tcp_session_peer_port(session));
  30. tcp_session_user_data *user_data = malloc(sizeof(tcp_session_user_data));
  31. user_data->_session = session;
  32. mk_tcp_session_set_user_data(session,user_data);
  33. }
  34. /**
  35. * 收到tcp客户端发过来的数据
  36. * @param session 会话处理对象
  37. * @param data 数据指针
  38. * @param len 数据长度
  39. */
  40. void API_CALL on_mk_tcp_session_data(uint16_t server_port,mk_tcp_session session, mk_buffer buffer){
  41. char ip[64];
  42. log_printf(LOG_LEV,"from %s %d, data[%d]: %s",
  43. mk_tcp_session_peer_ip(session,ip),
  44. (int)mk_tcp_session_peer_port(session),
  45. mk_buffer_get_size(buffer),
  46. mk_buffer_get_data(buffer));
  47. mk_tcp_session_send(session,"echo:",0);
  48. mk_tcp_session_send_buffer(session, buffer);
  49. }
  50. /**
  51. * 每隔2秒的定时器,用于管理超时等任务
  52. * @param session 会话处理对象
  53. */
  54. void API_CALL on_mk_tcp_session_manager(uint16_t server_port,mk_tcp_session session){
  55. char ip[64];
  56. log_printf(LOG_LEV,"%s %d",mk_tcp_session_peer_ip(session,ip),(int)mk_tcp_session_peer_port(session));
  57. }
  58. /**
  59. * 一般由于客户端断开tcp触发
  60. * 本事件中可以调用mk_tcp_session_send_safe函数
  61. * @param session 会话处理对象
  62. * @param code 错误代码
  63. * @param msg 错误提示
  64. */
  65. void API_CALL on_mk_tcp_session_disconnect(uint16_t server_port,mk_tcp_session session,int code,const char *msg){
  66. char ip[64];
  67. log_printf(LOG_LEV,"%s %d: %d %s",mk_tcp_session_peer_ip(session,ip),(int)mk_tcp_session_peer_port(session),code,msg);
  68. tcp_session_user_data *user_data = (tcp_session_user_data *)mk_tcp_session_get_user_data(session);
  69. free(user_data);
  70. }
  71. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  72. typedef struct {
  73. mk_tcp_client client;
  74. //下面你可以夹杂你的私货数据
  75. char your_some_useful_data[1024];
  76. int count;
  77. } tcp_client_user_data;
  78. /**
  79. * tcp客户端连接服务器成功或失败回调
  80. * @param client tcp客户端
  81. * @param code 0为连接成功,否则为失败原因
  82. * @param msg 连接失败错误提示
  83. */
  84. void API_CALL on_mk_tcp_client_connect(mk_tcp_client client,int code,const char *msg){
  85. log_printf(LOG_LEV,"connect result:%d %s",code,msg);
  86. if(code == 0){
  87. //连接上后我们发送一个hello world测试数据
  88. mk_tcp_client_send(client,"hello world",0);
  89. }else{
  90. tcp_client_user_data *user_data = mk_tcp_client_get_user_data(client);
  91. mk_tcp_client_release(client);
  92. free(user_data);
  93. }
  94. }
  95. /**
  96. * tcp客户端与tcp服务器之间断开回调
  97. * 一般是eof事件导致
  98. * @param client tcp客户端
  99. * @param code 错误代码
  100. * @param msg 错误提示
  101. */
  102. void API_CALL on_mk_tcp_client_disconnect(mk_tcp_client client,int code,const char *msg){
  103. log_printf(LOG_LEV,"disconnect:%d %s",code,msg);
  104. //服务器主动断开了,我们销毁对象
  105. tcp_client_user_data *user_data = mk_tcp_client_get_user_data(client);
  106. mk_tcp_client_release(client);
  107. free(user_data);
  108. }
  109. /**
  110. * 收到tcp服务器发来的数据
  111. * @param client tcp客户端
  112. * @param data 数据指针
  113. * @param len 数据长度
  114. */
  115. void API_CALL on_mk_tcp_client_data(mk_tcp_client client, mk_buffer buffer){
  116. log_printf(LOG_LEV, "data[%d]:%s", mk_buffer_get_size(buffer), mk_buffer_get_data(buffer));
  117. }
  118. /**
  119. * 每隔2秒的定时器,用于管理超时等任务
  120. * @param client tcp客户端
  121. */
  122. void API_CALL on_mk_tcp_client_manager(mk_tcp_client client){
  123. tcp_client_user_data *user_data = mk_tcp_client_get_user_data(client);
  124. char buf[1024];
  125. sprintf(buf,"on_mk_tcp_client_manager:%d",user_data->count);
  126. mk_tcp_client_send(client,buf,0);
  127. if(++user_data->count == 5){
  128. //发送5遍后主动释放对象
  129. mk_tcp_client_release(client);
  130. free(user_data);
  131. }
  132. }
  133. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  134. void test_server(){
  135. mk_tcp_session_events events_server = {
  136. .on_mk_tcp_session_create = on_mk_tcp_session_create,
  137. .on_mk_tcp_session_data = on_mk_tcp_session_data,
  138. .on_mk_tcp_session_manager = on_mk_tcp_session_manager,
  139. .on_mk_tcp_session_disconnect = on_mk_tcp_session_disconnect
  140. };
  141. mk_tcp_server_events_listen(&events_server);
  142. mk_tcp_server_start(80, TCP_TYPE);
  143. }
  144. void test_client(){
  145. mk_tcp_client_events events_clent = {
  146. .on_mk_tcp_client_connect = on_mk_tcp_client_connect,
  147. .on_mk_tcp_client_data = on_mk_tcp_client_data,
  148. .on_mk_tcp_client_disconnect = on_mk_tcp_client_disconnect,
  149. .on_mk_tcp_client_manager = on_mk_tcp_client_manager,
  150. };
  151. mk_tcp_client client = mk_tcp_client_create(&events_clent, TCP_TYPE);
  152. tcp_client_user_data *user_data = (tcp_client_user_data *)malloc(sizeof(tcp_client_user_data));
  153. user_data->client = client;
  154. user_data->count = 0;
  155. mk_tcp_client_set_user_data(client,user_data);
  156. mk_tcp_client_connect(client, "121.40.165.18", 8800, 3);
  157. //你可以连接127.0.0.1 80测试
  158. // mk_tcp_client_connect(client, "127.0.0.1", 80, 3);
  159. }
  160. int main(int argc, char *argv[]) {
  161. char *ini_path = mk_util_get_exe_dir("c_api.ini");
  162. char *ssl_path = mk_util_get_exe_dir("ssl.p12");
  163. mk_config config = {
  164. .ini = ini_path,
  165. .ini_is_path = 1,
  166. .log_level = 0,
  167. .log_mask = LOG_CONSOLE,
  168. .ssl = ssl_path,
  169. .ssl_is_path = 1,
  170. .ssl_pwd = NULL,
  171. .thread_num = 0
  172. };
  173. mk_env_init(&config);
  174. free(ini_path);
  175. free(ssl_path);
  176. test_server();
  177. test_client();
  178. log_info("enter any key to exit");
  179. getchar();
  180. mk_stop_all_server();
  181. return 0;
  182. }