예제 #1
0
void on_client(api_loop_t* loop, void* arg)
{
    api_tcp_t* client = (api_tcp_t*)arg;
    api_tcp_t server;
    proxy_t proxy;

    api_stream_attach(&client->stream, loop);

    client->stream.read_timeout = 10 * 1000;
    client->stream.write_timeout = 10 * 1000;

    if (API_OK == api_tcp_connect(&server, loop, backend_ip, backend_port, 10 * 1000))
    {
        server.stream.read_timeout = 10 * 1000;
        server.stream.write_timeout = 10 * 1000;

        api_event_init(&proxy.ready, loop);
        
        proxy.client = client;
        proxy.server = &server;

        // transfer request in parallel
        api_loop_post(loop, transfer_request, &proxy, 0);

        // transfer response
        api_stream_transfer(&client->stream, &server.stream, 100 * 1024, 0);

        api_event_wait(&proxy.ready, 0);

        api_stream_close(&server.stream);
    }

    api_stream_close(&client->stream);
    api_free(api_pool_default(loop), sizeof(api_tcp_t), client);
}
예제 #2
0
void* api_async_task_fn(api_task_t* task)
{
    api_async_t* async = (api_async_t*)task->data;
    async->callback(async->loop, async->arg);
    api_free(&async->loop->base.pool, sizeof(*async), async);

    return 0;
}
예제 #3
0
파일: cat.cpp 프로젝트: tangfuhao/30System
void HariMain(void)
{
	int file = api_fopen("license.txt");
	int size = api_fsize(file /* handle */, 0 /* mode */);
	char* buff = (char *) api_malloc(size + 1);
	api_fread(buff, size, file);
	buff[size] = '\0';
	api_putstr0(buff);
	api_fclose(file);
	api_free(buff);
	api_end();
}
예제 #4
0
void HariMain()
{
	int win, xsize = 400, ysize = 200;
	char win_buf[xsize * ysize];
	api_initmalloc();
	
	struct MOUSE_INFO *minfo;
	minfo = api_malloc(sizeof(struct MOUSE_INFO));
	minfo->flag = -589;
	int counter = 0;
	int index = 0;
	char cbuf[20];
	
	win = api_openwin((char *)win_buf, xsize, ysize, -1 /*ºÚÉ«*/, "minesweeper");
    api_refreshwin(win, 0, 0, xsize-1, ysize-1);

	char s[20];
	for(;;)
	{
		sprintf(cbuf, "%10d", counter);
		api_boxfilwin(win, 30, 30, 30 + strlen(cbuf) * 8, 30 + 16, 8/*ÁÁ»Ò*/);
		api_putstrwin(win, 30, 30, 0 /*ºÚÉ«*/, strlen(cbuf),  cbuf);
		
		api_getmouse(1, minfo);
			
		if(minfo->flag == 1)
		{	
			sprintf(s,"%5d %5d, [l m r] %d", minfo->x, minfo->y, minfo->btn);
			//sprintf(s,"%d", (int)minfo);
			if( (minfo->btn & 0x01) != 0 ){
				s[14] = 'L';
			}
			if( (minfo->btn & 0x02) != 0 ){
				s[18] = 'R';
			}
			if( (minfo->btn & 0x04) != 0 ){
				s[16] = 'M';
			}
			api_boxfilwin(win, 30, 50, 30 + 40 * 8, 50 + 16, 8/*ÁÁ»Ò*/);
			api_putstrwin(win, 30, 50, 0, strlen(s),  s);
			minfo->flag = -1;
		}
		counter++;
	}
	
	api_free(minfo, sizeof(struct MOUSE_INFO));
	api_closewin(win);
	api_end();
}
예제 #5
0
size_t api_stream_read(api_stream_t* stream, char* buffer, size_t length)
{
    size_t done = 0;

    if (length == 0)
        return length;

    if (stream->status.read_timeout ||
        stream->status.eof ||
        stream->status.error != API__OK ||
        stream->status.closed ||
        stream->status.peer_closed ||
        stream->status.terminated)
        return 0;

    if (stream->loop->base.terminated)
    {
        stream->status.terminated = 1;
        return 0;
    }

    if (stream->unread.length > 0)
    {
        if (stream->unread.length <= length)
        {
            done = stream->unread.length;
            memcpy(buffer, stream->unread.buffer + stream->unread.offset,
                stream->unread.length);
            api_free(api_pool_default(stream->loop), stream->unread.length,
                stream->unread.buffer);
            stream->unread.length = 0;
        }
        else
        {
            done = length;
            memcpy(buffer, stream->unread.buffer + stream->unread.offset,
                length);
            stream->unread.offset += length;
            stream->unread.length -= length;
        }

        return done;
    }

    return stream->filter_head->on_read(stream->filter_head, buffer, length);
}
예제 #6
0
void proxy_server(api_loop_t* loop, void* arg)
{
    api_pool_t* pool = api_pool_default(loop);
    api_tcp_listener_t listener;
    api_tcp_t* tcp;

    if (API_OK != api_tcp_listen(&listener, loop, listen_ip, listen_port, 128))
        return;

    tcp = (api_tcp_t*)api_alloc(pool, sizeof(api_tcp_t));
    while (API_OK == api_tcp_accept(&listener, tcp))
    {
        api_loop_post(loop, on_client, tcp, 50 * 1024);

        tcp = (api_tcp_t*)api_alloc(pool, sizeof(api_tcp_t));
    }
    api_free(pool, sizeof(api_tcp_t), tcp);

    api_tcp_close(&listener);
}
예제 #7
0
int api_stream_close(api_stream_t* stream)
{
    int error = 0;

    if (stream->type == STREAM_File)
    {
        stream->status.closed = 1;
        close(stream->fd);
    }
    else
    {
        error = epoll_ctl(stream->loop->epoll, EPOLL_CTL_DEL, stream->fd,
                        &stream->os_linux.e);
        if (error == 0)
        {
            stream->status.closed = 1;
            close(stream->fd);
        }
    }

    stream->filter_head->on_closed(stream->filter_head);

    if (stream->unread.length > 0) 
    {
        api_free(api_pool_default(stream->loop), stream->unread.length,
                    stream->unread.buffer);
        stream->unread.length = 0;
    }

    if (stream->loop != 0)
    {
        api_loop_unref(stream->loop);
        stream->loop = 0;
        return API__OK;
    }

    return api_error_translate(errno);
}
예제 #8
0
int api_async_wakeup(api_loop_t* loop, api_task_t* task)
{
    api_async_t* async = (api_async_t*)api_alloc(&loop->base.pool, sizeof(api_async_t));
    int error = 0;

    if (async == 0)
        return API__NO_MEMORY;

    async->loop = loop;
    async->arg = task;
    async->stack_size = 0;
    async->handler = api_async_wakeup_handler;

    if (!PostQueuedCompletionStatus(loop->iocp, sizeof(*async),
                (ULONG_PTR)&g_api_async_processor, (LPOVERLAPPED)async))
    {
        error = api_error_translate(GetLastError());
        api_free(&loop->base.pool, sizeof(api_async_t), async);
        return error;
    }

    return API__OK;
}