Esempio n. 1
0
int32_t _broadcast_direct( struct iolayer * self, uint8_t index, struct session_manager * manager, struct message * msg )
{
    uint32_t i = 0;
    int32_t count = 0;

    // 数据改造
    if ( self->transform != NULL )
    {
        // 数据需要改造
        char * buffer = NULL;
        uint32_t nbytes = message_get_length( msg );
        buffer = self->transform( self->context, message_get_buffer(msg), &nbytes );
        if ( buffer == NULL )
        {
            // 数据改造失败
            message_destroy( msg );
            return -1;
        }
        if ( buffer != message_get_buffer(msg) )
        {
            // 数据改造成功
            message_set_buffer( msg, buffer, nbytes );
        }
    }

    for ( i = 0; i < sidlist_count(msg->tolist); ++i )
    {
        sid_t id = sidlist_get(msg->tolist, i);

        if ( SID_INDEX(id) != index )
        {
            message_add_failure( msg, id );
            continue;
        }

        struct session * session = session_manager_get( manager, id );
        if ( unlikely(session == NULL) )
        {
            message_add_failure( msg, id );
            continue;
        }

        if ( session_append(session, msg) >= 0 )
        {
            // 尝试单独发送
            // 添加到发送队列成功
            ++count;
        }
    }

    // 消息发送完毕, 直接销毁
    if ( message_is_complete(msg) )
    {
        message_destroy( msg );
    }

    return count;
}
Esempio n. 2
0
int32_t session_append( struct session * self, struct message * message )
{
    int32_t rc = -1;
    ioservice_t * service = &self->service;

    char * buf = message_get_buffer( message );
    uint32_t nbytes = message_get_length( message );

    // 数据改造(加密 or 压缩)
    char * buffer = service->transform( self->context, (const char *)buf, &nbytes );

    if ( buffer == buf )
    {
        // 消息未进行改造

        // 添加到会话的发送列表中
        rc = QUEUE_PUSH(sendqueue)(&self->sendqueue, &message);
        if ( rc == 0 )
        {
            // 注册写事件, 处理发送队列
            session_add_event( self, EV_WRITE );
        }
    }
    else if ( buffer != NULL )
    {
        // 消息改造成功

        rc = _send( self, buffer, nbytes );
        if ( rc >= 0 )
        {
            // 改造后的消息已经单独发送
            message_add_success( message );
        }

        free( buffer );
    }

    if ( rc < 0 )
    {
        message_add_failure( message, self->id );
    }

    return rc;
}