static void recvJob(int16 cnId) { int16 nInQueue; clock_t tQuit; while (1) { /* loop over subcommands */ tQuit = clock() + TIMEOUT * CLK_TCK; /* time to quit at */ while ( (nInQueue = CNbyte_count(cnId)) == 0 || nInQueue == E_NODATA) { if (clock() > tQuit) { uiPrintf(uiH, uiPrERR, "rcvJob|timed out"); return; } uiYield(uiH, YIELDMS); /* wait till something arrives */ } if (nInQueue == E_EOF) return; /* connection closed, no more subcommands */ if (nInQueue > 0) { NDB* ndb; if ( (ndb = CNget_NDB(cnId)) != NULL ) { dispatchSubCmd(cnId, ndb); } else { uiPrintf(uiH, uiPrERR, "recvJob|get_NDB"); } } else { uiPrintf(uiH, uiPrERR, "recvJob|%s", get_err_text(nInQueue)); return; } } /* while..more subcommands */ } /* recvJob */
void test0130(void) { int handle, res, ok; int a,b,c; out_test_header(0x0130, "UDP - CNbyte_count() on 3 datagrams"); handle = UDP_open(SERVER_ADDR, SERVER_PORT_START + 4); if(handle >= 0) { // send 3 x 100 bytes (void) UDP_send(handle, wBuf, 100); (void) UDP_send(handle, wBuf, 100); (void) UDP_send(handle, wBuf, 100); sleep(1); res = CNbyte_count(handle); // see how many bytes we got for reading on this socket ok = (res == 300) ? 1 : 0; out_result_error(ok, res); //----------------------------------- out_test_header(0x0131, "UDP - CNget_block() on 3 datagrams"); // receive it using 3x CNget_block() a = CNget_block (handle, rBuf, 100); b = CNget_block (handle, rBuf, 100); c = CNget_block (handle, rBuf, 100); ok = (a == 100 && b == 100 && c == 100) ? 1 : 0; out_result_error(ok, res); //----------------------------------- UDP_close(handle); } else { out_result_string(0, "UDP_open failed"); } ////////////////////////////////////////////////////////////////////////////// out_test_header(0x0132, "UDP - CNbyte_count() after partial read"); handle = UDP_open(SERVER_ADDR, SERVER_PORT_START + 4); if(handle >= 0) { // send 300 bytes (void) UDP_send(handle, wBuf, 300); sleep(1); res = CNbyte_count(handle); // see how many bytes we got for reading on this socket ok = (res == 300) ? 1 : 0; if(!ok) { // if not enough data, fail out_result_error_string(ok, res, "not enough data"); } else { a = CNget_block (handle, rBuf, 100); b = CNbyte_count(handle); ok = (a == 100 && b == 200) ? 1 : 0; out_result_error(ok, b); } UDP_close(handle); } else { out_result_string(0, "UDP_open failed"); } ////////////////////////////////////////////////////////////////////////////// out_test_header(0x0133, "UDP - get 3 DGRAMs with 1 CNget_block"); handle = UDP_open(SERVER_ADDR, SERVER_PORT_START + 4); if(handle >= 0) { // send 300 bytes (void) UDP_send(handle, wBuf, 100); (void) UDP_send(handle, wBuf, 100); (void) UDP_send(handle, wBuf, 100); sleep(1); res = CNbyte_count(handle); // see how many bytes we got for reading on this socket ok = (res == 300) ? 1 : 0; if(!ok) { // if not enough data, fail out_result_error_string(ok, res, "not enough data"); } else { a = CNget_block (handle, rBuf, 300); b = CNbyte_count(handle); ok = (a == 300 && b == 0) ? 1 : 0; out_result_error(ok, b); } UDP_close(handle); } else { out_result_string(0, "UDP_open failed"); } ////////////////////////////////////////////////////////////////////////////// out_test_header(0x0134, "UDP - get 3 DGRAMs with 3x CNget_NDB"); handle = UDP_open(SERVER_ADDR, SERVER_PORT_START + 4); if(handle >= 0) { // send 300 bytes (void) UDP_send(handle, wBuf, 100); (void) UDP_send(handle, wBuf, 100); (void) UDP_send(handle, wBuf, 100); sleep(1); res = CNbyte_count(handle); // see how many bytes we got for reading on this socket ok = (res == 300) ? 1 : 0; if(!ok) { // if not enough data, fail out_result_error_string(ok, res, "not enough data"); } else { NDB *m,*n,*o; m = CNget_NDB(handle); n = CNget_NDB(handle); o = CNget_NDB(handle); ok = (m != NULL && n != NULL && o != NULL) ? 1 : 0; if(!ok) { out_result_string(ok, "some CNget_NDB failed"); } else { ok = (m->len == 100 && n->len == 100 && o->len == 100) ? 1 : 0; if(!ok) { out_result_string(ok, "length of NDB block wrong"); } else { out_result(1); } } } UDP_close(handle); } else { out_result_string(0, "UDP_open failed"); } ////////////////////////////////////////////////////////////////////////////// out_test_header(0x0135, "UDP - get 3 DGRAMs with CNget_char"); handle = UDP_open(SERVER_ADDR, SERVER_PORT_START + 4); if(handle >= 0) { // send 300 bytes (void) UDP_send(handle, wBuf, 100); (void) UDP_send(handle, wBuf, 100); (void) UDP_send(handle, wBuf, 100); sleep(1); res = CNbyte_count(handle); // see how many bytes we got for reading on this socket ok = (res == 300) ? 1 : 0; b = 1; // good for now if(!ok) { // if not enough data, fail out_result_error_string(ok, res, "not enough data"); } else { int i; for(i=0; i<300; i++) { a = CNget_char(handle); if(a >= 0) { rBuf[i] = a; } else { out_result_error_string(0, a, "error on CNget_char"); b = 0; // failed break; } } if(b) { // if good, check data a = memcmp(rBuf, wBuf, 100); b = memcmp(rBuf + 100, wBuf, 100); c = memcmp(rBuf + 200, wBuf, 100); ok = (a == 0 && b == 0 && c == 0) ? 1 : 0; out_result(ok); } } UDP_close(handle); } else { out_result_string(0, "UDP_open failed"); } }
int sendAndReceive(BYTE tcpNotUdp, DWORD blockSize, int handle, BYTE getBlockNotNdb) { //---------- // send int res; DWORD now, endTime; DWORD kbs = 1 + (blockSize / 1024); endTime = getTicks() + (kbs * 200); // for each kB of data give 1 second to transfer while(1) { // try to send if(tcpNotUdp) { res = TCP_send(handle, wBuf, blockSize); } else { res = UDP_send(handle, wBuf, blockSize); } if(res != E_OBUFFULL) { break; } now = getTicks(); if(now >= endTime) { // timeout? out_result_error_string(0, blockSize, "send() timeout"); return 0; } } if(res != E_NORMAL) { out_result_error_string(0, res, "send() failed"); return 0; } //---------- // wait endTime = getTicks() + (kbs * 200); // for each kB of data give 1 second to transfer memset(rBuf, 0, blockSize); BYTE *pBuf = rBuf; DWORD toGet = blockSize; while(toGet > 0) { now = getTicks(); if(now >= endTime) { // timeout? out_result_error_string(0, blockSize, "CNbyte_count() timeout"); return 0; } res = CNbyte_count(handle); // find out how many bytes are waiting if(res > 0) { // something waiting? read it if(getBlockNotNdb) { // retrieve using CNget_block? res = CNget_block(handle, pBuf, res); if(res > 0) { pBuf += res; toGet -= res; } else if(res != E_NODATA && res < 0) { // if it's some error, and that error is not E_NODATA, fail out_result_error_string(0, blockSize, "CNget_block() failed"); return 0; } } else { // retrieve using CNget_NDB NDB *ndb = CNget_NDB(handle); if(ndb) { // if something retrieved memcpy(pBuf, ndb->ndata, ndb->len); // copy in the data pBuf += ndb->len; // it was this many bytes toGet -= ndb->len; KRfree (ndb->ptr); // free the ram KRfree (ndb); } } } } //---------- // data are valid? res = memcmp(rBuf, wBuf, blockSize); if(res != 0) { out_result_string(0, "Received data mismatch"); return 0; } //------- // if came here, everything is OK return 1; }
void do_some_work() { NDB *ndb; time_t timeout; int handle, message[2], response, ready; if ((handle = UDP_open (0, IP_DIALER_PORT)) < 0) { form_alert (1, no_udp); return; } for (;;) { timeout = time (NULL) + TIMEOUT; do { evnt_timer (200, 0); if ((ndb = CNget_NDB (handle)) != NULL) { switch (* (int16 *) ndb->ndata) { case IP_DIAL_REQUEST : sprintf (alert, query, ndb->ndata + 4, * (int16 *) (ndb->ndata + 2)); if (form_alert (1, alert) == 1) message[0] = IP_DIAL_DONE, message[1] = 0; else { message[0] = IP_DIAL_ERROR; response = 0; do { sprintf (alert, number, response); ready = 0; switch (form_alert (1, alert)) { case 1 : response --; break; case 2 : ready = 1; break; case 3 : response ++; break; } } while (! ready); message[1] = response; } break; case IP_DIAL_HANGUP : form_alert (1, hangup); message[0] = IP_DIAL_DONE; message[1] = 0; break; } KRfree (ndb->ptr); KRfree (ndb); UDP_send (handle, (char *) message, 4); UDP_close (handle); if ((handle = UDP_open (0, IP_DIALER_PORT)) < 0) { form_alert (1, no_udp); return; } } } while (time (NULL) < timeout); if (form_alert (1, proceed) != 1) break; } UDP_close (handle); }
static void waitRequests(void) { int16 cnId, state; int16 nInQueue; int toggle, proceed=1; do { /* listen again */ if( (cnId = TCP_open(0, LPR_LOC_PORT, 0, tcpBuffSize)) <= 0 ) { uiPrintf(uiH, uiPrERR, "waitRequests|TCP_open"); return; } if ( (state = TCP_wait_state(cnId, TLISTEN, 30)) < 0 ) { uiPrintf(uiH, uiPrERR, "waitRequests|%s", get_err_text(state)); return; } toggle = 10; /* every ten waits look also for an AES message */ while ( (nInQueue = CNbyte_count(cnId)) != E_EOF ) { /* poll for input */ /* listening or no data yet cause us to wait */ if (nInQueue == E_LISTEN || nInQueue == 0 || nInQueue == E_NODATA) { if (--toggle>0) { uiYield(uiH, YIELDMS); } else { WORD msgbuff[8]; WORD event; /* Ergebnis mit Ereignissen */ WORD mx, my, /* Mauskoordinaten */ mbutton, /* Mausknopf */ mkstate, /* keyb shift status for mouse button */ mclicks; /* Anzahl Mausklicks */ UWORD keycode; /* scancode + asciicode */ toggle=10; event = evnt_multi( MU_MESAG | MU_TIMER, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, msgbuff, YIELDMS, 0, &mx, &my, &mbutton, &mkstate, &keycode, &mclicks); if ( (event & MU_MESAG) && msgbuff[0] == AP_TERM ) { proceed=0; /* no more new connections */ break; /* end listening */ } } } else { if (nInQueue > 0) { /* otherwise there is valid data */ NDB* ndb; if ( (ndb = CNget_NDB(cnId)) != NULL ) { dispatchD(cnId, ndb); } else { uiPrintf(uiH, uiPrERR, "waitRequests|get_NDB"); break; } } else { /* catch other errors */ uiPrintf(uiH, uiPrERR, "waitRequests|%s", get_err_text(nInQueue)); break; } /* if valid data */ } /* if any data */ } /* while wait for a request */ TCP_close(cnId, TIMEOUT, NULL); /* disconnect */ } while (proceed); /* while new connection shall be done */ } /* waitRequests */
static void dumpFile(int16 cnId, char fileNam[], long fileLen) { int eFlag; char *eM; long accuLen; int fh; int16 nInQueue; clock_t tQuit; #if 0 uiPrintf(uiH, " %s|L: %ld|N: %s", pCnt, fileLen, fileNam); #endif if ( (fh=Fcreate(fileNam, 0)) < 0 ) { uiPrintf(uiH, uiPrERR, "dumpFile|cannot create file"); return; } for (eFlag=1,accuLen=0; eFlag; ) { tQuit = clock() + LTIMEOUT * CLK_TCK; /* time to quit at */ while ( (nInQueue = CNbyte_count(cnId)) == 0 || nInQueue == E_NODATA) { if (clock() > tQuit) { eM="timed out"; goto errExit; } uiYield(uiH, YIELDMS); /* wait till something arrives */ } if (nInQueue > 0) { NDB* ndb; if ( (ndb = CNget_NDB(cnId)) != NULL ) { accuLen += ndb->len; #if 0 uiPrintf(uiH, "al: %ld", accuLen); #endif if (accuLen == fileLen+1) { /* this happens at the end */ if (Fwrite(fh, ndb->len-1, ndb->ndata) <0) { eM="cannot write 1"; goto errExit; } if (ndb->ndata[ndb->len-1] != '\0') { eM="trailing 0 ?"; goto errExit; } eFlag=0; /* normal end */ } else { if (accuLen > fileLen+1) { /* this should never happen */ eM="prot.mismatch"; goto errExit; } else { if (Fwrite(fh, ndb->len, ndb->ndata) <0) { eM="cannot write 2"; goto errExit; } } } KRfree(ndb->ptr); KRfree(ndb); /* throw ndb away */ } else { eM="get_NDB"; goto errExit; } /* if..get_NDB successful */ } else { eM=get_err_text(nInQueue); goto errExit; } /* if..something in Queue */ } /* while..more blocks to read */ Fclose(fh); return; errExit: uiPrintf(uiH, uiPrERR, "dumpFile|%s", eM); Fclose(fh); } /* dumpFile */