Ejemplo n.º 1
0
//
//--tested, working--//
//bit of a misnomer. This waits to receive a packet and then stores it in a buffer
//it is important that this is called before any of the read or available commands
//are called. This does the actual work. Read, peek, etc. are just organizational.
//
int WiFiUDP::parsePacket()
{
    //
    //make sure we actually have a socket
    //
    if (_socketIndex == NO_SOCKET_AVAIL) {
        return 0;
    }
    
    //
    //the sl_select command blocks until something interesting happens or
    //it times out (current timeout set for 10 ms, the minimum)
    //
    SlTimeval_t timeout;
    timeout.tv_sec = 0;
    timeout.tv_usec = 10000;
    
    int socketHandle = WiFiClass::_handleArray[_socketIndex];
    
    SlFdSet_t readSocketHandles, errorSocketHandles;
    SL_FD_ZERO(&readSocketHandles);
    SL_FD_ZERO(&errorSocketHandles);
    SL_FD_SET(socketHandle, &readSocketHandles);
    SL_FD_SET(socketHandle, &errorSocketHandles);
    
    int iRet = sl_Select(socketHandle+1, &readSocketHandles, NULL, &errorSocketHandles, &timeout);
    if (iRet <= 0) {
        return 0;
    }

    //
    //Since we've reached this point, the sl_select command has indicated
    //that either we're going to get an error, or an immediate read
    //
    SlSockAddrIn_t  address = {0};
    int AddrSize = sizeof(address);
    int bytes = sl_RecvFrom(socketHandle, rx_buf, UDP_RX_PACKET_MAX_SIZE, NULL, (SlSockAddr_t*)&address, (SlSocklen_t*)&AddrSize);

    //
    //store the sender's address (sl_HtonX reorders bits to processor order)
    //!! Although this follows some examples (upd_socket), it goes against the
    //!! API documentation. The API maintains that the 5th arg to RecvFrom is not in/out
    //
    _remoteIP = address.sin_addr.s_addr;
    _remotePort = sl_Htons(address.sin_port);
    
    //
    //If an error occured, return 0, otherwise return the byte length of the packet
    //and reset the buffer index counter and fill level variables
    //
    if (bytes < 0) {
        rx_fillLevel = 0;
        return 0;
    } else {
        rx_currentIndex = 0;
        rx_fillLevel = bytes;
        return bytes;
    }
}
Ejemplo n.º 2
0
int cc3200_write(Network* n, unsigned char* buffer, int len, int timeout_ms) {
	SlTimeval_t timeVal;
	SlFdSet_t fdset;
	int rc = 0;
	int readySock;

	SL_FD_ZERO(&fdset);
	SL_FD_SET(n->my_socket, &fdset);

	timeVal.tv_sec = 0;
	timeVal.tv_usec = timeout_ms * 1000;
	do {
		readySock = sl_Select(n->my_socket + 1, NULL, &fdset, NULL, &timeVal);
	} while(readySock != 1);
	rc = sl_Send(n->my_socket, buffer, len, 0);
	return rc;
}
Ejemplo n.º 3
0
int cc3200_read(Network* n, unsigned char* buffer, int len, int timeout_ms) {
	SlTimeval_t timeVal;
	SlFdSet_t fdset;
	int rc = 0;
	int recvLen = 0;

	SL_FD_ZERO(&fdset);
	SL_FD_SET(n->my_socket, &fdset);

	timeVal.tv_sec = 0;
	timeVal.tv_usec = timeout_ms * 1000;
	if (sl_Select(n->my_socket + 1, &fdset, NULL, NULL, &timeVal) == 1) {
		do {
			rc = sl_Recv(n->my_socket, buffer + recvLen, len - recvLen, 0);
			recvLen += rc;
		} while(recvLen < len);
	}
	return recvLen;
}
Ejemplo n.º 4
0
//--tested, working--//
//--client and server side--//
int WiFiClient::available()
{
    //
    //don't do anything if not properly set up
    //
    if (_socketIndex == NO_SOCKET_AVAIL) {
        return 0;
    }
    
    //
    //if the buffer doesn't have any data in it or we've read everything
    //then receive some data
    //
    int bytesLeft = rx_fillLevel - rx_currentIndex;
    if (bytesLeft <= 0) {
        SlTimeval_t timeout;
        memset(&timeout, 0, sizeof(SlTimeval_t));
        timeout.tv_sec = 0;
        timeout.tv_usec = 500;
        SlFdSet_t readsds, errorsds;
        SL_FD_ZERO(&readsds);
        SL_FD_ZERO(&errorsds);
        SL_FD_SET(WiFiClass::_handleArray[_socketIndex], &readsds);
        SL_FD_SET(WiFiClass::_handleArray[_socketIndex], &errorsds);

//        sl_Select(WiFiClass::_handleArray[_socketIndex] + 1, &readsds, NULL, &errorsds, &timeout);
//        if(!SL_FD_ISSET(WiFiClass::_handleArray[_socketIndex], &readsds)) return 0;

        //
        //Receive any pending information into the buffer
        //if the connection has died, call stop() to make the object aware it's dead
        //
        int iRet = sl_Recv(WiFiClass::_handleArray[_socketIndex], rx_buffer, TCP_RX_BUFF_MAX_SIZE, 0);
        if ((iRet <= 0)  &&  (iRet != SL_EAGAIN)) {
            sl_Close(WiFiClass::_handleArray[_socketIndex]);

            WiFiClass::_portArray[_socketIndex] = -1;
            WiFiClass::_handleArray[_socketIndex] = -1;
            WiFiClass::_typeArray[_socketIndex] = -1;
            _socketIndex = NO_SOCKET_AVAIL;

            memset(rx_buffer, 0, TCP_RX_BUFF_MAX_SIZE);
            return 0;
        }
        
        //
        //receive successful. Reset rx index pointer and set buffer fill level indicator
        //(if SL_EAGAIN was received, the actual number of bytes received was zero, not -11)
        //
        rx_currentIndex = 0;
        rx_fillLevel = (iRet != SL_EAGAIN) ? iRet : 0;
        bytesLeft = rx_fillLevel - rx_currentIndex;
    }
    
    //
    //limit bytes left to >= 0
    //
    if (bytesLeft < 0) {
        bytesLeft = 0;
    }
    return bytesLeft;
}