예제 #1
0
spx_private err_t ydb_storage_query_remote(SpxLogDelegate *log,
        struct ydb_tracker *t,string_t groupname,
        string_t machineid,string_t syncgroup){/*{{{*/
    err_t err = 0;
    struct spx_job_context *sjc = t->sjc;
    int fd = spx_socket_new(&err);
    if(0 >= fd){
        SpxLog2(log,SpxLogError,err,"create query sync socket fd is fail.");
        return err;
    }
    if(0 != (err = spx_set_nb(fd))){
        SpxLog2(log,SpxLogError,err,"set socket nonblacking is fail.");
        goto r1;
    }
    if(0 != (err = spx_socket_set(fd,SpxKeepAlive,SpxAliveTimeout,\
                    SpxDetectTimes,SpxDetectTimeout,\
                    SpxLinger,SpxLingerTimeout,\
                    SpxNodelay,\
                    true,30))){
        SpxLog2(log,SpxLogError,err,"set socket operator is fail.");
        goto r1;
    }
    if(0 != (err = spx_socket_connect(fd,t->host.ip,t->host.port))){
        SpxLogFmt2(log,SpxLogError,err,\
                "conntect to tracker:%s:%d is fail.",\
                t->host.ip,t->host.port);

        goto r1;
    }

    struct spx_msg_header *writer_header = NULL;
    writer_header = spx_alloc_alone(sizeof(*writer_header),&err);
    if(NULL == writer_header){
        SpxLog2(log,SpxLogError,err,\
                "alloc writer header is fail.");
        goto r1;
    }
    sjc->writer_header = writer_header;
    writer_header->version = YDB_VERSION;
    writer_header->protocol = YDB_QUERY_SYNC_STORAGES;
    writer_header->bodylen = YDB_GROUPNAME_LEN + YDB_MACHINEID_LEN \
                             + YDB_SYNCGROUP_LEN ;
    struct spx_msg *ctx = spx_msg_new(writer_header->bodylen,&err);
    if(NULL == ctx){
        SpxLog2(log,SpxLogError,err,\
                "alloc writer body is fail.");
        goto r1;
    }
    sjc->writer_body_ctx = ctx;
    spx_msg_pack_fixed_string(ctx,groupname,YDB_GROUPNAME_LEN);
    spx_msg_pack_fixed_string(ctx,machineid,YDB_MACHINEID_LEN);
    spx_msg_pack_fixed_string(ctx,syncgroup,YDB_SYNCGROUP_LEN);

    sjc->fd = fd;
    spx_nio_regedit_writer(sloop,sjc->fd,sjc);
    return err;
r1:
    SpxClose(fd);
    return err;
}/*}}}*/
예제 #2
0
파일: logdb_server.c 프로젝트: alxbean/dfs
spx_private void * CreateMainSocket(void *arg){/*{{{*/
    struct server_config * conf = (struct server_config *) arg;
    SpxLogDelegate *log = conf->log;
    err_t err = 0;
    
    main_socket_loop = ev_loop_new(0);
    if(NULL == main_socket_loop){
        SpxLog2(log, SpxLogError, err, "create main socket loop is fail.");
        goto r1;
    }

    int mainsocket = spx_socket_new(&err);
    if(0 == mainsocket){
        SpxLog2(log, SpxLogError, err, "create main socket is fail.");
        goto r1;
    }

    if( 0 != (err = spx_set_nb(mainsocket))){
        SpxLog2(log, SpxLogError, err, "set main socket nonblock is fail.");
    }

    if( 0 != (err = spx_socket_start(mainsocket, conf->ip, conf->port,\
                                     true, conf->timeout,\
                                     3, conf->timeout,\
                                     false, 0,\
                                     true,\
                                     true, conf->timeout,
                                     1024))){
        SpxLog2(log, SpxLogError, err, "start main socket is fail.");
        goto r1;
    }

    SpxLogFmt1(log, SpxLogMark,
               "main socket fd: %d"
               "and accepting...",
               mainsocket);

    RegisterIOWatcher(&monitor_watcher, mainsocket, Reciver, EV_READ, log);
    ev_io_start(main_socket_loop, &monitor_watcher); 

    ev_run(main_socket_loop, 0);
r1:
    SpxClose(mainsocket);
    return NULL;
}/*}}}*/