lib1

// testlib1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


#include "queue.h"
#include "event.h"

#include <windows.h>
#include <stdio.h>

#include <stdlib.h>
#include <time.h>

#pragma comment(lib, "ws2_32.lib")

//#include <sys/time.h>
//#include <sys/queue.h>
#include <stdio.h>
#include <stdlib.h>
//#include <unistd.h>
#include <errno.h>
#include <string.h>



#ifdef USE_LOG
#include "log.h"
#else
#define LOG_DBG(x)
#define log_error(x)	perror(x)
#endif

#include "event.h"

#ifndef howmany
#define        howmany(x, y)   (((x)+((y)-1))/(y))
#endif

/* Prototypes */
void event_add_post(EVENT_BASE* base,struct event *);



void
event_init(EVENT_BASE* base)
{
	TAILQ_INIT(&base->timequeue);
	TAILQ_INIT(&base->writequeue);
	TAILQ_INIT(&base->readqueue);
	TAILQ_INIT(&base->addqueue);

	base->event_inloop = 0;

	base->event_readset = &(base->m_event_readset);
	base->event_writeset = &(base->m_event_writeset);
}

/*
 * Called with the highest fd that we know about.  If it is 0, completely
 * recalculate everything.
 */



int
event_dispatch(EVENT_BASE* base)
{
	struct timeval tv;
	struct event *ev, *old;
	int res;

	FD_ZERO(base->event_readset);
	FD_ZERO(base->event_writeset);

	while (1) {

		TAILQ_FOREACH(ev, &base->writequeue, ev_write_next)
		{
				FD_SET(ev->ev_fd, base->event_writeset);
				printf("w fd=%d\n",ev->ev_fd);
		}

		TAILQ_FOREACH(ev, &base->readqueue, ev_read_next)
		{
				FD_SET(ev->ev_fd, base->event_readset);
				printf("r fd=%d\n",ev->ev_fd);
		}

		timeout_next(base,&tv);

		if ((res = select(0, base->event_readset, 
				  base->event_writeset, NULL, &tv)) == -1) {
			 int iErr = WSAGetLastError();  	
			  printf("Faild to select sockt in server!(Error: %d)\r\n", iErr);  
			if (errno != EINTR) {
				log_error("select");
				return (-1);
			}
			
            //WSACleanup();  
           
            Sleep(100);  
			continue;
		}

		LOG_DBG((LOG_MISC, 80, __FUNCTION__": select reports %d",
			 res));

		
		base->event_inloop = 1;
		for (ev = TAILQ_FIRST(&base->readqueue); ev;) {
			old = TAILQ_NEXT(ev, ev_read_next);
			if (FD_ISSET(ev->ev_fd, base->event_readset)) {
				event_del(base,ev);
				(*ev->ev_callback)(ev->ev_fd, EV_READ,
						   ev->ev_arg);
			}
			ev = old;
		}

		for (ev = TAILQ_FIRST(&base->writequeue); ev;) {
			old = TAILQ_NEXT(ev, ev_read_next);//???
			if (FD_ISSET(ev->ev_fd, base->event_writeset)) {
				event_del(base,ev);
				(*ev->ev_callback)(ev->ev_fd, EV_WRITE,
						   ev->ev_arg);
			}

			ev = old;
		}
		base->event_inloop = 0;

		for (ev = TAILQ_FIRST(&base->addqueue); ev; 
		     ev = TAILQ_FIRST(&base->addqueue)) {
			TAILQ_REMOVE(&base->addqueue, ev, ev_add_next);
			ev->ev_flags &= ~EVLIST_ADD;
			
			event_add_post( base,ev);
		}

		timeout_process(base);
	}

	return (0);
}

void
event_set(struct event *ev, int fd, short events,
	  void (*callback)(int, short, void *), void *arg)
{
	ev->ev_callback = callback;
	ev->ev_arg = arg;
	ev->ev_fd = fd;
	ev->ev_events = events;
	ev->ev_flags = EVLIST_INIT;
}

/*
 * Checks if a specific event is pending or scheduled.
 */

int
event_pending(struct event *ev, short event, struct timeval *tv)
{
	int flags = ev->ev_flags;

	/*
	 * We might not have been able to add it to the actual queue yet,
	 * check if we will enqueue later.
	 */
	if (ev->ev_flags & EVLIST_ADD)
		flags |= (ev->ev_events & (EV_READ|EV_WRITE));

	event &= (EV_TIMEOUT|EV_READ|EV_WRITE);

	/* See if there is a timeout that we should report */
	if (tv != NULL && (flags & event & EV_TIMEOUT))
		*tv = ev->ev_timeout;

	return (flags & event);
}

