summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xPICA_client.c67
-rwxr-xr-xPICA_client.h29
2 files changed, 81 insertions, 15 deletions
diff --git a/PICA_client.c b/PICA_client.c
index 12e3f68..4e2bcdd 100755
--- a/PICA_client.c
+++ b/PICA_client.c
@@ -1462,8 +1462,69 @@ mp = c2n_writebuf_push( ci, PICA_PROTO_CONNREQOUTG, PICA_PROTO_CONNREQOUTG_SIZE)
return PICA_OK;
}
-int PICA_event_loop(struct PICA_c2n **connections, struct PICA_listener **listeners)
+static void fdset_add(fd_set *fds, int fd, int *max)
{
+FD_SET(fd, fds);
+
+if (fd > *max)
+ *max = fd;
+}
+
+int PICA_event_loop(struct PICA_c2n **connections, struct PICA_listener **listeners, int timeout)
+{
+fd_set rfds, wfds;
+struct timeval tv;
+int ret, nfds = 0;
+struct PICA_c2n **ic2n;
+struct PICA_listener **ilst;
+
+tv.tv_sec = timeout / 1000;
+tv.tv_usec = (timeout % 1000) * 1000;
+
+FD_ZERO(&rfds);
+FD_ZERO(&wfds);
+
+ilst = listeners;
+
+while(ilst && *ilst)
+ {
+ fdset_add(&rfds, (*ilst)->sck_listener, &nfds);
+ ilst++;
+ }
+
+ic2n = connections;
+
+while(ic2n && *ic2n)
+ {
+ struct PICA_c2c *ic2c;
+
+ fdset_add(&rfds, (*ic2n)->sck_comm, &nfds);
+
+ if ((*ic2n)->write_pos)
+ fdset_add(&wfds, (*ic2n)->sck_comm, &nfds);
+
+ ic2c = (*ic2n)->chan_list_head;
+
+ while(ic2c)
+ {
+ if (ic2c->state == PICA_CHANSTATE_ACTIVE)
+ {
+ fdset_add(&rfds, ic2c->sck_data, &nfds);
+
+ if (ic2c->write_pos)
+ fdset_add(&wfds, ic2c->sck_data, &nfds);
+ }
+
+ ic2c = ic2c->next;
+ }
+
+ ic2n++;
+ }
+
+
+
+ret = select(nfds + 1, &rfds, &wfds, NULL, &tv);
+
}
@@ -1787,10 +1848,6 @@ int PICA_read_c2n(struct PICA_c2n *ci)
{
int ret;
-switch(ci->state)
-{
-//<<<
-}
//ret=SSL_read(ci->ssl_comm, ci->read_buf + ci->read_pos, PICA_CONNREADBUFSIZE - ci->read_pos);
if (PICA_CONNSTATE_CONNECTING == ci->state)
diff --git a/PICA_client.h b/PICA_client.h
index d37e0f0..1d40612 100755
--- a/PICA_client.h
+++ b/PICA_client.h
@@ -98,13 +98,6 @@ typedef unsigned __int64 uint64_t;
#define PICA_CONNWRITEBUFSIZE 4096
#define PICA_FILEFRAGMENTSIZE 16384 //max size of TLS record
-#define PICA_CHANSTATE_ACTIVE 13
-
-#define PICA_CONNSTATE_CONNECTING 17
-#define PICA_CONNSTATE_WAITINGREP 18
-#define PICA_CONNSTATE_WAITINGTLS 21
-#define PICA_CONNSTATE_CONNECTED 19
-
#define PICA_CHANSENDFILESTATE_IDLE 0
#define PICA_CHANSENDFILESTATE_SENTREQ 20
#define PICA_CHANSENDFILESTATE_SENDING 22
@@ -130,6 +123,15 @@ unsigned char id[PICA_ID_SIZE]; //SHA224 hash of user's certificate in DER forma
SSL_CTX* ctx;
};
+enum PICA_c2n_state
+{
+PICA_CONNSTATE_NONE = 0,
+PICA_CONNSTATE_CONNECTING,
+PICA_CONNSTATE_WAITINGREP,
+PICA_CONNSTATE_WAITINGTLS,
+PICA_CONNSTATE_CONNECTED
+};
+
struct PICA_c2n
{
struct PICA_acc *acc;
@@ -141,7 +143,7 @@ SOCKET sck_comm;
SSL* ssl_comm;
-int state;
+enum PICA_c2n_state state;
unsigned char *read_buf;
unsigned char *write_buf;
@@ -157,6 +159,12 @@ int init_resp_ok;
unsigned char node_ver_major, node_ver_minor;
};
+enum PICA_c2c_state
+{
+PICA_CHANSTATE_NONE = 0,
+PICA_CHANSTATE_ACTIVE
+};
+
struct PICA_c2c
{
struct PICA_acc *acc;
@@ -177,7 +185,7 @@ unsigned int write_sslbytestowrite;
struct PICA_c2c *next;
struct PICA_c2c *prev;
-int state;
+PICA_c2c_state state;
time_t timestamp;
int sendfilestate;
uint64_t sendfile_size;
@@ -280,7 +288,8 @@ int PICA_read(struct PICA_c2n *ci,int timeout);
int PICA_write(struct PICA_c2n *ci);
//connections, listeners - NULL-terminated arrays of pointers to appropriate structures
-int PICA_event_loop(struct PICA_c2n **connections, struct PICA_listener **listeners);
+// timeout - timeout in milliseconds
+int PICA_event_loop(struct PICA_c2n **connections, struct PICA_listener **listeners, int timeout);
int PICA_send_msg(struct PICA_c2c *chn, char *buf,unsigned int len);
int PICA_read_msg(struct PICA_c2c *chn,char *buf,unsigned int *n);