Exemplo n.º 1
0
int swDataBuffer_clear(swDataBuffer *data_buffer, int fd)
{
	swDataBuffer_item *item = NULL;
	swHashMap_add_int(&data_buffer->map, fd, item);
	if (item == NULL)
	{
		swTrace("buffer item not found\n");
		return SW_ERR;
	}
	else
	{
		swDataBuffer_trunk *trunk = item->head;
		swDataBuffer_trunk *will_free_trunk; //保存trunk的指针,用于释放内存
		while (trunk != NULL)
		{
			sw_free(trunk->data);
			will_free_trunk = trunk;
			trunk = trunk->next;
			sw_free(will_free_trunk);
		}
		swHashMap_del_int(&data_buffer->map, fd);
		sw_free(item);
	}
	return SW_OK;
}
Exemplo n.º 2
0
int swDataBuffer_flush(swDataBuffer *data_buffer, swDataBuffer_item *item)
{
	if(item->head == NULL)
	{
		return SW_ERR;
	}
	item->head->len = 0;
	item->tail = item->head;
	item->trunk_num = 1;
	item->length = 0;

	swDataBuffer_trunk *trunk = item->head->next;
	swDataBuffer_trunk *will_free_trunk; //保存trunk的指针,用于释放内存

	while (trunk!= NULL)
	{
		trunk->len = 0;
		sw_free(trunk->data);
		will_free_trunk = trunk;
		trunk = trunk->next;    //这里会指向下个指针,所以需要保存
		sw_free(will_free_trunk);
//		swWarn("will_free_trunk");
	}
	item->head->next = NULL;
	return SW_OK;
}
Exemplo n.º 3
0
static int swFactoryThread_writer_loop(swThreadParam *param)
{
	swFactory *factory = param->object;
	swFactoryThread *this = factory->object;
	int pti = param->pti;
	int ret;
	swEventData *req;

	//main loop
	while (swoole_running > 0)
	{
		ret = read(this->writers[pti].evfd, &req, sizeof(&req));
		printf("[WriteThread]recv: %d|ret=%d\n", pti, ret);
		if (ret > 0)
		{
			factory->onTask(factory, req);
			sw_free(req);
		}
		else
		{
			swTrace("[swFactoryThread_writer_loop]read eventfd error");
		}
	}
	//shutdown
	close(this->writers[pti].evfd);
	sw_free(param);
	pthread_exit(SW_OK);
	return SW_OK;
}
Exemplo n.º 4
0
static void swReactorKqueue_free(swReactor *reactor)
{
    swReactorKqueue *this = reactor->object;
    close(this->epfd);
    sw_free(this->events);
    sw_free(this);
}
Exemplo n.º 5
0
static int swFactoryThread_writer_loop(swThreadParam *param)
{
	swFactory *factory = param->object;
	swFactoryThread *this = factory->object;
	int pti = param->pti;
	swEventData *req;
	uint64_t flag;

	//main loop
	while (swoole_running > 0)
	{
		if(swRingBuffer_pop(&(this->buffers[pti]), (void **)&req)==0)
		{
			factory->onTask(factory, req);
			sw_free(req);
		}
		else
		{
			read(this->writers[pti].evfd, &flag, sizeof(flag));
		}
	}
	factory->running = 0;
	//shutdown
	close(this->writers[pti].evfd);
	sw_free(param);
	pthread_exit(SW_OK);
	return SW_OK;
}
Exemplo n.º 6
0
void swPort_free(swListenPort *port)
{
#ifdef SW_USE_OPENSSL
    if (port->ssl)
    {
        if (port->ssl_context)
        {
            swSSL_free_context(port->ssl_context);
        }
        sw_free(port->ssl_option.cert_file);
        sw_free(port->ssl_option.key_file);
        if (port->ssl_option.client_cert_file)
        {
            sw_free(port->ssl_option.client_cert_file);
        }
    }
#endif

    close(port->sock);

    //remove unix socket file
    if (port->type == SW_SOCK_UNIX_STREAM || port->type == SW_SOCK_UNIX_DGRAM)
    {
        unlink(port->host);
    }
}
Exemplo n.º 7
0
void swReactorEpoll_free(swReactor *reactor)
{
    swReactorEpoll *object = reactor->object;
    close(object->epfd);
    sw_free(object->events);
    sw_free(object);
}
Exemplo n.º 8
0
int swBuffer_flush(swBuffer *buffer)
{
	if(buffer->head == NULL)
	{
		return SW_ERR;
	}

	buffer->head->length = 0;
	buffer->tail = buffer->head;
	buffer->trunk_num = 1;
	buffer->length = 0;

	swBuffer_trunk *trunk = buffer->head->next;
	swBuffer_trunk *will_free_trunk; //保存trunk的指针,用于释放内存

	while (trunk!= NULL)
	{
		trunk->length = 0;
		sw_free(trunk->data);
		will_free_trunk = trunk;
		trunk = trunk->next;    //这里会指向下个指针,所以需要保存
		sw_free(will_free_trunk);
//		swWarn("will_free_trunk");
	}
	buffer->head->next = NULL;
	return SW_OK;
}
Exemplo n.º 9
0
int swTimer_del(swTimer *timer, swTimer_node *tnode)
{
    if (tnode->remove)
    {
        return SW_FALSE;
    }
    if (SwooleG.timer._current_id > 0 && tnode->id == SwooleG.timer._current_id)
    {
        tnode->remove = 1;
        return SW_TRUE;
    }
    if (swHashMap_del_int(timer->map, tnode->id) < 0)
    {
        return SW_ERR;
    }
    //remove from min-heap
    swHeap_remove(timer->heap, tnode->heap_node);
    if (tnode->heap_node)
    {
        sw_free(tnode->heap_node);
    }
    sw_free(tnode);
    timer->num --;
    return SW_TRUE;
}
Exemplo n.º 10
0
void swReactorEpoll_free(swReactor *reactor)
{
	swReactorEpoll *this = reactor->object;
	close(this->epfd);
	sw_free(this->events);
	sw_free(this);
}
Exemplo n.º 11
0
Arquivo: buffer.c Projeto: 899/swoole
int swDataBuffer_clear(swDataBuffer *data_buffer, int fd)
{
	swDataBuffer_item *item = NULL;
	HASH_FIND_INT(data_buffer->ht, &fd, item);
	if (item == NULL)
	{
		swTrace("buffer item not found\n");
		return SW_ERR;
	}
	else
	{
		swDataBuffer_trunk *trunk = item->first;
		swDataBuffer_trunk *will_free_trunk; //保存trunk的指针,用于释放内存
		while (trunk != NULL)
		{
			sw_free(trunk->data);
			will_free_trunk = trunk;
			trunk = trunk->next;
			sw_free(will_free_trunk);
		}
		HASH_DEL(data_buffer->ht, item);
		sw_free(item);
	}
	return SW_OK;
}
Exemplo n.º 12
0
static int swFactoryThread_writer_loop(swThreadParam *param)
{
	swFactory *factory = param->object;
	swServer *serv = factory->ptr;
	swFactoryThread *this = factory->object;
	int pti = param->pti;
	int ret;
	swEventData *req;
	uint64_t flag;

	//cpu affinity setting
#if HAVE_CPU_AFFINITY
	if (serv->open_cpu_affinity)
	{
		cpu_set_t cpu_set;
		CPU_ZERO(&cpu_set);
		CPU_SET(pti % SW_CPU_NUM, &cpu_set);
		if (0 != pthread_setaffinity_np(pthread_self(), sizeof(cpu_set), &cpu_set))
		{
			swTrace("pthread_setaffinity_np set fail\n");
		}
	}
#endif

	if (serv->onWorkerStart != NULL)
	{
		serv->onWorkerStart(serv, pti);
	}
	swSingalNone();
	//main loop
	while (swoole_running > 0)
	{
		if (swRingQueue_pop(&(this->queues[pti]), (void **) &req) == 0)
		{
			factory->last_from_id = req->info.from_id;
			factory->onTask(factory, req);
			sw_free(req);
		}
		else
		{
			ret = this->writers[pti].evfd.read(&this->writers[pti].evfd, &flag, sizeof(flag));
			if (ret < 0)
			{
				swTrace("read fail.errno=%d", errno);
			}
		}
	}
	//shutdown
	this->writers[pti].evfd.close(&this->writers[pti].evfd);

	if (serv->onWorkerStop != NULL)
	{
		serv->onWorkerStop(serv, pti);
	}
	sw_free(param);
	pthread_exit(SW_OK);
	return SW_OK;
}
Exemplo n.º 13
0
void swTimer_del(swTimer *timer, swTimer_node *tnode)
{
    swHeap_remove(timer->heap, tnode->heap_node);
    if (tnode->heap_node)
    {
        sw_free(tnode->heap_node);
    }
    sw_free(tnode);
}
Exemplo n.º 14
0
int swFactoryThread_shutdown(swFactory *factory)
{
	swoole_running = 0;
	swFactoryThread *this = factory->object;
	sw_free(this->writers);
	sw_free(this->queues);
	sw_free(this);
	return SW_OK;
}
Exemplo n.º 15
0
static void swProcessPool_free(swProcessPool *pool)
{
	int i;
	swPipe *pipe;
	for (i = 0; i < pool->worker_num; i++)
	{
		pipe = &pool->pipes[i];
		pipe->close(pipe);
	}
	sw_free(pool->workers);
	sw_free(pool->pipes);
	swHashMap_free(&pool->map);
}
Exemplo n.º 16
0
void swTimeWheel_free(swTimeWheel *tw)
{
    int i;
    for (i = 0; i < tw->size; i++)
    {
        if (tw->wheel[i] != NULL)
        {
            swHashMap_free(tw->wheel[i]);
            tw->wheel[i] = NULL;
        }
    }
    sw_free(tw->wheel);
    sw_free(tw);
}
Exemplo n.º 17
0
static swTimer_node* swTimer_add(swTimer *timer, int _msec, int interval, void *data, swTimerCallback callback)
{
    swTimer_node *tnode = sw_malloc(sizeof(swTimer_node));
    if (!tnode)
    {
        swSysError("malloc(%ld) failed.", sizeof(swTimer_node));
        return NULL;
    }

    int64_t now_msec = swTimer_get_relative_msec();
    if (now_msec < 0)
    {
        sw_free(tnode);
        return NULL;
    }

    tnode->data = data;
    tnode->type = SW_TIMER_TYPE_KERNEL;
    tnode->exec_msec = now_msec + _msec;
    tnode->interval = interval ? _msec : 0;
    tnode->remove = 0;
    tnode->callback = callback;
    tnode->round = timer->round;

    if (timer->_next_msec < 0 || timer->_next_msec > _msec)
    {
        timer->set(timer, _msec);
        timer->_next_msec = _msec;
    }

    tnode->id = timer->_next_id++;
    if (unlikely(tnode->id < 0))
    {
        tnode->id = 1;
        timer->_next_id = 2;
    }
    timer->num++;

    tnode->heap_node = swHeap_push(timer->heap, tnode->exec_msec, tnode);
    if (tnode->heap_node == NULL)
    {
        sw_free(tnode);
        return NULL;
    }
    swTrace("id=%ld, exec_msec=%" PRId64 ", msec=%d, round=%" PRIu64, tnode->id, tnode->exec_msec, _msec, tnode->round);
    swHashMap_add_int(timer->map, tnode->id, tnode);
    return tnode;
}
Exemplo n.º 18
0
SWINLINE swBuffer_trunk* swConnection_get_in_buffer(swConnection *conn)
{
	swBuffer_trunk *trunk = NULL;
	swBuffer *buffer;

	if (conn->in_buffer == NULL)
	{
		buffer = swBuffer_new(SW_BUFFER_SIZE);
		//buffer create failed
		if (buffer == NULL)
		{
			return NULL;
		}
		//new trunk
		trunk = swBuffer_new_trunk(buffer, SW_TRUNK_DATA, buffer->trunk_size);
		if (trunk == NULL)
		{
			sw_free(buffer);
			return NULL;
		}
		conn->in_buffer = buffer;
	}
	else
	{
		buffer = conn->in_buffer;
		trunk = buffer->tail;
		if (trunk == NULL || trunk->length == buffer->trunk_size)
		{
			trunk = swBuffer_new_trunk(buffer, SW_TRUNK_DATA, buffer->trunk_size);
		}
	}
	return trunk;
}
Exemplo n.º 19
0
static void aio_onReadFileCompleted(swAio_event *event)
{
    zval *retval = NULL;
    zval *result = NULL;

    SW_MAKE_STD_ZVAL(result);
    if (event->ret < 0)
    {
        SwooleG.error = event->error;
        ZVAL_BOOL(result, 0);
    }
    else
    {
        ZVAL_STRINGL(result, event->buf, event->ret);
        sw_free(event->buf);
    }

    php_context *context = (php_context *) event->object;
    int ret = coro_resume(context, result, &retval);
    if (ret == CORO_END && retval)
    {
        sw_zval_ptr_dtor(&retval);
    }
    sw_zval_ptr_dtor(&result);
    efree(event->req);
    efree(context);
}
Exemplo n.º 20
0
int swServer_reactor_thread_onWrite(swReactor *reactor, swDataHead *ev)
{
	swServer *serv = SwooleG.serv;
	swConnection *conn = swServer_get_connection(serv, ev->fd);
	int ret, sendn;
	if (conn->output.send_file != NULL)
	{
		swTask_sendfile *task = conn->output.send_file;
		sendn = (task->filesize - task->offset > SW_SENDFILE_TRUNK) ? SW_SENDFILE_TRUNK : task->filesize - task->offset;
		ret = sendfile(ev->fd, task->fd, &task->offset, sendn);

		//swWarn("ret=%d|task->offset=%ld|sendn=%d|filesize=%ld", ret, task->offset, sendn, task->filesize);
		if (ret < 0)
		{
			swWarn("sendfile failed. Error: %s[%d]", strerror(errno), errno);
			return SW_ERR;
		}
		if (task->offset >= task->filesize)
		{
			reactor->set(reactor, ev->fd, SW_EVENT_TCP | SW_EVENT_READ);
			conn->output.send_file = NULL;
			close(task->fd);
			sw_free(task);
		}
	}
	return SW_OK;
}
Exemplo n.º 21
0
/**
 * Main Loop
 */
