Пример #1
0
/**
 * Main function for this unit test
 *
 *
 * @return 0 if test passed
 */
int main()
{
	t_buffer *buffer;
	t_buffer buffer2;

	buffer = buffer_create(NULL);
	XTEST(buffer != NULL);
	XTEST(buffer_add(buffer, "123456789", 9) == 9);
	check_buf(buffer, 123456789);
	buffer_erase(buffer, buffer_size(buffer));
	XTEST(buffer_add(buffer, "1", 1) == 1);
	check_buf(buffer, 1);
	buffer_erase(buffer, buffer_size(buffer));
	XTEST(buffer_add(buffer, "-12345678", 9) == 9);
	check_buf(buffer, -12345678);
	buffer_erase(buffer, buffer_size(buffer));
	buffer_destroy(buffer);
	strtobuffer(&buffer2, "987654321");
	check_buf(&buffer2, 987654321);
	strtobuffer(&buffer2, "0");
	check_buf(&buffer2, 0);
	strtobuffer(&buffer2, "-987654321");
	check_buf(&buffer2, -987654321);
	XPASS();
}
Пример #2
0
// 会话停止(删除网络事件以及关闭描述符)
void _stop( struct session * self )
{
    evsets_t sets = self->evsets;

    // 删除网络事件
    if ( self->status&SESSION_READING )
    {
        evsets_del( sets, self->evread );
        self->status &= ~SESSION_READING;
    }
    if ( self->status&SESSION_WRITING )
    {
        evsets_del( sets, self->evwrite );
        self->status &= ~SESSION_WRITING;
    }
    if ( self->status&SESSION_KEEPALIVING )
    {
        evsets_del( sets, self->evkeepalive );
        self->status &= ~SESSION_KEEPALIVING;
    }

    // 清空接收缓冲区
    buffer_erase( &self->inbuffer, buffer_length(&self->inbuffer) );

    // 关闭描述符
    if ( self->fd > 0 )
    {
        close( self->fd );
        self->fd = -1;
    }
}
Пример #3
0
uint32_t buffer_take( struct buffer * self, char * buf, uint32_t length )
{
    length = ( length > self->length ? self->length : length );

    memcpy( buf, self->buffer, length );
    buffer_erase( self, length );

    return length;
}
Пример #4
0
int string_erase(string *s, size_t pos, size_t size)
{
  size_t l = string_length(s);

  if (pos > l)
    return -1;

  buffer_erase(&s->buffer, pos, size > l - pos ? l - pos : size);

  return 0;
}
Пример #5
0
/*
 * Returns a message to the client ; the connection is shut down for read,
 * and the request is cleared so that no server connection can be initiated.
 * The buffer is marked for read shutdown on the other side to protect the
 * message, and the buffer write is enabled. The message is contained in a
 * "chunk". If it is null, then an empty message is used. The reply buffer does
 * not need to be empty before this, and its contents will not be overwritten.
 * The primary goal of this function is to return error messages to a client.
 */
void stream_int_retnclose(struct stream_interface *si, const struct chunk *msg)
{
	buffer_auto_read(si->ib);
	buffer_abort(si->ib);
	buffer_auto_close(si->ib);
	buffer_erase(si->ib);
	if (likely(msg && msg->len))
		buffer_write(si->ob, msg->str, msg->len);

	si->ob->wex = tick_add_ifset(now_ms, si->ob->wto);
	buffer_auto_read(si->ob);
	buffer_auto_close(si->ob);
	buffer_shutr_now(si->ob);
}
Пример #6
0
/**
 * Main function for this unit test
 *
 *
 * @return 0 if test passed
 */
int main()
{
	uint32_t i;
	t_buffer *buffer1;

	buffer1 = buffer_create(NULL);
	XTEST(buffer1 != NULL);
	for (i = 0; i < ARRAY_SIZE(strings) - 1; i++) {
		buffer_add(buffer1, strings[i], strlen(strings[i]));
		XTEST(buffer_strcasecmp(buffer1, strings[i + 1]) < 0);
		buffer_erase(buffer1, buffer_size(buffer1));
	}
	buffer_destroy(buffer1);
	XPASS();
}
Пример #7
0
int rinoo_fs_browse(const char *path, t_fs_entry **last_entry)
{
	DIR *dirfd;
	t_fs_entry *curentry;
	struct dirent *result;
	t_fs_directory *directory;

	if (last_entry == NULL) {
		return -1;
	}
	curentry = *last_entry;
	if (curentry == NULL) {
		curentry = rinoo_fs_entry(path);
		if (curentry == NULL) {
			return -1;
		}
		dirfd = opendir(path);
		if (dirfd == NULL) {
			goto browse_error;
		}
		if (rinoo_fs_stack_push(curentry, dirfd, path) != 0) {
			closedir(dirfd);
			goto browse_error;
		}
	}
	directory = rinoo_fs_stack_head(curentry);
	if (directory == NULL) {
		goto browse_error;
	}
	for (result = NULL; result == NULL && directory != NULL;) {
		if (readdir_r(directory->fd, curentry->entry, &result) != 0) {
			goto browse_error;
		}
		if (result == NULL) {
			closedir(directory->fd);
			rinoo_fs_stack_pop(curentry);
			directory = rinoo_fs_stack_head(curentry);
			result = NULL;
			continue;
		}
		if (curentry->entry->d_name[0] == '.' && (curentry->entry->d_name[1] == 0 || curentry->entry->d_name[1] == '.')) {
			result = NULL;
			continue;
		}
		buffer_erase(curentry->path, 0);
		buffer_addstr(curentry->path, directory->path);
		if (((char *) buffer_ptr(curentry->path))[curentry->path->size - 1] != '/') {
			buffer_addstr(curentry->path, "/");
		}
		buffer_addstr(curentry->path, curentry->entry->d_name);
		buffer_addnull(curentry->path);
		if (stat(buffer_ptr(curentry->path), &curentry->stat) != 0) {
			/* Try next entry */
			result = NULL;
			continue;
		}
		if (S_ISDIR(curentry->stat.st_mode)) {
			dirfd = opendir(buffer_ptr(curentry->path));
			if (dirfd == NULL) {
				/* Try next entry */
				result = NULL;
				continue;
			}
			if (rinoo_fs_stack_push(curentry, dirfd, buffer_ptr(curentry->path)) != 0) {
				goto browse_error;
			}
		}
	}
	if (result == NULL && directory == NULL) {
		/* End of browsing */
		rinoo_fs_entry_destroy(curentry);
		*last_entry = NULL;
		return 0;
	}
	*last_entry = curentry;
	return 0;
browse_error:
	if (curentry != NULL) {
		rinoo_fs_entry_destroy(curentry);
	}
	return -1;
}