e9eff3a9552a9178f09f530aa8fa5e24a4cd026e
[apps/madmutt.git] / lib-sys / evtloop.h
1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License as published by
4  *  the Free Software Foundation; either version 2 of the License, or (at
5  *  your option) any later version.
6  *
7  *  This program is distributed in the hope that it will be useful, but
8  *  WITHOUT ANY WARRANTY; without even the implied warranty of
9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  *  General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License
13  *  along with this program; if not, write to the Free Software
14  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
15  *  MA 02110-1301, USA.
16  */
17 /*
18  *  Copyright © 2007 Pierre Habouzit
19  */
20
21 #ifndef MUTT_LIB_SYS_EVTLOOP_H
22 #define MUTT_LIB_SYS_EVTLOOP_H
23
24 #include <lib-lib/lib-lib.h>
25 #include <gnutls/gnutls.h>
26 #include <sys/socket.h>
27 #include "account.h"
28
29 typedef enum el_state {
30     EL_LLP_INIT,
31     EL_LLP_READY,
32     EL_LLP_FINI,
33 } el_state;
34
35 typedef enum el_mode {
36     EL_NEW     = 0,
37     EL_READING = 1,
38     EL_WRITING = 2,
39     EL_RDWR    = 3,
40     EL_IDLE    = 4,
41 } el_mode;
42
43 typedef enum el_status {
44     EL_SUCCESS,
45     EL_ERROR,
46     EL_KILLED,
47     EL_RDHUP,
48 } el_status;
49
50 typedef enum el_event {
51     EL_EVT_IN      = EL_READING,
52     EL_EVT_OUT     = EL_WRITING,
53     EL_EVT_INOUT   = EL_RDWR,
54     EL_EVT_RUNNING = 4,
55     EL_EVT_WAKEUP  = 5,
56 } el_event;
57
58 typedef struct job_t {
59     int fd;
60     int ssf;
61
62     unsigned cond  : 1;
63     el_state state : 2;
64     el_mode  mode  : 3;
65     el_mode  emode : 3;
66
67     gnutls_session_t session;
68     gnutls_certificate_credentials_t xcred;
69
70     struct timeval mru;
71     int (*llp)(struct job_t *);
72     const struct machine_t *m;
73     void *ptr;
74 } job_t;
75 DO_INIT(job_t, job);
76 void job_wipe(job_t *w);
77 DO_NEW(job_t, job);
78 DO_DELETE(job_t, job);
79
80 typedef struct machine_t {
81     const char *name;
82     __must_check__ int (*setup)(job_t *w, void *);
83     __must_check__ int (*on_event)(job_t *w, el_event);
84     void (*finalize)(job_t *w, el_status);
85 } machine_t;
86
87 #define EL_JOB_CHECK(expr) \
88     do { if ((expr) < 0) return -1; } while (0)
89
90 __must_check__ int el_job_setmode(job_t *w, el_mode);
91 __must_check__ job_t *el_job_start(const machine_t *m, void *cfg);
92 __must_check__ int el_job_release(job_t *j, el_status);
93
94 __must_check__ int el_job_connect(job_t *w, struct sockaddr *, socklen_t len,
95                                   int type, int proto, int ssl);
96 __must_check__ int el_job_connect2(job_t *w, const ACCOUNT *);
97 __must_check__ int el_job_starttls(job_t *w);
98 __must_check__ ssize_t el_job_read(job_t *w, buffer_t *buf);
99 __must_check__ ssize_t el_job_write(job_t *w, buffer_t *buf);
100
101 void el_lock(void);
102 void el_unlock(void);
103 void el_wait(volatile job_t *w);
104
105 int el_dispatch(int timeout);
106
107 void el_initialize(void);
108 void el_shutdown(void);
109
110 #endif