#include #include #include #include #include #include #include #include using namespace std; class C_INFO: public ARRAY_OBJ{ public: bool is_ctl; bool is_client; int talkto; }; class C_BLK{ public: int target_tick; int talkto; int linelen; char *line; C_BLK(int _target_tick, int _talkto, const char *_line, int _linelen) { target_tick = _target_tick; talkto = _talkto; linelen = _linelen; line = NULL; if (linelen >= 0){ line = (char*)malloc(_linelen); memcpy (line,_line,_linelen); } } ~C_BLK() { free (line); } C_BLK(const C_BLK &c) { target_tick = c.target_tick; talkto = c.talkto; linelen = c.linelen; line = NULL; if (linelen >= 0){ line = (char*)malloc (c.linelen); memcpy (line,c.line,c.linelen); } } C_BLK &operator =(const C_BLK &c) { target_tick = c.target_tick; talkto = c.talkto; linelen = c.linelen; line = NULL; if (linelen >= 0){ line = (char*)malloc (c.linelen); memcpy (line,c.line,c.linelen); } return *this; } }; int main (int argc, char *argv[]) { glocal int ret = -1; glocal const char *host = NULL; glocal const char *dport = NULL; glocal const char *lport = "8200"; glocal int nbtick = 10; glocal.ret = (argc,argv); setarg ('h',"host","host to connect",glocal.host,true); setarg ('l',"listen","Listen on this port",glocal.lport,false); setarg ('p',"port","port to connect",glocal.dport,true); setarg ('n',"nbtick","nbtick between sent packets (1 tick is 10ms)",glocal.nbtick,false); if (fork()==0){ sleep(1); ("localhost",glocal.lport,10); while (1){ usleep(10000); send (" ",1); } }else{ glocal bool first = true; glocal int tick = 0; glocal vector blks; (glocal.lport,10); C_INFO *inf = new C_INFO; if (glocal.first){ glocal.first = false; inf->is_ctl = true; }else{ inf->is_ctl = false; inf->is_client = true; inf->talkto = cmdsock_connect (glocal.host ,glocal.dport,10,1); if (inf->talkto == -1){ endclient = true; }else{ C_INFO *inf2 = new C_INFO; inf2->talkto = no; inf2->is_ctl = false; inf2->is_client = false; inject (inf->talkto,inf2); debug_printf ("newclient %d -> %d\n",no,inf->talkto); } } info.data = inf; C_INFO *inf = (C_INFO*)info.data; debug_printf ("endclient %d -> %d\n",no,inf->talkto); // On enleve toutes references a ce handle // On transmet tout ce qui est en attente pour le destinataire // et on le ferme for (vector::iterator it=glocal.blks.begin(); it != glocal.blks.end(); it++){ if (it->talkto == no){ it->talkto = -1; }else if (it->talkto == inf->talkto){ sendto (it->talkto,it->line,it->linelen); it->talkto = -1; } } closeclient (inf->talkto); C_INFO *inf = (C_INFO*)info.data; if (inf->is_ctl){ if ((glocal.tick % 100)==0) debug_printf ("tick %d\n",glocal.tick); glocal.tick++; int delto = 0; for (vector::iterator it=glocal.blks.begin(); it != glocal.blks.end(); it++){ if (it->target_tick <= glocal.tick){ if (it->talkto != -1){ if (it->linelen == -1){ closeclient (it->talkto); }else{ debug_printf ("Send %d [%d]\n",it->talkto,it->linelen); sendto (it->talkto,it->line,it->linelen); } } delto++; }else{ break; } } if (delto > 0){ glocal.blks.erase (glocal.blks.begin() ,glocal.blks.begin() + delto); } }else{ debug_printf ("Rec %d -> %d [%d]\n",no,inf->talkto,info.linelen); // Split the block in smaller blocks as if it was received over the network unsigned blocklen = info.linelen; const char *ptblock = line; unsigned tick = glocal.tick+glocal.nbtick; while (blocklen > 1500){ debug_printf ("emplace %d 1500\n",inf->talkto); glocal.blks.emplace_back(tick,inf->talkto,ptblock,1500); blocklen -= 1500; ptblock += 1500; tick++; } if (blocklen > 0){ debug_printf ("emplace %d %u\n",inf->talkto,blocklen); glocal.blks.emplace_back(tick,inf->talkto,ptblock,blocklen); } } s.setrawmode (true); s.loop(); } return 0; return glocal.ret; }