File Conn.c changed (mode: 100644) (index a8aa5b7..05261cd) |
... |
... |
int Conn_engine_set(const unsigned int engine) |
102 |
102 |
return 0; |
return 0; |
103 |
103 |
} |
} |
104 |
104 |
|
|
105 |
|
int Conn_init(unsigned int max) |
|
|
105 |
|
int Conn_init(const unsigned int max) |
106 |
106 |
{ |
{ |
107 |
107 |
unsigned int ret; |
unsigned int ret; |
108 |
108 |
unsigned int engine; |
unsigned int engine; |
|
... |
... |
static struct Conn *Conn_alloc(void) |
389 |
389 |
return &Conns[slot]; |
return &Conns[slot]; |
390 |
390 |
} |
} |
391 |
391 |
|
|
392 |
|
struct Conn *Conn_socket(int domain, int type, int port) |
|
|
392 |
|
struct Conn *Conn_socket(const int domain, const int type, const int port) |
393 |
393 |
{ |
{ |
394 |
394 |
struct Conn *C; |
struct Conn *C; |
395 |
395 |
int i, ret; |
int i, ret; |
|
... |
... |
struct Conn *Conn_socket(int domain, int type, int port) |
530 |
530 |
return NULL; |
return NULL; |
531 |
531 |
} |
} |
532 |
532 |
|
|
533 |
|
struct Conn *Conn_connect(int domain, int type, char *addr, int port) |
|
|
533 |
|
struct Conn *Conn_connect(const int domain, const int type, const char *addr, |
|
534 |
|
const int port) |
534 |
535 |
{ |
{ |
535 |
536 |
struct Conn *X; |
struct Conn *X; |
536 |
|
unsigned int Xslot; |
|
537 |
537 |
|
|
538 |
538 |
Log(8, "%s(%s, %d)\n", |
Log(8, "%s(%s, %d)\n", |
539 |
539 |
__FUNCTION__, addr, port); |
__FUNCTION__, addr, port); |
|
... |
... |
static void Conn_band_update(struct Conn *C) |
717 |
717 |
if (C->band_tokens > C->band_factor * C->band_width) |
if (C->band_tokens > C->band_factor * C->band_width) |
718 |
718 |
C->band_tokens = C->band_factor * C->band_width; |
C->band_tokens = C->band_factor * C->band_width; |
719 |
719 |
|
|
720 |
|
slot = C->slot; |
|
721 |
|
Conns[slot].events |= CONN_POLLOUT; |
|
722 |
|
Conn_engine_chg_obj(&Conns[slot]); |
|
|
720 |
|
C->events |= CONN_POLLOUT; |
|
721 |
|
Conn_engine_chg_obj(C); |
723 |
722 |
|
|
724 |
|
Log(debug_band, "\t\tBAND: Added tokens -> %u.\n", |
|
725 |
|
C->band_tokens); |
|
|
723 |
|
Log(debug_band, "\t\tBAND: slot=%u, id=%llu, added tokens -> %u.\n", |
|
724 |
|
C->slot, C->id, C->band_tokens); |
726 |
725 |
} |
} |
727 |
726 |
|
|
728 |
727 |
/* |
/* |
729 |
728 |
* Set the bandwidth for a connection |
* Set the bandwidth for a connection |
730 |
729 |
* width is in 1b increments |
* width is in 1b increments |
731 |
730 |
*/ |
*/ |
732 |
|
int Conn_band(struct Conn *C, unsigned int width, unsigned int factor) |
|
|
731 |
|
int Conn_band(struct Conn *C, const unsigned int width, |
|
732 |
|
const unsigned int factor) |
733 |
733 |
{ |
{ |
734 |
|
Log(11, "\tConn_band(C, width=%u, factor=%u)\n", |
|
735 |
|
width, factor); |
|
|
734 |
|
Log(11, "\tConn_band: slot=%u, id=%llu, width=%u, factor=%u.\n", |
|
735 |
|
C->slot, C->id, width, factor); |
736 |
736 |
|
|
737 |
737 |
C->band_lasttime = Conn_now; |
C->band_lasttime = Conn_now; |
738 |
738 |
C->band_width = width; |
C->band_width = width; |
|
... |
... |
static void Conn_trytoconnect(void) |
768 |
768 |
if (Conns[i].state != CONN_STATE_CONNECT_a) |
if (Conns[i].state != CONN_STATE_CONNECT_a) |
769 |
769 |
continue; |
continue; |
770 |
770 |
|
|
771 |
|
Log(9, "\tTry to connect to %s/%d...\n", |
|
772 |
|
Conns[i].addr, Conns[i].port); |
|
|
771 |
|
Log(9, "\tTry to connect slot=%u, id=%llu, to %s/%d...\n", |
|
772 |
|
Conns[i].slot, Conns[i].id, Conns[i].addr, Conns[i].port); |
773 |
773 |
|
|
774 |
774 |
memset(&hints, 0, sizeof(hints)); |
memset(&hints, 0, sizeof(hints)); |
775 |
775 |
if (Conns[i].sock_domain == 0) |
if (Conns[i].sock_domain == 0) |
|
... |
... |
static void Conn_trytoconnect(void) |
829 |
829 |
} |
} |
830 |
830 |
|
|
831 |
831 |
Conns[i].state = CONN_STATE_CONNECT_b; |
Conns[i].state = CONN_STATE_CONNECT_b; |
|
832 |
|
|
832 |
833 |
Conn_pending--; |
Conn_pending--; |
833 |
834 |
Conn_total++; |
Conn_total++; |
834 |
835 |
|
|
|
... |
... |
static void Conn_send_cb_i(struct Conn *C) |
846 |
847 |
int xerrno; |
int xerrno; |
847 |
848 |
char *dump; |
char *dump; |
848 |
849 |
|
|
849 |
|
Log(10, "Conn_send_cb_i id=%llu, slot=%u...\n", |
|
850 |
|
C->id, C->slot); |
|
|
850 |
|
Log(10, "Conn_send_cb_i slot=%u, id=%llu, head=%u, tail=%u, size=%u...\n", |
|
851 |
|
C->slot, C->id, C->obuf_head, C->obuf_tail, C->obuf_size); |
851 |
852 |
|
|
852 |
853 |
slot = C->slot; |
slot = C->slot; |
853 |
854 |
|
|
|
... |
... |
static void Conn_send_cb_i(struct Conn *C) |
875 |
876 |
} |
} |
876 |
877 |
|
|
877 |
878 |
again: |
again: |
878 |
|
Log(10, "\tsend(fd%d, buf (head=%u, tail=%u), max=%d (count=%d), 0)...\n", |
|
|
879 |
|
Log(10, "\tsend(fd=%d, buf (head=%u, tail=%u), max=%d (count=%d), 0)...\n", |
879 |
880 |
Conns[slot].fd, Conns[slot].obuf_head, |
Conns[slot].fd, Conns[slot].obuf_head, |
880 |
881 |
Conns[slot].obuf_tail, max, count); |
Conns[slot].obuf_tail, max, count); |
881 |
882 |
n = send(Conns[slot].fd, buf, max, 0); |
n = send(Conns[slot].fd, buf, max, 0); |
|
... |
... |
static void Conn_send_cb_i(struct Conn *C) |
886 |
887 |
if ((n == -1) && (errno == EAGAIN)) |
if ((n == -1) && (errno == EAGAIN)) |
887 |
888 |
return; |
return; |
888 |
889 |
|
|
889 |
|
Log(10, "%s: Slot %u: FD%d Sent %d bytes [head=%d tail=%d]\n", |
|
890 |
|
__FUNCTION__, slot, Conns[slot].fd, |
|
|
890 |
|
Log(10, "%s: slot=%u, id=%llu: fd=%d Sent %d bytes [head=%d tail=%d]\n", |
|
891 |
|
__FUNCTION__, slot, Conns[slot].id, Conns[slot].fd, |
891 |
892 |
n, Conns[slot].obuf_head, Conns[slot].obuf_tail); |
n, Conns[slot].obuf_head, Conns[slot].obuf_tail); |
892 |
893 |
if (Conn_level >= 10) { |
if (Conn_level >= 10) { |
893 |
894 |
dump = Conn_dump(buf, n); |
dump = Conn_dump(buf, n); |
|
... |
... |
static void Conn_send_cb_i(struct Conn *C) |
912 |
913 |
n, Conns[slot].band_tokens); |
n, Conns[slot].band_tokens); |
913 |
914 |
} |
} |
914 |
915 |
} else { |
} else { |
915 |
|
Log(0, "%s: Error in send [id %llu] [slot %u] [%s]\n", |
|
|
916 |
|
Log(0, "%s: Error in send (slot=%u, id=%llu) [%s]\n", |
916 |
917 |
__FUNCTION__, |
__FUNCTION__, |
917 |
|
Conns[slot].id, slot, strerror(errno)); |
|
|
918 |
|
slot, Conns[slot].id, strerror(errno)); |
918 |
919 |
Conns[slot].error_state = CONN_ERROR_SEND; |
Conns[slot].error_state = CONN_ERROR_SEND; |
919 |
920 |
Conns[slot].xerrno = xerrno; |
Conns[slot].xerrno = xerrno; |
920 |
921 |
} |
} |
|
... |
... |
static void Conn_send_cb_i(struct Conn *C) |
922 |
923 |
|
|
923 |
924 |
static void Conn_recv_cb_i(struct Conn *C) |
static void Conn_recv_cb_i(struct Conn *C) |
924 |
925 |
{ |
{ |
925 |
|
ssize_t n; |
|
|
926 |
|
ssize_t n; |
926 |
927 |
unsigned int max; |
unsigned int max; |
927 |
928 |
unsigned int slot; |
unsigned int slot; |
928 |
929 |
int r, xerrno; |
int r, xerrno; |
929 |
930 |
char *dump; |
char *dump; |
930 |
931 |
|
|
931 |
|
Log(10, "Conn_recv_cb_i id=%llu, slot=%u...\n", |
|
932 |
|
C->id, C->slot); |
|
|
932 |
|
Log(10, "Conn_recv_cb_i slot=%u, id=%llu, head=%u, tail=%u, size=%u...\n", |
|
933 |
|
C->slot, C->id, C->ibuf_head, C->ibuf_tail, C->ibuf_size); |
933 |
934 |
|
|
934 |
935 |
slot = C->slot; |
slot = C->slot; |
935 |
936 |
|
|
|
... |
... |
static void Conn_recv_cb_i(struct Conn *C) |
955 |
956 |
break; |
break; |
956 |
957 |
} |
} |
957 |
958 |
|
|
958 |
|
Log(10, "%s: Slot %u: FD%d Received %d bytes.\n", |
|
959 |
|
__FUNCTION__, slot, Conns[slot].fd, n); |
|
|
959 |
|
Log(10, "%s: slot=%u, id=%llu: fd=%d Received %d bytes.\n", |
|
960 |
|
__FUNCTION__, slot, Conns[slot].id, Conns[slot].fd, n); |
960 |
961 |
|
|
961 |
962 |
if (n > 0) { |
if (n > 0) { |
962 |
963 |
if (Conn_level >= 10) { |
if (Conn_level >= 10) { |
|
... |
... |
static void Conn_recv_cb_i(struct Conn *C) |
978 |
979 |
} else if (n == 0) { |
} else if (n == 0) { |
979 |
980 |
Conns[slot].error_state = CONN_ERROR_HANGUP; |
Conns[slot].error_state = CONN_ERROR_HANGUP; |
980 |
981 |
} else { |
} else { |
981 |
|
Log(4, "Error in recv id %llu slot %u [%s]\n", |
|
982 |
|
Conns[slot].id, slot, strerror(errno)); |
|
|
982 |
|
Log(4, "Error in recv (slot=%u, id=%llu) [%s]\n", |
|
983 |
|
slot, Conns[slot].id, strerror(errno)); |
983 |
984 |
Conns[slot].error_state = CONN_ERROR_RECV; |
Conns[slot].error_state = CONN_ERROR_RECV; |
984 |
985 |
Conns[slot].xerrno = xerrno; |
Conns[slot].xerrno = xerrno; |
985 |
986 |
} |
} |
File Conn.h changed (mode: 100644) (index 6a4f651..677f5d8) |
3 |
3 |
|
|
4 |
4 |
#include <Conn_engine_core.h> |
#include <Conn_engine_core.h> |
5 |
5 |
|
|
6 |
|
extern int Conn_init(unsigned int max); |
|
7 |
|
extern ssize_t Conn_send(struct Conn *C, void *buf, size_t count); |
|
8 |
|
extern ssize_t Conn_recv(struct Conn *C, void *buf, size_t count); |
|
9 |
|
extern struct Conn *Conn_socket(int domain, int type, int port); |
|
10 |
|
extern int Conn_poll(int timeout); |
|
11 |
|
extern int Conn_enqueue(struct Conn *C, void *buf, size_t count); |
|
12 |
|
/* |
|
13 |
|
extern void Conn_debug(FILE *f, unsigned short debug); |
|
14 |
|
*/ |
|
15 |
|
extern struct Conn *Conn_connect(int domain, int type, char *addr, int port); |
|
16 |
|
extern int Conn_band(struct Conn *C, unsigned int width, unsigned int factor); |
|
17 |
|
|
|
|
6 |
|
extern int Conn_init(const unsigned int max); |
|
7 |
|
extern int Conn_shutdown(void); |
|
8 |
|
extern ssize_t Conn_send(struct Conn *C, void *buf, const size_t count); |
|
9 |
|
extern ssize_t Conn_recv(struct Conn *C, void *buf, const size_t count); |
|
10 |
|
extern struct Conn *Conn_socket(const int domain, const int type, |
|
11 |
|
const int port); |
|
12 |
|
extern int Conn_poll(const int timeout); |
|
13 |
|
extern int Conn_enqueue(struct Conn *C, void *buf, |
|
14 |
|
const size_t count); |
|
15 |
|
extern struct Conn *Conn_connect(const int domain, const int type, |
|
16 |
|
const char *addr, const int port); |
|
17 |
|
extern int Conn_band(struct Conn *C, const unsigned int width, |
|
18 |
|
const unsigned int factor); |
18 |
19 |
|
|
19 |
20 |
#endif |
#endif |
File examples/c.c changed (mode: 100644) (index 79d49f4..66da5c8) |
20 |
20 |
#include <Conn.h> |
#include <Conn.h> |
21 |
21 |
|
|
22 |
22 |
/* Global variables */ |
/* Global variables */ |
23 |
|
static unsigned short debug = 9; |
|
|
23 |
|
static unsigned short debug = 20; |
|
24 |
|
static unsigned int ipv4conns = 500, ipv6conns = 500; |
24 |
25 |
|
|
25 |
26 |
static FILE *Logf = NULL; |
static FILE *Logf = NULL; |
26 |
27 |
static char *log_file = "c.log"; |
static char *log_file = "c.log"; |
|
... |
... |
static void c_connected(struct Conn *C) |
52 |
53 |
|
|
53 |
54 |
static void c_close(struct Conn *C) |
static void c_close(struct Conn *C) |
54 |
55 |
{ |
{ |
55 |
|
Log(5, "%s(Slot %d will close)\n", |
|
56 |
|
__FUNCTION__, C->slot); |
|
|
56 |
|
Log(5, "%s: id=%llu will close...\n", |
|
57 |
|
__FUNCTION__, Conn_getid(C)); |
57 |
58 |
free(C->private); |
free(C->private); |
58 |
59 |
} |
} |
59 |
60 |
|
|
|
... |
... |
int main(void) |
95 |
96 |
int ret; |
int ret; |
96 |
97 |
struct timeval start, end; |
struct timeval start, end; |
97 |
98 |
unsigned int elap; |
unsigned int elap; |
98 |
|
unsigned int i, ipv4conns = 5000, ipv6conns = 5000; |
|
|
99 |
|
unsigned int i; |
99 |
100 |
struct Conn *C4, *C6; |
struct Conn *C4, *C6; |
100 |
101 |
|
|
101 |
102 |
Logf = fopen(log_file, "a+"); |
Logf = fopen(log_file, "a+"); |
|
... |
... |
int main(void) |
107 |
108 |
if (debug > 0) |
if (debug > 0) |
108 |
109 |
setlinebuf(Logf); |
setlinebuf(Logf); |
109 |
110 |
|
|
|
111 |
|
Conn_debug(Logf, debug); |
|
112 |
|
|
110 |
113 |
Log(0, "Starting...\n"); |
Log(0, "Starting...\n"); |
111 |
114 |
Log(0, "\tPort=%d\n", port); |
Log(0, "\tPort=%d\n", port); |
112 |
115 |
Log(0, "\tLogFile=%s Debug=%d\n", log_file, debug); |
Log(0, "\tLogFile=%s Debug=%d\n", log_file, debug); |
113 |
116 |
|
|
114 |
|
Conn_debug(Logf, debug); |
|
115 |
|
|
|
116 |
117 |
ret = Conn_init(0); |
ret = Conn_init(0); |
117 |
118 |
if (ret == -1) { |
if (ret == -1) { |
118 |
119 |
Log(0, "%s", Conn_strerror()); |
Log(0, "%s", Conn_strerror()); |
|
... |
... |
int main(void) |
156 |
157 |
break; |
break; |
157 |
158 |
} |
} |
158 |
159 |
|
|
159 |
|
if (ret == 0) { |
|
160 |
|
Log(0, "Finish work!\n"); |
|
161 |
|
break; |
|
162 |
|
} |
|
163 |
|
|
|
164 |
|
if (answers % 10 == 0) |
|
165 |
|
printf("."); |
|
|
160 |
|
printf("%u answer(s)\n", answers); |
166 |
161 |
|
|
167 |
162 |
if (answers == ipv4conns + ipv6conns) |
if (answers == ipv4conns + ipv6conns) |
168 |
163 |
break; |
break; |