物联网主动复制结构
通信器
通过指定下面描述的参数来配置通信器。以下结构由函数 mco_iot_comm_params_init()
初始化,并传递给函数 mco_iot_comm_create()
:
typedef struct mco_iot_comm_params_t_
{
// 回调任务的数量等于运行时并行服务的最大连接数;
uint2 n_callback_threads;
MCO_SOCK_DOMAIN sock_domain. // 默认的套接字域(TCP 或本地域)
void *ssl_params; // 默认的 SSL 参数
timer_unit send_timeout; // 默认的写入超时时间(以毫秒为单位)
timer_unit recv_timeout; // 默认的读取超时时间(以毫秒为单位)
int listen_queue_size; // 监听套接字队列的大小
uint2 wakeup_port;
int compression_level;
} mco_iot_comm_params_t;
连接统计信息
以下结构用于维护物联网连接的统计信息:
typedef struct mco_iot_connection_stat_t_
{
mco_inetaddr_t inetaddr; /* peer IP */
int port; /* peer port */
uint8 sent_bytes; /* TX bytes */
uint8 recv_bytes; /* RX bytes */
uint2 sent_compression_ratio; /* TX 压缩比(百分比)*/
uint2 recv_compression_ratio; /* RX 压缩比(以百分比计) */
} mco_iot_connection_stat_t;
网络通信
以下结构用于物联网网络通信:
typedef struct mco_iot_ack_t_
{
uint8 seq; // 在mco_iot_replicator_sync()函数内部使用,以实现MCO_IOT_SYNC_WAIT模式。
uint8 sender_agent_id; // 发送方的agent_id
uint8 sender_db_id; // 发送方的数据库ID
uint8 receiver_agent_id; // 接收方的agent_id
uint8 timestamp; // “私有”对象(具有指定接收 ID)的确认时间*/
uint8 timestamp_cmn; // ‘common’对象(接收方ID == ALL_AGENTS)的确认时间*/
uint4 error_code; // 接收方的错误代码*/
uint4 flags; // mco_iot_replicator_sync() 函数的标志参数的子集
} mco_iot_ack_t;
复制器参数
主动复制结构的复制器通过指定以下描述的参数进行配置。以下结构由函数 mco_iot_replicator_params_init()
初始化,并传递给函数 mco_iot_replicator_create()
:
typedef struct mco_iot_replicator_params_t_
{
uint2 conn_pool_size; // 连接池的大小,复制器能够保持和重复使用的数据库连接的最大数量。
timer_unit wait_timeout; // 带有标志MCO_IOT_SYNC_WAIT的mco_iot_replicator_sync()函数的超时时间
} mco_iot_replicator_params_t;
回调函数
主动复制结构的回调函数是使用以下结构定义的函数。以下结构用于定义回调函数,这些函数通过 mco_iot_comm_register_callback()
注册,并通过 mco_iot_comm_unregister_callback()
注销:
typedef struct mco_iot_read_stream_t_
{
void *handle;
mco_stream_read reader;
} mco_iot_read_stream_t;
typedef struct iot_ack_t_
{
uint8 seq;
uint8 sender_agent_id;
uint8 receiver_agent_id;
uint8 timestamp;
uint4 error_code;
} iot_ack_t;
typedef struct iot_comm_callback_t_
{
int (*onConnect) (mco_iot_connection_h iotc, void *context);
int (*onReceive) (mco_iot_connection_h iotc, mco_iot_read_stream_t *stream, void *context);
int (*onAck) (mco_iot_connection_h iotc, const iot_ack_t *ack, void *context);
int (*onDisconnect) (mco_iot_connection_h iotc, void *context);
int (*onDestroy) (mco_iot_connection_h iotc, void *context);
} iot_comm_callback_t;
回调事件
在结构 iot_comm_callback_t 中定义的回调函数会在以下事件发生时被调用:
- onConnect - 当通过
connect()
或accept()
建立通信时调用。 - onReceive - 当接收到数据(一组序列化对象)时调用。
- onAck - 当收到先前发送数据的确认信息(
ACK
消息)时调用。 - onDisconnect - 当连接断开时调用。
- onDestroy - 在销毁连接之前被调用。
使用说明
context
参数是一个用户定义的上下文(指向包含应用程序特定数据的结构的指针)。在 iot_comm_callback_t_ 结构中注册的任何回调函数都必须返回 IOT_CALLBACK_OK
(成功完成)或 IOT_CALLBACK_FAIL
(出现错误时,相应的连接将被断开)。
另外请注意,如果未使用回调,则可以将其设置为 null
。
示例
{
...
iot_comm_callback_t my_callback = {&my_on_connect, 0, 0, &my_on_disconnect, 0};
...
mco_iot_comm_register_callback(comm, &my_callback, my_context);
}