Exemplo n.º 1
0
/* check if a socket is ready for IO */
static rsRetVal
IsReady(nsdsel_t *pNsdsel, nsd_t *pNsd, nsdsel_waitOp_t waitOp, int *pbIsReady)
{
	DEFiRet;
	nsdsel_gtls_t *pThis = (nsdsel_gtls_t*) pNsdsel;
	nsd_gtls_t *pNsdGTLS = (nsd_gtls_t*) pNsd;

	ISOBJ_TYPE_assert(pThis, nsdsel_gtls);
	ISOBJ_TYPE_assert(pNsdGTLS, nsd_gtls);
	if(pNsdGTLS->iMode == 1) {
		if(waitOp == NSDSEL_RD && gtlsHasRcvInBuffer(pNsdGTLS)) {
			*pbIsReady = 1;
			--pThis->iBufferRcvReady; /* one "pseudo-read" less */
			dbgprintf("nsdl_gtls: dummy read, decermenting %p->iBufRcvReady, now %d\n",
				   pThis, pThis->iBufferRcvReady);
			FINALIZE;
		}
		if(pNsdGTLS->rtryCall == gtlsRtry_handshake) {
			CHKiRet(doRetry(pNsdGTLS));
			/* we used this up for our own internal processing, so the socket
			 * is not ready from the upper layer point of view.
			 */
			*pbIsReady = 0;
			FINALIZE;
		}
		else if(pNsdGTLS->rtryCall == gtlsRtry_recv) {
			iRet = doRetry(pNsdGTLS);
			if(iRet == RS_RET_OK) {
				*pbIsReady = 0;
				FINALIZE;
			}
		}

		/* now we must ensure that we do not fall back to PTCP if we have
		 * done a "dummy" select. In that case, we know when the predicate
		 * is not matched here, we do not have data available for this
		 * socket. -- rgerhards, 2010-11-20
		 */
		if(pThis->iBufferRcvReady) {
			dbgprintf("nsd_gtls: dummy read, buffer not available for this FD\n");
			*pbIsReady = 0;
			FINALIZE;
		}
	}

	CHKiRet(nsdsel_ptcp.IsReady(pThis->pTcp, pNsdGTLS->pTcp, waitOp, pbIsReady));

finalize_it:
	RETiRet;
}
Exemplo n.º 2
0
/* check if a socket is ready for IO */
static rsRetVal
IsReady(nsdsel_t *pNsdsel, nsd_t *pNsd, nsdsel_waitOp_t waitOp, int *pbIsReady)
{
	DEFiRet;
	nsdsel_gtls_t *pThis = (nsdsel_gtls_t*) pNsdsel;
	nsd_gtls_t *pNsdGTLS = (nsd_gtls_t*) pNsd;

	ISOBJ_TYPE_assert(pThis, nsdsel_gtls);
	ISOBJ_TYPE_assert(pNsdGTLS, nsd_gtls);
	if(pNsdGTLS->iMode == 1) {
		if(waitOp == NSDSEL_RD && gtlsHasRcvInBuffer(pNsdGTLS)) {
			*pbIsReady = 1;
			FINALIZE;
		}
		if(pNsdGTLS->rtryCall != gtlsRtry_None) {
			CHKiRet(doRetry(pNsdGTLS));
			/* we used this up for our own internal processing, so the socket
			 * is not ready from the upper layer point of view.
			 */
			*pbIsReady = 0;
			FINALIZE;
		}
	}

	CHKiRet(nsdsel_ptcp.IsReady(pThis->pTcp, pNsdGTLS->pTcp, waitOp, pbIsReady));

finalize_it:
	RETiRet;
}
Exemplo n.º 3
0
void MSListener::doClose(void)
{
  if (fd()<0)
   {
     removeRetryTimer();
   }
  else
   {
     if (acceptChannel()!=0)
      {
        removeAcceptChannel();
        _deafTod=todsec();
	_listenedTime+=(deafTod()-listenTod());
      }
     delete [] _localName;
     _localName=(struct sockaddr *)(0);
     _localNamelen=0;
     MSChannel::fdsfresh(fd());
#if defined(MS_WINSOCK)
     if (::closesocket(fd())==SOCKET_ERROR)
#else
     if (::close(fd())<0)
#endif
      {
        MSMessageLog::warningMessage("MSListener::close(%s) : warning: close(%d)\n",name().string(),fd());
      }
     _fd=-1;
     _closeTod=todsec();
     _closeCount++;
     _openedTime+=(closeTod()-openTod());
   }
  if (retry()==MSListener::Yes) doRetry();
}
static CMD_HANDLER_FUNC(wsFileTxHandler)
{
    /**
     * If other node is running same software, we will just use its "file" handler:
     * buffer <offset> <num bytes> ...
     * commit <filename> <file offset> <num bytes from buffer>
     */
    char srcFile[128] = { 0 };
    char dstFile[128] = { 0 };
    int timeout = 1000;
    int addr = 0;
    FIL file;

    if (3 != cmdParams.scanf("%128s %128s %i", &srcFile[0], &dstFile[0], &addr)) {
        return false;
    }
    if (FR_OK != f_open(&file, srcFile, FA_OPEN_EXISTING | FA_READ)) {
        return false;
    }

    NordicStream &n = NordicStream::getInstance();
    n.setDestAddr(addr);

    char c = 0;
    char buffer[512];
    int expectedChecksum = 0;
    unsigned int bytesRead = 0;
    unsigned int fileOffset = 0;
    unsigned int retries = 0;
    unsigned int retriesMax = 3;
    STR_ON_STACK(response, 128);

    // Sorry for the dirty hack #define
    #define doRetry() ++retries; n.printf("\n"); n.flush(); while (n.getChar(&c, timeout)); goto retry

    output.printf("Transfer %s --> %i:%s\n", srcFile, addr, dstFile);
    while(FR_OK == f_read(&file, buffer, sizeof(buffer), &bytesRead) && bytesRead > 0)
    {
        retry:
        if (retries >= retriesMax) {
            break;
        }

        n.printf("file buffer 0 %i\n", bytesRead);
        n.flush();

        expectedChecksum = 0;
        for (unsigned int i=0; i < bytesRead; i++) {
            n.putChar(buffer[i]);
            expectedChecksum += buffer[i];
        }
        n.flush();

        // Confirm the checksum: response should be something like "Checksum: 123"
        n.gets((char*) response(), response.getCapacity(), timeout);
        response.eraseFirstWords(1);
        if ( (int)response != expectedChecksum) {
            output.printf("ERROR: Checksum Expected %i Actual %i\n", expectedChecksum, (int) response);
            doRetry();
        }

        // Make sure the file was written correctly, response should be "OK"
        n.printf("file commit %s %i %i\n", dstFile, fileOffset, bytesRead);
        n.flush();
        n.gets((char*) response(), response.getCapacity(), timeout);
        if (!response.containsIgnoreCase("ok")) {
            output.printf("ERROR: Remote node did not acknowledge file write (%s)\n", response());
            doRetry();
        }

        response = "";
        fileOffset += bytesRead;
        output.printf("Sent %i/%i\n", fileOffset, file.fsize);

        // Reset the retries such that only if we fail continuously we will give up
        if (retries > 0) {
            --retries;
        }
    }

    f_close(&file);
    return true;
}