Exemple #1
0
/* chunk reader */
int conn_chunk_reader(CONN *conn)
{
    int ret = -1, n = -1;
    CHUNK *cp = NULL;
    CONN_CHECK_RET(conn, ret);

    if(conn && conn->chunk) 
    {
        if(MB_NDATA(conn->buffer) > 0)
        {
            if((n = CHUNK_FILL(conn->chunk, MB_DATA(conn->buffer), MB_NDATA(conn->buffer))) > 0)
            {
                MB_DEL(conn->buffer, n);
            }
            if(CHUNK_STATUS(conn->chunk) == CHUNK_STATUS_OVER )
            {
                conn->s_state = S_STATE_DATA_HANDLING;
                conn->push_message(conn, MESSAGE_DATA);
                DEBUG_LOGGER(conn->logger, "Chunk completed %d bytes from %s:%d via %d",
                        CK_SIZE(conn->chunk), conn->ip, conn->port, conn->fd);
            }
            if(n > 0)
            {
                DEBUG_LOGGER(conn->logger, "Filled  %d byte(s) left:%lld to chunk from buffer "
                        "on conn[%s:%d] via %d", n, CK_LEFT(conn->chunk),
                        conn->ip, conn->port, conn->fd);
                ret = 0;
            }
        }
    }
    return ret;
}
Exemple #2
0
int wtable_stop(WTABLE *w)
{
    int ret = -1, i = 0;

    if(w) 
    {
        DEBUG_LOGGER(w->logger, "ready for stopping %d workers", w->state->nworkers);
        for(i = 1; i <= w->state->nworkers; i++)
        {
            if(w->workers[i].running == W_RUN_WORKING)
            {
                DEBUG_LOGGER(w->logger, "stopping workers[%d] => %d", i, w->workers[i].childid);
                w->workers[i].running = W_RUN_STOP;
                wtable_new_task(w, i, W_CMD_STOP);
#ifdef USE_PTHREAD
        pthread_join((pthread_t)(w->workers[i].childid), NULL);
#else
        waitpid((pid_t)w->workers[i].childid, NULL, 0);
#endif
                DEBUG_LOGGER(w->logger, "stop workers[%d] => %d", i, w->workers[i].childid);
            }
        }
        DEBUG_LOGGER(w->logger, "stop %d workers", w->state->nworkers);
        ret = 0;
    }
    return ret;
}
Exemple #3
0
/* write handler */
int conn_write_handler(CONN *conn)
{
    int ret = -1, n = 0;
    CONN_CHECK_RET(conn, ret);
    CHUNK *cp = NULL;

    if(conn && conn->send_queue && QTOTAL(conn->send_queue) > 0)
    {
        DEBUG_LOGGER(conn->logger, "Ready for send data to %s:%d via %d "
                "qtotal:%d qhead:%d qcount:%d",
                conn->ip, conn->port, conn->fd, QTOTAL(conn->send_queue),
                QHEAD(conn->send_queue), QCOUNT(conn->send_queue));   
        if(QUEUE_HEAD(conn->send_queue, PCHUNK, &cp) == 0)
        {
            DEBUG_LOGGER(conn->logger, "Ready for send data to %s:%d via %d qtotal:%d pcp:%08x",
                    conn->ip, conn->port, conn->fd, QTOTAL(conn->send_queue), cp);   
            if((n = CHUNK_WRITE(cp, conn->fd)) > 0)
            {
                conn->sent_data_total += n;
                DEBUG_LOGGER(conn->logger, "Sent %d byte(s) (total sent %lld) "
                        "to %s:%d via %d leave %lld", n, conn->sent_data_total, 
                        conn->ip, conn->port, conn->fd, CK_LEFT(cp));
                /* CONN TIMER sample */
                TIMER_SAMPLE(conn->timer);
                if(CHUNK_STATUS(cp) == CHUNK_STATUS_OVER )
                {
                    if(QUEUE_POP(conn->send_queue, PCHUNK, &cp) == 0)
                    {
                        DEBUG_LOGGER(conn->logger, "Completed chunk[%08x] and clean it leave %d",
                                cp, QTOTAL(conn->send_queue));
                        CK_CLEAN(cp);
                    }
                }
                ret = 0;
            }
            else
            {
                FATAL_LOGGER(conn->logger, "Sending data to %s:%d via %d failed, %s",
                        conn->ip, conn->port, conn->fd, strerror(errno));
                /* Terminate connection */
                CONN_TERMINATE(conn);
            }
        }
        if(QTOTAL(conn->send_queue) <= 0)
        {
            conn->event->del(conn->event, E_WRITE);
        }
    }
    return ret;
}
Exemple #4
0
/* set connection */
int conn_set(CONN *conn)
{
    short flag = 0;
    if(conn && conn->fd > 0 )
    {
        fcntl(conn->fd, F_SETFL, O_NONBLOCK);
        if(conn->evbase && conn->event)
        {
            flag = E_READ|E_PERSIST;
            if(conn->status == CONN_STATUS_READY) flag |= E_WRITE;
            conn->event->set(conn->event, conn->fd, flag, (void *)conn, &conn_event_handler);
            conn->evbase->add(conn->evbase, conn->event);
            DEBUG_LOGGER(conn->logger, "setting connection[%s:%d] via %d", 
                    conn->ip, conn->port, conn->fd);
            return 0;
        }
        else
        {
            FATAL_LOGGER(conn->logger, "Connection[%08x] fd[%d] evbase or"
                    "initialize event failed, %s", conn, conn->fd, strerror(errno));	
            /* Terminate connection */
            CONN_TERMINATE(conn);
        }
    }	
    return -1;	
}
Exemple #5
0
/* Delete event flags */
void event_del(EVENT *event, short flags)
{
	if(event)
	{
        MUTEX_LOCK(event->mutex);
		if(event->ev_flags & flags)
		{
            event->old_ev_flags = event->ev_flags;
			event->ev_flags ^= flags;
			if(event->ev_base)
			{
                DEBUG_LOGGER(event->ev_base->logger, "Updated event[%p] flags[%d] on fd[%d]",
                            event, event->ev_flags, event->ev_fd);
                if(event->ev_flags & (E_READ|E_WRITE))
                {
                    event->ev_base->update(event->ev_base, event);
                }
                else
                {
                    event->ev_base->del(event->ev_base, event);
                }
			}
		}
        MUTEX_UNLOCK(event->mutex);
	}	
    return ;
}
Exemple #6
0
/**
 *@brief 记录二进制格式的  debug 级别日志
 */
