您的位置:首页 > 其它

lib1

2015-12-07 00:40 369 查看
// 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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: