Пример #1
0
int main(int argc, char *argv[])
{
	char  peer[64], local[64];
	int   ch, count = 1, dlen = 100, inter = 1000, nthreads = 1, quit = 0;
	int   need_read = 0;

	acl_lib_init();
	acl_msg_stdout_enable(1);

	snprintf(peer, sizeof(peer), "127.0.0.1:8888");
	snprintf(local, sizeof(local), "127.0.0.1:0");

	while ((ch = getopt(argc, argv, "hl:s:n:N:i:t:rq")) > 0) {
		switch (ch) {
		case 'h':
			usage(argv[0]);
			return 0;
		case 'l':
			snprintf(local, sizeof(local), "%s", optarg);
			break;
		case 's':
			snprintf(peer, sizeof(peer), "%s", optarg);
			break;
		case 'n':
			count = atoi(optarg);
			break;
		case 'N':
			dlen = atoi(optarg);
			break;
		case 'i':
			inter = atoi(optarg);
			break;
		case 't':
			nthreads = atoi(optarg);
			break;
		case 'r':
			need_read = 1;
			break;
		case 'q':
			quit = 1;
			break;
		default:
			break;
		}
	}

	if (peer[0] == 0 || local[0] == 0) {
		usage(argv[0]);
		return 1;
	}

	if (nthreads > 1) {
		int   i;
		acl_pthread_pool_t *threads =
			acl_thread_pool_create(nthreads, 120);
		for (i = 0; i < nthreads; i++) {
			THREAD_CTX *ctx = (THREAD_CTX*)
				acl_mymalloc(sizeof(THREAD_CTX));
			snprintf(ctx->local, sizeof(ctx->local), "%s", local);
			snprintf(ctx->peer, sizeof(ctx->peer), "%s", peer);
			ctx->count = count;
			ctx->dlen = dlen;
			ctx->inter = inter;
			ctx->need_read = need_read;
			ctx->quit = quit;
			acl_pthread_pool_add(threads, thread_run, ctx);
		}

		acl_pthread_pool_destroy(threads);
	} else
		run(local, peer, count, dlen, inter, need_read, quit);

	printf("\r\nlocal: %s, peer: %s, count: %d, dlen: %d, inter: %d\r\n",
		local, peer, count, dlen, inter);

	acl_lib_end();

	return 0;
}
Пример #2
0
static int test_vstream(void)
{
	const char *filename = "test.txt";
	ACL_VSTREAM *fp = acl_vstream_fopen(filename, O_RDWR | O_CREAT, 0644, 1024);
	struct acl_stat sbuf;
	char  buf[256];
	struct tm *local_time;

	acl_lib_init();

	if (fp == NULL) {
		printf("open file(%s) error(%s)\r\n", filename, acl_last_serror());
		end();
		return (-1);
	}

	if (acl_vstream_fstat(fp, &sbuf) < 0) {
		acl_vstream_close(fp);
		printf("fstat file(%s) error(%s)\r\n", filename, acl_last_serror());
		end();
		return (-1);
	}

	printf("file(%s) stat:\r\n", filename);

	printf("size=%d\r\n", (int) sbuf.st_size);

	local_time = localtime(&sbuf.st_mtime);
	if (local_time) {
		strftime(buf, sizeof(buf), "%Y/%m/%d %H:%M:%S", local_time);
		printf("修改时间,mtime=%s\r\n", buf);
	} else {
		printf("mtime: error(%s)\r\n", acl_last_serror());
	}

	local_time = localtime(&sbuf.st_ctime);
	if (local_time) {
		strftime(buf, sizeof(buf), "%Y/%m/%d %H:%M:%S", local_time);
		printf("创建时间,ctime=%s\r\n", buf);
	} else {
		printf("ctime: error(%s)\r\n", acl_last_serror());
	}

	local_time = localtime(&sbuf.st_atime);
	if (local_time) {
		strftime(buf, sizeof(buf), "%Y/%m/%d %H:%M:%S", local_time);
		printf("访问时间,atime=%s\r\n", buf);
	} else {
		printf("atime: error(%s)\r\n", acl_last_serror());
	}

	int  ch, ch1;

	ch = ACL_VSTREAM_GETC(fp);
	ch = 'a';

	for (int i = 0; i < 100; i++) {
		ch1 = ACL_VSTREAM_PUTC(ch, fp);
		assert(ch1 == ch);
		printf("ok write char: %c\n", ch1);
	}
	ACL_VSTREAM_PUTC('\n', fp);
	acl_vstream_fclose(fp);
	end();	

	FILE *fp1 = fopen("test.txt", "w+");
	assert(fp1);
	int ret = fputs("hello", fp1);
	printf("ret: %d\n", ret);
	fclose(fp1);
	return (0);
}
Пример #3
0
static void init(void)
{
    acl_lib_init();
}