void debug_hex_log(const char* _buf, int _len)
{
    //for (int i=0; i < _len; i++)
    //{

    //    if (0 == i % 16)
    //    {
    //        PRINTF_LOGGER(dsmplog,"\n%d\t",i);
    //    }
    //    PRINTF_LOGGER(dsmplog, "%02X ", (unsigned char)_buf[i] & 0xFF);
    //}
    //PRINTF_LOGGER(dsmplog, "\n");
    int j=0;
    char b[1024]={0};
    for (int i=0; i<_len; i++) {
        if (i % 16 == 0) {
            sprintf(b+3*i+j, "\n%02X ", (unsigned char)_buf[i] & 0xFF);
            j++;
        }
        else {
            sprintf(b+3*i+j, "%02X ", (unsigned char)_buf[i] & 0xFF);
        }
    }
    DEBUG_LOGGER(dsmplog, "%s", b);
}
Exemple #7
0
/* Add new event to evbase */
int evselect_add(EVBASE *evbase, EVENT *event)
{
    short ev_flags = 0;
    if(evbase && event && event->ev_fd >= 0 && event->ev_fd < evbase->allowed)
    {
        //MUTEX_LOCK(evbase->mutex);
        event->ev_base = evbase;
        if(evbase->ev_read_fds && (event->ev_flags & E_READ))
        {
            FD_SET(event->ev_fd, (fd_set *)evbase->ev_read_fds);
            ev_flags |= E_READ;
        }
        if(evbase->ev_read_fds && (event->ev_flags & E_WRITE))
        {
            ev_flags |= E_WRITE;
            FD_SET(event->ev_fd, (fd_set *)evbase->ev_write_fds);	
        }
        if(ev_flags)
        {
            if(event->ev_fd > evbase->maxfd) evbase->maxfd = event->ev_fd;
            evbase->evlist[event->ev_fd] = event;	
            ++(evbase->nfd);
            DEBUG_LOGGER(evbase->logger, "Added event[%p] flags[%d] on fd[%d]", 
                    event, ev_flags, event->ev_fd);
        }
        //MUTEX_UNLOCK(evbase->mutex);
        return 0;
    }	
    return -1;
}
Exemple #8
0
/* resume */
int tasktable_resume_task(TASKTABLE *tasktable)
{
    int fd = -1, ret = -1;
    TASK task;

    if(tasktable)
    {
        if((fd = open(tasktable->taskfile, O_RDONLY, 0644)) > 0)
        {
            //DEBUG_LOGGER(tasktable->logger, "open file:%s fd:%d\n", tasktable->taskfile, fd);
            while(read(fd, &task, sizeof(TASK)) == sizeof(TASK))
            {
                //DEBUG_LOGGER(tasktable->logger, "read %d \n", task.id);
                tasktable->ntask++;
                if(tasktable->running_task.id >= 0) continue;
                if(task.status != TASK_STATUS_OVER)
                {
                    memcpy(&(tasktable->running_task), &task, sizeof(TASK));
                }
            }
            //DEBUG_LOGGER(tasktable->logger, "read file:%s failed, %s\n", 
            //tasktable->taskfile, strerror(errno));
            ret = 0;
            close(fd);
        }
        else
        {
            DEBUG_LOGGER(tasktable->logger, "resume taskfile:%s failed, %s", 
                    tasktable->taskfile, strerror(errno));
        }
    }
    return ret;
}
Exemple #9
0
/* check task status */
int tasktable_check_status(TASKTABLE *tasktable)
{
    TASK task = {0};
    int fd = -1;
    int ret = -1;
    long long offset = 0;

    if(tasktable)
    {
        if(tasktable->status != NULL) return 0;
        MUTEX_LOCK(tasktable->mutex);
        //check running taskid
        if (tasktable->running_task_id < tasktable->ntask)
        {
            if((fd = open(tasktable->taskfile, O_RDONLY)) > 0) 
            {
                //DEBUG_LOGGER(tasktable->logger, "taskfile fd:%d\n", fd);
                offset = sizeof(TASK ) * (tasktable->running_task_id+1) * 1;
                if(lseek(fd, offset, SEEK_SET) == offset)
                {
                    while(read(fd, &(tasktable->running_task), sizeof(TASK)) == sizeof(TASK))
                    {
                        if(tasktable->running_task.status != TASK_STATUS_OVER)
                        {
                            DEBUG_LOGGER(tasktable->logger, 
                                    "running_task taskid:%d id:%d file:%s", 
                                    tasktable->running_task_id, tasktable->running_task.id,
                                    tasktable->running_task.file);
                            tasktable->running_task_id = tasktable->running_task.id;
                            ret = 0;
                            break;
                        }
                    }
                    //DEBUG_LOGGER(tasktable->logger, "taskfile fd:%d offset:%lld \n", fd, offset);
                }
                else
                {
                    DEBUG_LOGGER(tasktable->logger, "LSEEK taskfile %s offset:%lld failed, %s", 
                            tasktable->taskfile, offset, strerror(errno));
                }
                close(fd);
            }
        }
        MUTEX_UNLOCK(tasktable->mutex);
    }
    return ret;
}
Exemple #10
0
/* Terminate procthread */
void procthread_terminate(PROCTHREAD *pth)
{
    if(pth)
    {
        DEBUG_LOGGER(pth->logger, "Ready for closing procthread[%d]", pth->index);
        pth->running_status = 0;
    }
}
Exemple #11
0
/* set logfile */
void evbase_set_logfile(EVBASE *evbase, char *file)
{
    if(evbase)
    {
        LOGGER_INIT(evbase->logger, file);
        DEBUG_LOGGER(evbase->logger, "Initailize evlog[%s]", file);
    }
    return ;
}
Exemple #12
0
/* Update event in evbase */
int evselect_update(EVBASE *evbase, EVENT *event)
{
    short ev_flags = 0, add_ev_flags = 0, del_ev_flags = 0;
    if(evbase && event && event->ev_fd >= 0 && event->ev_fd <= evbase->maxfd)
    {
        //MUTEX_LOCK(evbase->mutex);
        ev_flags = (event->ev_flags ^ event->old_ev_flags);
        add_ev_flags = (event->ev_flags & ev_flags);
        del_ev_flags = (event->old_ev_flags & ev_flags);

        if(add_ev_flags & E_READ)
        {
            FD_SET(event->ev_fd, (fd_set *)evbase->ev_read_fds);
            ev_flags |= E_READ;
            DEBUG_LOGGER(evbase->logger, "Added EV_READ on fd[%d]", event->ev_fd);
        }
        if(del_ev_flags & E_READ)
        {
            FD_CLR(event->ev_fd, (fd_set *)evbase->ev_read_fds);
            DEBUG_LOGGER(evbase->logger, "Deleted EV_READ on fd[%d]", event->ev_fd);
        }
        if(add_ev_flags & E_WRITE)
        {
            FD_SET(event->ev_fd, (fd_set *)evbase->ev_write_fds);
            ev_flags |= E_WRITE;
            DEBUG_LOGGER(evbase->logger, "Added EV_WRITE on fd[%d]", event->ev_fd);
        }
        if(del_ev_flags & E_WRITE)
        {
            FD_CLR(event->ev_fd, (fd_set *)evbase->ev_write_fds);
            DEBUG_LOGGER(evbase->logger, "Deleted EV_WRITE on fd[%d]", event->ev_fd);
        }
        if(event->ev_fd > evbase->maxfd) evbase->maxfd = event->ev_fd;
        evbase->evlist[event->ev_fd] = event;
        DEBUG_LOGGER(evbase->logger, "Updated event[%p] flags[%d] on fd[%d]",
                event, event->ev_flags, event->ev_fd);
        //MUTEX_UNLOCK(evbase->mutex);
        return 0;
    }
    return -1;
}
Exemple #13
0
static void
debug_logger_get_property(GObject *object,
                          guint prop_id,
                          GValue *value,
                          GParamSpec *pspec)
{
    DebugLogger *logger;

    logger = DEBUG_LOGGER(object);

    switch(prop_id)
    {
        case PROP_CRITICAL_COLOR:
            g_value_set_string(value, logger->critical_color);
            break;

        case PROP_WARNING_COLOR:
            g_value_set_string(value, logger->warning_color);
            break;

        case PROP_MESSAGE_COLOR:
            g_value_set_string(value, logger->message_color);
            break;

        case PROP_INFO_COLOR:
            g_value_set_string(value, logger->info_color);
            break;

        case PROP_DEBUG_COLOR:
            g_value_set_string(value, logger->debug_color);
            break;

        case PROP_UNKNOWN_COLOR:
            g_value_set_string(value, logger->unknown_color);
            break;

        case PROP_USE_COLOR:
            g_value_set_boolean(value, logger->use_color);
            break;

        case PROP_UI_ENABLED:
            g_value_set_boolean(value, logger->ui_enabled);
            break;

        case PROP_CLOSEABLE:
            g_value_set_boolean(value, logger->closeable);
            break;

        default:
            G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
            break;
    }
}
Exemple #14
0
/* Add new connection */
int procthread_add_connection(PROCTHREAD *pth, CONN *conn)
{
    int ret = -1;

    if(pth && conn)
    {
            DEBUG_LOGGER(pth->logger, "Ready for add connection[%s:%d] via %d to pool",
                    conn->ip, conn->port, conn->fd);
        conn->message_queue = pth->message_queue;
        conn->parent = pth;
        if(conn->set(conn) == 0)
        {
            DEBUG_LOGGER(pth->logger, "Ready for add connection[%s:%d] via %d to pool",
                    conn->ip, conn->port, conn->fd);
            ret = pth->service->pushconn(pth->service, conn);
            DEBUG_LOGGER(pth->logger, "Ready for add connection[%s:%d] via %d to pool",
                    conn->ip, conn->port, conn->fd);
        }
    }
    return ret;
}
Exemple #15
0
/* worker  terminate */
int wtable_worker_terminate(WTABLE *w, int wid)
{
    if(w && wid >= 0)
    {
        DEBUG_LOGGER(w->logger, "terminate workers[%d] childid:%lld", wid, w->workers[wid].childid);
        MUTEX_DESTROY(w->workers[wid].mmlock);
        mqueue_clean(w->workers[wid].queue);
        mtree_close(w->workers[wid].map);
        memset(&(w->workers[wid]), 0, sizeof(WORKER));
    }
    return 0;
}
Exemple #16
0
/* Add connection message */
int procthread_addconn(PROCTHREAD *pth, CONN *conn)
{
    int ret = -1;

    if(pth && pth->message_queue && conn  && pth->lock == 0)
    {
        PUSH_TASK_MESSAGE(pth, MESSAGE_NEW_SESSION, -1, conn->fd, -1, conn, NULL);
        DEBUG_LOGGER(pth->logger, "Ready for adding msg[%s] connection[%p][%s:%d] d_state:%d on %s:%d via %d total %d", messagelist[MESSAGE_NEW_SESSION], conn, conn->remote_ip, conn->remote_port, conn->d_state, conn->local_ip, conn->local_port, conn->fd, QMTOTAL(pth->message_queue));
        ret = 0;
    }
    return ret;
}
Exemple #17
0
/* add new transaction */
int procthread_newtransaction(PROCTHREAD *pth, CONN *conn, int tid)
{
    int ret = -1;

    if(pth && pth->message_queue && conn && pth->lock == 0)
    {
        PUSH_TASK_MESSAGE(pth,MESSAGE_TRANSACTION, -1, conn->fd, tid, conn, NULL);
        DEBUG_LOGGER(pth->logger, "Added thread[%p]->qmessage[%p] transaction[%d] to conn[%p][%s:%d] on %s:%d via %d total %d", (void *)(pth->threadid),  pth->message_queue, tid, conn, conn->remote_ip, conn->remote_port, conn->local_ip, conn->local_port, conn->fd, QMTOTAL(pth->message_queue));
        ret = 0;
    }
    return ret;
}
Exemple #18
0
/* push new connection  */
int procthread_pushconn(PROCTHREAD *pth, int fd, void *ssl)
{
    int ret = -1;

    if(pth && pth->message_queue && fd > 0)
    {
        PUSH_TASK_MESSAGE(pth,MESSAGE_NEW_CONN, -1, fd, -1, ssl, NULL);
        DEBUG_LOGGER(pth->logger, "Added message[NEW_CONN][%d] to procthreads[%d] qtotal:%d", fd, pth->index, QMTOTAL(pth->message_queue));
        ret = 0;
    }
    return ret;
}
Exemple #19
0
/* Reset evbase */
void evselect_reset(EVBASE *evbase)
{
    if(evbase)
    {
        evbase->nfd = 0;
        evbase->maxfd = 0;
        evbase->nevent = 0;
        FD_ZERO((fd_set *)evbase->ev_read_fds);
        FD_ZERO((fd_set *)evbase->ev_write_fds);
        DEBUG_LOGGER(evbase->logger, "Reset evbase[%p]", evbase);
    }
    return ;
}
Exemple #20
0
/* read handler */
int conn_read_handler(CONN *conn)
{
    int ret = -1, n = -1;
    CONN_CHECK_RET(conn, ret);

    if(conn)
    {
        /* Receive OOB */
        if((n = MB_RECV(conn->oob, conn->fd, MSG_OOB)) > 0)
        {
            conn->recv_oob_total += n;
            DEBUG_LOGGER(conn->logger, "Received %d bytes OOB total %lld from %s:%d via %d",
                    n, conn->recv_oob_total, conn->ip, conn->port, conn->fd);
            conn->oob_handler(conn);
            /* CONN TIMER sample */
            TIMER_SAMPLE(conn->timer);
            return (ret = 0);
        }
        /* Receive to chunk with chunk_read_state before reading to buffer */
        CONN_CHUNK_READ(conn, n);
        /* Receive normal data */
        if((n = MB_READ(conn->buffer, conn->fd)) <= 0)
        {
            FATAL_LOGGER(conn->logger, "Reading %d bytes left:%d data from %s:%d via %d failed, %s",
                    n, MB_LEFT(conn->buffer), conn->ip, conn->port, conn->fd, strerror(errno));
            /* Terminate connection */
            CONN_TERMINATE(conn);
            return (ret = 0);
        }
        /* CONN TIMER sample */
        TIMER_SAMPLE(conn->timer);
        conn->recv_data_total += n;
        DEBUG_LOGGER(conn->logger, "Received %d bytes data total %lld from %s:%d via %d",
                n, conn->recv_data_total, conn->ip, conn->port, conn->fd);
        conn->packet_reader(conn);
        ret = 0;
    }
    return ret;
}
Exemple #21
0
/* terminate connection */
int conn_terminate(CONN *conn)
{
    int ret = -1;

    if(conn)
    {
        if(conn->c_state == C_STATE_USING && conn->session.error_handler)
        {
            DEBUG_LOGGER(conn->logger, "error handler session[%s:%d] via %d cid:%d", 
                    conn->ip, conn->port, conn->fd, conn->c_id);
            conn->session.error_handler(conn, PCB(conn->packet), PCB(conn->cache), PCB(conn->chunk));
        }
        conn->event->destroy(conn->event);
        DEBUG_LOGGER(conn->logger, "terminateing session[%s:%d] via %d",
                conn->ip, conn->port, conn->fd);
        shutdown(conn->fd, SHUT_RDWR);
        close(conn->fd);
        conn->fd = -1;
        ret = 0;
    }
    return ret;
}
Exemple #22
0
/* stop procthread */
void procthread_stop(PROCTHREAD *pth)
{
    MESSAGE msg = {0};

    if(pth && pth->message_queue)
    {
        msg.msg_id      = MESSAGE_STOP;
        msg.parent      = (void *)pth;
        QUEUE_PUSH(pth->message_queue, MESSAGE, &msg);
        DEBUG_LOGGER(pth->logger, "Ready for stopping procthread[%d]", pth->index);
    }
    return ;
}
Exemple #23
0
/* Add new connection */
int procthread_add_connection(PROCTHREAD *pth, CONN *conn)
{
    int ret = -1;

    if(pth && conn)
    {
        DEBUG_LOGGER(pth->logger, "Ready for add connection[%p][%s:%d] d_state:%d on %s:%d via %d to pool", conn, conn->remote_ip, conn->remote_port, conn->d_state, conn->local_ip, conn->local_port, conn->fd);
        conn->message_queue = pth->message_queue;
        conn->evbase        = pth->evbase;
        conn->indaemon      = pth->indaemon;
        conn->inqmessage    = pth->inqmessage;
        conn->outdaemon     = pth->outdaemon;
        conn->outqmessage   = pth->outqmessage;
        conn->outevbase     = pth->outevbase;
        conn->parent        = pth;
        conn->service       = pth->service;
        if(pth->service->pushconn(pth->service, conn) == 0 && conn->set(conn) == 0)
        {
            DEBUG_LOGGER(pth->logger, "Ready for add conn[%p][%s:%d] d_state:%d on %s:%d via %d to pool", conn, conn->remote_ip, conn->remote_port, conn->d_state, conn->local_ip, conn->local_port, conn->fd);
        }
    }
    return ret;
}
Exemple #24
0
/* dump tasktable to file */
int tasktable_dump_task(TASKTABLE *tasktable)
{
    int fd = 0;
    int i = 0;
    int ret = -1;
    long long offset = 0;

    if(tasktable && tasktable->running_task.id >= 0 
            && tasktable->running_task.id < tasktable->ntask)
    {
        if((fd = open(tasktable->taskfile, O_RDWR)) > 0)
        {
            offset = sizeof(TASK) * tasktable->running_task.id; 
            //DEBUG_LOGGER(tasktable->logger, "Ready for dump running_task:%d offset:%lld\n", 
            //    tasktable->running_task.id, offset);
            if(lseek(fd, offset, SEEK_SET) == offset)
            {
                if(write(fd, &(tasktable->running_task), sizeof(TASK)) == sizeof(TASK))
                {
                    //DEBUG_LOGGER(tasktable->logger, "Dump task %d\n", tasktable->running_task.id);
                    ret = 0;
                }
            }
            else
            {
                DEBUG_LOGGER(tasktable->logger, "LSEEK %s offset:%lld failed, %s\n", 
                        tasktable->taskfile, offset, strerror(errno));
            }
            close(fd);
        }
        else
        {
            DEBUG_LOGGER(tasktable->logger, "dump taskfile %s failed, %s", tasktable->taskfile, strerror(errno));
        }
    }
    return ret;
}
Exemple #25
0
/* new connection */
int procthread_newconn(PROCTHREAD *pth, int fd, void *ssl)
{
    socklen_t rsa_len = sizeof(struct sockaddr_in);
    struct sockaddr_in rsa;
    SERVICE *service = NULL;
    int port = 0, ret = -1;
    CONN *conn = NULL;
    char *ip = NULL;

    if(pth && fd > 0 && (service = pth->service))
    {
        if(getpeername(fd, (struct sockaddr *)&rsa, &rsa_len) == 0
                && (ip = inet_ntoa(rsa.sin_addr)) && (port = ntohs(rsa.sin_port)) > 0 
                && (conn = service_addconn(service, service->sock_type, fd, ip, port, 
                    service->ip, service->port, &(service->session), ssl, CONN_STATUS_FREE)))
        {
            DEBUG_LOGGER(pth->logger, "adding new-connection[%p][%s:%d] via %d", conn, ip, port, fd);
            ret = 0;
        }
        else
        {
#ifdef HAVE_SSL
            if(ssl)
            {
                SSL_shutdown((SSL *)ssl);
                SSL_free((SSL *)ssl);
                ssl = NULL;
            }
#endif
            DEBUG_LOGGER(pth->logger, "adding new-connection[%d] failed,%s", fd, strerror(errno));
            shutdown(fd, SHUT_RDWR);
            close(fd);
        }
        return ret;
    }
    return -1;
}
Exemple #26
0
static void
debug_logger_finalize(GObject *object)
{
    DebugLogger *self = DEBUG_LOGGER(object);
    GObjectClass *parent_class;

    while(self->priv->domains != NULL)
        debug_logger_remove_domain(self,
                ((DomainHandler *)(self->priv->domains->data))->name);

    g_slist_free(self->priv->domains);

    parent_class = g_type_class_peek_parent(G_OBJECT_GET_CLASS(object));
    parent_class->finalize(object);
}
Exemple #27
0
/* Loop evbase */
int evselect_loop(EVBASE *evbase, short loop_flag, struct timeval *tv)
{
    int i = 0, n = 0;
    short ev_flags = 0;
    fd_set rd_fd_set, wr_fd_set ;
    EVENT *ev = NULL;
    struct timeval timeout = {0};

    //if(evbase  && evbase->nfd > 0)
    if(evbase)
    {
        FD_ZERO(&rd_fd_set);
        memcpy(&rd_fd_set, evbase->ev_read_fds, sizeof(fd_set));
        FD_ZERO(&wr_fd_set);
        memcpy(&wr_fd_set, evbase->ev_write_fds, sizeof(fd_set));
        if(tv == NULL)
        {
            timeout.tv_sec = 0;
            timeout.tv_usec = 1000;
            tv = &timeout;
        }
        n = select(evbase->allowed, &rd_fd_set, &wr_fd_set, NULL, tv);
        //fprintf(stdout, "%s::%d n:%d\n", __FILE__, __LINE__, n);
        if(n <= 0) return n;
        DEBUG_LOGGER(evbase->logger, "Actived %d event in %d", n,  evbase->allowed);
        for(i = 0; i < evbase->allowed; ++i)
        {
            if((ev = evbase->evlist[i]))
            {
                ev_flags = 0;
                if(FD_ISSET(i, &rd_fd_set))	
                {
                    ev_flags |= E_READ;
                }
                if(FD_ISSET(i, &wr_fd_set))
                {
                    ev_flags |= E_WRITE;
                }
                if(ev_flags == 0) continue;
                if((ev_flags  &= ev->ev_flags))	
                {
                    event_active(ev, ev_flags);
                }
            }			
        }
    }
    return n;
}
Exemple #28
0
/* Destroy event */
void event_destroy(EVENT *event)
{
    if(event)
    {
        //MUTEX_LOCK(event->mutex);
        event->ev_flags = 0;
        if(event->ev_base && event->ev_base->del)
        {
            DEBUG_LOGGER(event->ev_base->logger, "Destroy event[%p] on fd[%d]",event, event->ev_fd);
            event->ev_base->del(event->ev_base, event);
            event->ev_base = NULL;
        }
        //MUTEX_UNLOCK(event->mutex);
    }
    return ;
}
Exemple #29
0
/* add new task */
int procthread_newtask(PROCTHREAD *pth, CALLBACK *task_handler, void *arg)
{
    MESSAGE msg = {0};
    int ret = -1;

    if(pth && pth->message_queue && task_handler)
    {
        msg.msg_id      = MESSAGE_TASK;
        msg.parent      = pth;
        msg.handler     = task_handler;
        msg.arg         = arg;
        QUEUE_PUSH(pth->message_queue, MESSAGE, &msg);
        DEBUG_LOGGER(pth->logger, "Added message task to procthreads[%d]", pth->index);
        ret = 0;
    }
    return ret;
}
Exemple #30
0
/* receive chunk */
int conn_recv_chunk(CONN *conn, int size)
{
    int ret = -1, n = -1;
    CHUNK *cp = NULL;
    CONN_CHECK_RET(conn, ret);

    if(conn && conn->chunk)
    {
        DEBUG_LOGGER(conn->logger, "Ready for chunk size:%ld on %s:%d via %d",
                size, conn->ip, conn->port, conn->fd);
        CK_MEM(conn->chunk, size);
        conn->s_state = S_STATE_READ_CHUNK;
        conn->chunk_reader(conn);
        CONN_CHUNK_READ(conn, n);
        ret = 0;
    }
    return ret;
}