static int swServer_poll_loop(swThreadParam *param)
{
	swServer *serv = param->object;
	int ret, pti = param->pti;
	swReactor *reactor = &(serv->threads[pti].reactor);
	struct timeval timeo;

	ret = swReactorEpoll_create(reactor, (serv->max_conn / serv->poll_thread_num) + 1);
	if (ret < 0)
	{
		return SW_ERR;
	}
	timeo.tv_sec = serv->timeout_sec;
	timeo.tv_usec = serv->timeout_usec; //300ms
	reactor->ptr = serv;
	reactor->setHandle(reactor, SW_FD_CLOSE, swServer_poll_onClose);
	//Thread mode must copy the data.
	//will free after onFinish
	if(serv->factory_mode == SW_MODE_THREAD)
	{
		reactor->setHandle(reactor, SW_FD_CONN, swServer_poll_onReceive2);
	}
	else
	{
		reactor->setHandle(reactor, SW_FD_CONN, swServer_poll_onReceive);
	}
	//main loop
	reactor->wait(reactor, &timeo);
	//shutdown
	reactor->free(reactor);
	sw_free(param);
	return SW_OK;
}
Exemplo n.º 22
0
static int swServer_poll_onReceive2(swReactor *reactor, swEvent *event)
{
	int ret;
	swServer *serv = reactor->ptr;
	swFactory *factory = &(serv->factory);
	swEventData *buf = sw_malloc(sizeof(swEventData));
	if(buf==NULL)
	{
		swTrace("Malloc fail\n");
		return SW_ERR;
	}
	bzero(buf->data, sizeof(buf->data));
	ret = swRead(event->fd, buf->data, SW_BUFFER_SIZE);
	if (ret < 0)
	{
		swTrace("Receive Error.Fd=%d.From=%d\n", event->fd, event->from_id);
		return SW_ERR;
	}
	else if (ret == 0)
	{
		swTrace("Close Event.FD=%d|From=%d\n", event->fd, event->from_id);
		sw_free(buf);
		return swServer_close(serv, event);
	}
	else
	{
		buf->fd = event->fd;
		buf->len = ret;
		buf->from_id = event->from_id;
		//swTrace("recv: %s|fd=%d|ret=%d|errno=%d\n", buf->data, event->fd, ret, errno);
		factory->dispatch(factory, buf);
	}
	return SW_OK;
}
Exemplo n.º 23
0
static void swTable_compress_list(swTable *table)
{
    table->lock.lock(&table->lock);

    swTableRow **tmp = sw_malloc(sizeof(swTableRow *) * table->size);
    if (!tmp)
    {
        swWarn("malloc() failed, cannot compress the jump table.");
        goto unlock;
    }

    int i, tmp_i = 0;
    for (i = 0; i < table->list_n; i++)
    {
        if (table->rows_list[i] != NULL)
        {
            tmp[tmp_i] = table->rows_list[i];
            tmp[tmp_i]->list_index = tmp_i;
            tmp_i++;
        }
    }

    memcpy(table->rows_list, tmp, sizeof(swTableRow *) * tmp_i);
    sw_free(tmp);
    table->list_n = tmp_i;

    unlock: table->lock.unlock(&table->lock);
}
Exemplo n.º 24
0
void swClient_free(swClient *cli)
{
    assert(cli->socket->fd != 0);
    //remove from reactor
    if (!cli->socket->closed)
    {
        cli->close(cli);
    }
    if (cli->socket->out_buffer)
    {
        swBuffer_free(cli->socket->out_buffer);
        cli->socket->out_buffer = NULL;
    }
    if (cli->socket->in_buffer)
    {
        swBuffer_free(cli->socket->in_buffer);
        cli->socket->in_buffer = NULL;
    }
    bzero(cli->socket, sizeof(swConnection));
    if (cli->async)
    {
        cli->socket->removed = 1;
    }
    else
    {
        sw_free(cli->socket);
    }
}
Exemplo n.º 25
0
static int swPipeEventfd_close(swPipe *p)
{
	int ret;
	ret = close(((swPipeEventfd *)(p->object))->event_fd);
	sw_free(p->object);
	return ret;
}
Exemplo n.º 26
0
static int swFactoryThread_onTask(swThreadPool *pool, void *data, int len)
{
    swFactory *factory = pool->ptr2;
    int ret = factory->onTask(factory, (swEventData*) data);
    sw_free(data);
    return ret;
}
Exemplo n.º 27
0
static void swProcessPool_free(swProcessPool *pool)
{
    int i;
    swPipe *_pipe;

    if (!pool->use_msgqueue)
    {
        for (i = 0; i < pool->worker_num; i++)
        {
            _pipe = &pool->pipes[i];
            _pipe->close(_pipe);
        }
        sw_free(pool->pipes);
    }
    else if (pool->msgqueue_key == 0)
    {
        pool->queue->remove = 1;
        swMsgQueue_free(pool->queue);
    }

    if (pool->map)
    {
        swHashMap_free(pool->map);
    }
}
Exemplo n.º 28
0
swTimeWheel* swTimeWheel_new(uint16_t size)
{
    swTimeWheel *tw = sw_malloc(sizeof(swTimeWheel));
    if (!tw)
    {
        swWarn("malloc(%ld) failed.", sizeof(swTimeWheel));
        return NULL;
    }

    tw->size = size;
    tw->current = 0;
    tw->wheel = sw_calloc(size, sizeof(void*));
    if (tw->wheel == NULL)
    {
        swWarn("malloc(%ld) failed.", sizeof(void*) * size);
        sw_free(tw);
        return NULL;
    }

    int i;
    for (i = 0; i < size; i++)
    {
        tw->wheel[i] = swHashMap_new(16, NULL);
        if (tw->wheel[i] == NULL)
        {
            swTimeWheel_free(tw);
            return NULL;
        }
    }
    return tw;
}
Exemplo n.º 29
0
int swWorker_create(swWorker *worker)
{
	/**
	 * Create shared memory storage
	 */
	void *store = sw_shm_malloc(SwooleG.serv->buffer_output_size);
	if (store == NULL)
	{
		swWarn("malloc for worker->store failed.");
		return SW_ERR;
	}

	swPipe *worker_notify = sw_malloc(sizeof(swPipe));
	if (worker_notify == NULL)
	{
		swWarn("malloc for worker->notify failed.");
		sw_shm_free(store);
		return SW_ERR;
	}

	/**
	 * Create notify pipe
	 */
	if (swPipeNotify_auto(worker_notify, 1, 0))
	{
		sw_shm_free(store);
		sw_free(worker_notify);
		return SW_ERR;
	}

	worker->notify = worker_notify;
	worker->store.ptr = store;
	worker->store.lock = 0;
	return SW_OK;
}
Exemplo n.º 30
0
void swPipeUnsock_close(swPipe *p)
{
	swPipeUnsock *this = p->object;
	close(this->socks[0]);
	close(this->socks[1]);
	sw_free(this);
}