-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmyts.h
131 lines (116 loc) · 4.01 KB
/
myts.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Copyright (C) 2010 Luigi Rizzo, Universita' di Pisa
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* $Id: myts.h 7958 2010-12-04 01:15:04Z luigi $
This is a framework for event-based programming.
The entire state of the application is reachable through
a global "struct my_args _me".
Each sub-application is described by a "struct app" which
contains callbacks for boot, argument parsing, callbacks,
destructor, and private data.
*/
#ifndef _MYTS_H_
#define _MYTS_H_
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h> /* gettimeofday */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h> /* inet_aton */
extern int verbose;
#define DBG(level, format, ...) do { \
if (verbose >= level) { \
struct timeval now; gettimeofday(&now, NULL); \
fprintf(stderr, "%5d.%03d [%-14.14s %4d] " format, \
(int)(now.tv_sec %86400), (int)(now.tv_usec / 1000), \
__FUNCTION__, __LINE__, ##__VA_ARGS__); \
} } while(0)
/*
* descriptor of an application.
*/
struct app {
int (*init)(void);
int (*parse)(int *argc, char *argv[]);
int (*start)(void);
int (*end)(void);
void *data; /* pointer to private data */
};
/*
* callback in 'prepare' mode returns 1 if fd active.
* callback in 'run' mode returns 0 if ok, 1 if dying.
* destruction must be done in the callback itself
*/
struct cb_args {
struct timeval now;
struct timeval due; /* earliest due descriptor */
fd_set *r;
fd_set *w;
int maxfd;
int run; /* 0: prepare select, 1: run */
};
typedef int (*cb_fn)(void *sess, struct cb_args *a);
struct sess {
struct sess *next;
struct app *app; /* parent application */
cb_fn cb;
void *arg; /* identifier */
int fd;
};
/*
* All sessions should start with a 'struct sess'
*/
/*
* my_args contains all the arguments for the program
* The current app is me->app
* The current session is me->sess;
*/
struct my_args {
struct app **all_apps; // array of all applications
struct app *app; // app under service
struct sess *sess;
struct sess *tmp_sess;
struct sess *cur; // session under service
int verbose; /* allow read all file systems */
};
extern struct my_args __me;
/*
* Constructor for a new session
*/
void *new_sess(int size, int fd, cb_fn cb, void *arg);
/*
* generic socket open routine (general use)
*/
int opensock(struct sockaddr_in sa, int udp, int client);
/* add a millisecond value to a timer */
void timeradd_ms(const struct timeval *src, int ms, struct timeval *dst);
/* set dst to the min of the two */
void timersetmin(struct timeval *dst, const struct timeval *cur);
/* returns true if dst is set and <= 'now' */
int timerdue(const struct timeval *dst, const struct timeval *now);
/*extern struct app;*/
#endif /* _MYTS_H_ */