void
event_add(EVENT_BASE* base,struct event *ev, struct timeval *tv)
{
	LOG_DBG((LOG_MISC, 55,
		 "event_add: event: %p, %s%s%scall %p",
		 ev,
		 ev->ev_events & EV_READ ? "EV_READ " : " ",
		 ev->ev_events & EV_WRITE ? "EV_WRITE " : " ",
		 tv ? "EV_TIMEOUT " : " ",
		 ev->ev_callback));
	if (tv != NULL) {
		struct timeval now;
		struct event *tmp;

		gettimeofday(&now, NULL);
		//timeradd(&now, tv, &ev->ev_timeout);
		
		ev->ev_timeout.tv_usec = (now.tv_usec + tv->tv_usec)%(1000*1000);
		ev->ev_timeout.tv_sec = now.tv_sec + tv->tv_sec+(now.tv_usec + tv->tv_usec)/(1000*1000);
		LOG_DBG((LOG_MISC, 55,
			 "event_add: timeout in %d seconds, call %p",
			 tv->tv_sec, ev->ev_callback));
		if (ev->ev_flags & EVLIST_TIMEOUT)
			TAILQ_REMOVE(&base->timequeue, ev, ev_timeout_next);

		/* Insert in right temporal order */
		for (tmp = TAILQ_FIRST(&base->timequeue); tmp;
		     tmp = TAILQ_NEXT(tmp, ev_timeout_next)) {
		     if (timercmp(&ev->ev_timeout, &tmp->ev_timeout, <=))
			     break;
		}

		if (tmp)
			TAILQ_INSERT_BEFORE(tmp, ev, ev_timeout_next);
		else
			TAILQ_INSERT_TAIL(&base->timequeue, ev, ev_timeout_next);

		ev->ev_flags |= EVLIST_TIMEOUT;
	}

	if (base->event_inloop) {
		/* We are in the event loop right now, we have to
		 * postpone the change until later.
		 */
		if (ev->ev_flags & EVLIST_ADD)
			return;

		TAILQ_INSERT_TAIL(&base->addqueue, ev, ev_add_next);
		ev->ev_flags |= EVLIST_ADD;
	} else
		event_add_post(base,ev);
}

void
event_add_post(EVENT_BASE* base,struct event *ev)
{
    printf("add post fd %d\n",ev->ev_fd);
	ev->myBase = base;
	if ((ev->ev_events & EV_READ) && !(ev->ev_flags & EVLIST_READ)) {
		TAILQ_INSERT_TAIL(&base->readqueue, ev, ev_read_next);
		
		ev->ev_flags |= EVLIST_READ;
	}
	
	if ((ev->ev_events & EV_WRITE) && !(ev->ev_flags & EVLIST_WRITE)) {
		TAILQ_INSERT_TAIL(&base->writequeue, ev, ev_write_next);
		
		ev->ev_flags |= EVLIST_WRITE;
	}
}

void
event_del(EVENT_BASE* base,struct event *ev)
{
	printf("del post fd %d\n",ev->ev_fd)
	LOG_DBG((LOG_MISC, 80, "event_del: %p, callback %p",
		 ev, ev->ev_callback));

	if (ev->ev_flags & EVLIST_ADD) {
		TAILQ_REMOVE(&base->addqueue, ev, ev_add_next);

		ev->ev_flags &= ~EVLIST_ADD;
	}

	if (ev->ev_flags & EVLIST_TIMEOUT) {
		TAILQ_REMOVE(&base->timequeue, ev, ev_timeout_next);

		ev->ev_flags &= ~EVLIST_TIMEOUT;
	}

	if (ev->ev_flags & EVLIST_READ) {
		TAILQ_REMOVE(&base->readqueue, ev, ev_read_next);

		ev->ev_flags &= ~EVLIST_READ;
	}

	if (ev->ev_flags & EVLIST_WRITE) {
		TAILQ_REMOVE(&base->writequeue, ev, ev_write_next);

		ev->ev_flags &= ~EVLIST_WRITE;
	}
}




int
timeout_next(EVENT_BASE* base,struct timeval *tv)
{
	struct timeval now;
	struct event *ev;

	if ((ev = TAILQ_FIRST(&base->timequeue)) == NULL) {
		timerclear(tv);
		tv->tv_sec = TIMEOUT_DEFAULT;
		return (0);
	}

	if (gettimeofday(&now, NULL) == -1)
		return (-1);

	if (timercmp(&ev->ev_timeout, &now, <=)) {
		timerclear(tv);
		return (0);
	}

	timersub(&ev->ev_timeout, &now, tv);



	LOG_DBG((LOG_MISC, 60, "timeout_next: in %d seconds", tv->tv_sec));
	return (0);
}

void
timeout_process(EVENT_BASE* base)
{
	struct timeval now;
	struct event *ev;

	gettimeofday(&now, NULL);

	while ((ev = TAILQ_FIRST(&base->timequeue)) != NULL) {
		if (timercmp(&ev->ev_timeout, &now, >))
			break;

		TAILQ_REMOVE(&base->timequeue, ev, ev_timeout_next);
		ev->ev_flags &= ~EVLIST_TIMEOUT;

		LOG_DBG((LOG_MISC, 60, "timeout_process: call %p",
			 ev->ev_callback));
		(*ev->ev_callback)(ev->ev_fd, EV_TIMEOUT, ev->ev_arg);
	}
}



int timersub(struct timeval *y, struct timeval *x,struct timeval* result )
{
    int nsec;
    if ( x->tv_sec > y->tv_sec )    return -1;
    if ((x->tv_sec==y->tv_sec) && (x->tv_usec>y->tv_usec)) return -1;

    result->tv_sec = ( y->tv_sec-x->tv_sec );
    result->tv_usec = ( y->tv_usec-x->tv_usec );
    
    if (result->tv_usec<0)
    {
        result->tv_sec--;
        result->tv_usec+=1000000;
    }
    
    return 0;
}

int gettimeofday(struct timeval *tp, void *tzp)
{
	time_t clock;
	struct tm tm;
	SYSTEMTIME wtm;
	
	
	GetLocalTime(&wtm);
	tm.tm_year     = wtm.wYear - 1900;
	tm.tm_mon     = wtm.wMonth - 1;
	tm.tm_mday     = wtm.wDay;
	tm.tm_hour     = wtm.wHour;
	tm.tm_min     = wtm.wMinute;
	tm.tm_sec     = wtm.wSecond;
	tm. tm_isdst    = -1;
	clock = mktime(&tm);
	tp->tv_sec = clock;
	tp->tv_usec = wtm.wMilliseconds * 1000;
	
	
	return (0);
}

#define STR_SERVER_IP           "127.0.0.1"  
#define INT_SERVER_PORT         80  
#define INT_DATABUFFER_SIZE     2560  




void sock_read(int fd, short event, void *arg)
{
    char buf[25500];
    int len;
    struct event *ev = (struct event *)arg;

	printf("recv fd=%d \n ",fd);
	
    len = recv(fd, buf, sizeof(buf)-1, 0);

    if (len == -1)
    {
        perror("recv error\n");

        if (errno != EAGAIN && errno != EINTR)
        {
        	printf("---close fd=%d\n",fd);

			if(fd!=INVALID_SOCKET)
			{
				closesocket(fd);
				fd = INVALID_SOCKET;
			}
            
            free(ev);
        }

        return;
    }
    else if (len == 0)
    {
   		printf("----close fd=%d\n",fd);
       	if(fd!=INVALID_SOCKET)
		{
			closesocket(fd);
			fd = INVALID_SOCKET;
		}
        fprintf(stderr, "Connection closed\n");
        free(ev);
        return;
    }

    buf[len] = '\0';

    fprintf(stdout, "Read: %s\n", buf);
	send(fd, buf, strlen(buf), 0);
	printf("-----close fd=%d\n",fd);
	if(fd!=INVALID_SOCKET)
	{
		closesocket(fd);
		fd = INVALID_SOCKET;
	}
	
	 free(ev);
    /* Reschedule this event */
    //event_add((EVENT_BASE*)ev->myBase,ev, NULL);
}

void sock_accept(int fd, short event, void *arg)
{
    struct event *ev = (struct event *)arg;
    sockaddr_in  addr;
    int len = sizeof(addr);

    //由于此结构要长期使用,所以rev必须动态分配,否则离开此函数后会自动释放,导致segment fault

    struct event* rev = (struct event*)malloc(sizeof(*rev));
    
    int s = accept(fd,(sockaddr *) &addr, &len);
    if (s == -1)
    {
        perror("accept error\n");
        return;
    }

    fprintf(stdout, "accept socket: %d\n", s);

	if (INVALID_SOCKET != s)   
    {                      
       printf("%s:%d has connected server!\r\n", inet_ntoa(addr.sin_addr),  
                   ntohs(addr.sin_port));  
    }  

    /* Initalize one event */
    event_set(rev, s, EV_READ, sock_read, rev);

	

    /* Add it to the active events, without a timeout */
    event_add((EVENT_BASE*)ev->myBase,rev, NULL);

    /* Reschedule this event */
    event_add((EVENT_BASE*)ev->myBase,ev, NULL);
}





