ZLMediaKit/api/include/mk_tcp.h

269 lines
10 KiB
C++
Raw Normal View History

2019-12-26 21:22:19 +08:00
/*
2023-12-09 16:23:51 +08:00
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
2019-12-26 21:22:19 +08:00
*
2023-12-09 16:23:51 +08:00
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
2019-12-26 21:22:19 +08:00
*
2023-12-09 16:23:51 +08:00
* Use of this source code is governed by MIT-like license that can be found in the
2020-04-04 20:30:09 +08:00
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
2019-12-26 21:22:19 +08:00
*/
#ifndef MK_TCP_H
#define MK_TCP_H
#include "mk_common.h"
2019-12-26 21:22:19 +08:00
#ifdef __cplusplus
extern "C" {
#endif
2022-05-25 15:38:32 +08:00
///////////////////////////////////////////Buffer::Ptr/////////////////////////////////////////////
typedef struct mk_buffer_t *mk_buffer;
2022-05-25 15:38:32 +08:00
typedef void(API_CALL *on_mk_buffer_free)(void *user_data, void *data);
/**
* buffer对象
* @param data
* @param len
* @param cb free回调函数
* @param user_data free回调函数on_mk_buffer_free第一个参数
* @return buffer对象
*/
API_EXPORT mk_buffer API_CALL mk_buffer_from_char(const char *data, size_t len, on_mk_buffer_free cb, void *user_data);
API_EXPORT mk_buffer API_CALL mk_buffer_from_char2(const char *data, size_t len, on_mk_buffer_free cb, void *user_data, on_user_data_free user_data_free);
2022-05-25 15:38:32 +08:00
API_EXPORT mk_buffer API_CALL mk_buffer_ref(mk_buffer buffer);
API_EXPORT void API_CALL mk_buffer_unref(mk_buffer buffer);
API_EXPORT const char* API_CALL mk_buffer_get_data(mk_buffer buffer);
API_EXPORT size_t API_CALL mk_buffer_get_size(mk_buffer buffer);
///////////////////////////////////////////SockInfo/////////////////////////////////////////////
//SockInfo对象的C映射
typedef struct mk_sock_info_t *mk_sock_info;
//SockInfo::get_peer_ip()
2020-04-24 14:06:28 +08:00
API_EXPORT const char* API_CALL mk_sock_info_peer_ip(const mk_sock_info ctx, char *buf);
//SockInfo::get_local_ip()
2020-04-24 14:06:28 +08:00
API_EXPORT const char* API_CALL mk_sock_info_local_ip(const mk_sock_info ctx, char *buf);
//SockInfo::get_peer_port()
API_EXPORT uint16_t API_CALL mk_sock_info_peer_port(const mk_sock_info ctx);
//SockInfo::get_local_port()
API_EXPORT uint16_t API_CALL mk_sock_info_local_port(const mk_sock_info ctx);
#ifndef SOCK_INFO_API_RENAME
#define SOCK_INFO_API_RENAME
//mk_tcp_session对象转换成mk_sock_info对象后再获取网络相关信息
2020-04-24 14:06:28 +08:00
#define mk_tcp_session_peer_ip(x,buf) mk_sock_info_peer_ip(mk_tcp_session_get_sock_info(x),buf)
#define mk_tcp_session_local_ip(x,buf) mk_sock_info_local_ip(mk_tcp_session_get_sock_info(x),buf)
#define mk_tcp_session_peer_port(x) mk_sock_info_peer_port(mk_tcp_session_get_sock_info(x))
#define mk_tcp_session_local_port(x) mk_sock_info_local_port(mk_tcp_session_get_sock_info(x))
//mk_tcp_client对象转换成mk_sock_info对象后再获取网络相关信息
2020-04-24 14:06:28 +08:00
#define mk_tcp_client_peer_ip(x,buf) mk_sock_info_peer_ip(mk_tcp_client_get_sock_info(x),buf)
#define mk_tcp_client_local_ip(x,buf) mk_sock_info_local_ip(mk_tcp_client_get_sock_info(x),buf)
#define mk_tcp_client_peer_port(x) mk_sock_info_peer_port(mk_tcp_client_get_sock_info(x))
#define mk_tcp_client_local_port(x) mk_sock_info_local_port(mk_tcp_client_get_sock_info(x))
#endif
2019-12-26 21:22:19 +08:00
///////////////////////////////////////////TcpSession/////////////////////////////////////////////
//TcpSession对象的C映射
typedef struct mk_tcp_session_t *mk_tcp_session;
typedef struct mk_tcp_session_ref_t *mk_tcp_session_ref;
2022-05-25 15:38:32 +08:00
//获取基类指针以便获取其网络相关信息
API_EXPORT mk_sock_info API_CALL mk_tcp_session_get_sock_info(const mk_tcp_session ctx);
2019-12-26 21:22:19 +08:00
//TcpSession::safeShutdown()
API_EXPORT void API_CALL mk_tcp_session_shutdown(const mk_tcp_session ctx,int err,const char *err_msg);
//TcpSession::send()
2022-05-25 15:38:32 +08:00
API_EXPORT void API_CALL mk_tcp_session_send(const mk_tcp_session ctx, const char *data, size_t len);
API_EXPORT void API_CALL mk_tcp_session_send_buffer(const mk_tcp_session ctx, mk_buffer buffer);
2019-12-26 21:22:19 +08:00
//切换到该对象所在线程后再TcpSession::send()
2022-05-25 15:38:32 +08:00
API_EXPORT void API_CALL mk_tcp_session_send_safe(const mk_tcp_session ctx, const char *data, size_t len);
API_EXPORT void API_CALL mk_tcp_session_send_buffer_safe(const mk_tcp_session ctx, mk_buffer buffer);
//创建mk_tcp_session的强引用
2022-05-25 15:38:32 +08:00
API_EXPORT mk_tcp_session_ref API_CALL mk_tcp_session_ref_from(const mk_tcp_session ctx);
//删除mk_tcp_session的强引用
2022-05-25 15:38:32 +08:00
API_EXPORT void mk_tcp_session_ref_release(const mk_tcp_session_ref ref);
//根据强引用获取mk_tcp_session
2022-05-25 15:38:32 +08:00
API_EXPORT mk_tcp_session mk_tcp_session_from_ref(const mk_tcp_session_ref ref);
2019-12-26 21:22:19 +08:00
///////////////////////////////////////////自定义tcp服务/////////////////////////////////////////////
typedef struct {
/**
* mk_tcp_session创建对象
* @param server_port
* @param session
*/
void (API_CALL *on_mk_tcp_session_create)(uint16_t server_port,mk_tcp_session session);
/**
*
* @param server_port
* @param session
2022-05-25 15:38:32 +08:00
* @param buffer
2019-12-26 21:22:19 +08:00
*/
2022-05-25 15:38:32 +08:00
void (API_CALL *on_mk_tcp_session_data)(uint16_t server_port,mk_tcp_session session, mk_buffer buffer);
2019-12-26 21:22:19 +08:00
/**
* 2
* @param server_port
* @param session
*/
void (API_CALL *on_mk_tcp_session_manager)(uint16_t server_port,mk_tcp_session session);
/**
* tcp触发
* @param server_port
* @param session
* @param code
* @param msg
*/
void (API_CALL *on_mk_tcp_session_disconnect)(uint16_t server_port,mk_tcp_session session,int code,const char *msg);
} mk_tcp_session_events;
typedef enum {
//普通的tcp
mk_type_tcp = 0,
//ssl类型的tcp
mk_type_ssl = 1,
//基于websocket的连接
mk_type_ws = 2,
//基于ssl websocket的连接
mk_type_wss = 3
}mk_tcp_type;
/**
* tcp会话对象附着用户数据
* mk_tcp_server_server_start启动的服务类型有效
* @param session
* @param user_data
*/
API_EXPORT void API_CALL mk_tcp_session_set_user_data(mk_tcp_session session, void *user_data);
API_EXPORT void API_CALL mk_tcp_session_set_user_data2(mk_tcp_session session, void *user_data, on_user_data_free user_data_free);
2019-12-26 21:22:19 +08:00
/**
* tcp会话对象上附着的用户数据
* mk_tcp_server_server_start启动的服务类型有效
* @param session tcp会话对象
* @return
*/
API_EXPORT void* API_CALL mk_tcp_session_get_user_data(mk_tcp_session session);
/**
* tcp服务器
* @param port 0
* @param type
*/
2020-03-06 11:11:27 +08:00
API_EXPORT uint16_t API_CALL mk_tcp_server_start(uint16_t port, mk_tcp_type type);
2019-12-26 21:22:19 +08:00
/**
* tcp服务器事件
*/
API_EXPORT void API_CALL mk_tcp_server_events_listen(const mk_tcp_session_events *events);
///////////////////////////////////////////自定义tcp客户端/////////////////////////////////////////////
typedef struct mk_tcp_client_t *mk_tcp_client;
//获取基类指针以便获取其网络相关信息
API_EXPORT mk_sock_info API_CALL mk_tcp_client_get_sock_info(const mk_tcp_client ctx);
2019-12-26 21:22:19 +08:00
typedef struct {
/**
* tcp客户端连接服务器成功或失败回调
* @param client tcp客户端
* @param code 0
* @param msg
*/
void (API_CALL *on_mk_tcp_client_connect)(mk_tcp_client client,int code,const char *msg);
/**
* tcp客户端与tcp服务器之间断开回调
* eof事件导致
* @param client tcp客户端
* @param code
* @param msg
*/
void (API_CALL *on_mk_tcp_client_disconnect)(mk_tcp_client client,int code,const char *msg);
/**
* tcp服务器发来的数据
* @param client tcp客户端
2022-05-25 15:38:32 +08:00
* @param buffer
2019-12-26 21:22:19 +08:00
*/
2022-05-25 15:38:32 +08:00
void (API_CALL *on_mk_tcp_client_data)(mk_tcp_client client, mk_buffer buffer);
2019-12-26 21:22:19 +08:00
/**
* 2
* @param client tcp客户端
*/
void (API_CALL *on_mk_tcp_client_manager)(mk_tcp_client client);
} mk_tcp_client_events;
/**
* tcp客户端
* @param events
* @param user_data
* @param type
* @return
*/
API_EXPORT mk_tcp_client API_CALL mk_tcp_client_create(mk_tcp_client_events *events, mk_tcp_type type);
/**
* tcp客户端
* @param ctx
*/
API_EXPORT void API_CALL mk_tcp_client_release(mk_tcp_client ctx);
/**
*
* @param ctx
* @param host ip或域名
* @param port
* @param time_out_sec
*/
API_EXPORT void API_CALL mk_tcp_client_connect(mk_tcp_client ctx, const char *host, uint16_t port, float time_out_sec);
/**
* 线
* 线
* @param ctx
* @param data
* @param len 0strlen获取
*/
API_EXPORT void API_CALL mk_tcp_client_send(mk_tcp_client ctx, const char *data, int len);
2022-05-25 15:38:32 +08:00
API_EXPORT void API_CALL mk_tcp_client_send_buffer(mk_tcp_client ctx, mk_buffer buffer);
2019-12-26 21:22:19 +08:00
/**
* 线
* @param ctx
* @param data
* @param len 0strlen获取
*/
API_EXPORT void API_CALL mk_tcp_client_send_safe(mk_tcp_client ctx, const char *data, int len);
2022-05-25 15:38:32 +08:00
API_EXPORT void API_CALL mk_tcp_client_send_buffer_safe(mk_tcp_client ctx, mk_buffer buffer);
2019-12-26 21:22:19 +08:00
/**
*
* @param ctx
* @param user_data
*/
API_EXPORT void API_CALL mk_tcp_client_set_user_data(mk_tcp_client ctx, void *user_data);
API_EXPORT void API_CALL mk_tcp_client_set_user_data2(mk_tcp_client ctx, void *user_data, on_user_data_free user_data_free);
2019-12-26 21:22:19 +08:00
/**
*
* @param ctx
* @return
*/
API_EXPORT void* API_CALL mk_tcp_client_get_user_data(mk_tcp_client ctx);
#ifdef __cplusplus
}
#endif
#endif //MK_TCP_H