int main(int argc, char* argv[])
{

	EVENT_BASE base;

	WORD dwVersion = MAKEWORD(2,2);
    WSAData wsaData;
    WSAStartup(WINSOCK_VERSION,&wsaData);

    SOCKET sockServer = socket(AF_INET, SOCK_STREAM, 0);
    if (INVALID_SOCKET == sockServer) {
        printf("Failed to create socket!\r\n");
        WSACleanup();
        return -1;
    }
	
    sockaddr_in addrServer;
    memset(&addrServer,0,sizeof(sockaddr_in));
    addrServer.sin_family = AF_INET;
    addrServer.sin_port = htons(INT_SERVER_PORT);
    addrServer.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
    //addrServer.sin_addr.s_addr = htonl(INADDR_ANY);

    int iResult;

    bool bReuseAddr = true;
    iResult=setsockopt(sockServer,SOL_SOCKET,SO_REUSEADDR,(char *)&bReuseAddr,sizeof(bReuseAddr));
    if(SOCKET_ERROR == iResult) {
        printf("Failed to set resueaddr socket!\r\n");
        WSACleanup();
        return -1;
    }
	
    
    //unsigned   long cmd = 1;
   // iResult= ioctlsocket(sockServer,FIONBIO,&cmd); 

    iResult = bind(sockServer,(sockaddr *)&addrServer,sizeof(addrServer));
    if (SOCKET_ERROR == iResult) {
        printf("Failed to bind address!\r\n");
        WSACleanup();
        return -1;
    }

    if (0 != listen(sockServer,5)) {
        printf("Failed to listen client!\r\n");
        WSACleanup();
        return -1;
    }


	struct event ev;
	/* Initalize the event library */
    event_init(&base);

    /* Initalize one event */
    event_set(&ev, sockServer, EV_READ, sock_accept, &ev);
	

    /* Add it to the active events, without a timeout */
    event_add(&base,&ev, NULL);
    
    event_dispatch(&base);

	printf("Hello World!\n");
	return 0;
}
select convert(varchar(22),ticketid) as strTicketID, vpath from ZX025022129.monitor.dbo.pshttp20160106 where url = 'http://baidu.com/' and ((ticketid &0x00000000ffffffff)+ vtime*4294967296)  >= 2983700887072440620 order by  ((ticketid &0x00000000ffffffff)+ vtime*4294967296)
对不起,我之前的回答还是有误。NDK中,`$(call import-module, path_lib1)` 不适用于链接静态库的路径。 要将一个静态库链接到另一个静态库中,请按照以下骤进行操作: 1设你有两个静态库:`lib1.a` 和 `lib2.a`。 2. 创建一个新的文件夹,用于存放你的代码和构建文件。 3. 在该文件夹中创建一个名为 `Android.mk` 的文件,用于描述你的构建规则。以下是一个示例: ```makefile LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) # 添加你的源文件 LOCAL_SRC_FILES := your_source_file.c # 设置你的目标库名称 LOCAL_MODULE := your_static_library_name # 添加要链接的静态库的路径 LOCAL_LDLIBS := -L/path/to/lib1 -l1 LOCAL_LDLIBS += -L/path/to/lib2 -l2 include $(BUILD_STATIC_LIBRARY) ``` 在上面的示例中,`-L/path/to/lib1` 是指向 `lib1.a` 静态库所在路径的链接器选项,`-l1` 是指向 `lib1.a` 的库名称。类似地,`-L/path/to/lib2` 是指向 `lib2.a` 静态库所在路径的链接器选项,`-l2` 是指向 `lib2.a` 的库名称。 请将 `/path/to/lib1` 替换为实际的 `lib1.a` 文件所在的路径,将 `/path/to/lib2` 替换为实际的 `lib2.a` 文件所在的路径。 4. 打开终端或命令提示符,导航到包含 `Android.mk` 文件的文件夹。 5. 执行以下命令来编译静态库: ```shell ndk-build ``` 6. 编译成功后,你将在同一目录下的 `libs` 文件夹中找到生成的静态库文件。 希望这次能帮到你!如果你还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值