예제 #1
0
bool CPPTcpServerSocket::listen(short port, int maxConnections){
    std::lock_guard<std::mutex> lockR(recvLock), lockS(sendLock);
    struct sockaddr_in local;
    memset(&local, 0, sizeof(sockaddr_in));
    m_port = port;
    local.sin_family = AF_INET;
    local.sin_port = htons(port);
    local.sin_addr.s_addr = htonl(INADDR_ANY);
    
    if (!open())
        return false;

    int on=1;
    if (!setSocketOption(SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on), false))
        return false;

    // Bind socket to local address
    if(::bind(m_sock, (struct sockaddr *)&local, sizeof(struct sockaddr_in))==-1) {
        return false;
    }

    // Create queue for client connection requests
    if(::listen(m_sock, maxConnections)==-1) {
        return false;
    }
    
    m_port = port;
    return true;
}
/*! The main work of the component is carried out here
*
*  Take a DataSet from the input buffer and send to the usrp
*/
void Dvbt1UsrpTxComponent::process()
{
  //Get a DataSet from the input DataBuffer
  DataSet< complex<float> >* readDataSet = NULL;
  inBuf_->getReadData(readDataSet);

  size_t insize = readDataSet->data.size();

  // check buffers
  int remSize = insize;
  while(remSize > 0)
  {
    bool have_to_wait = true;
    int availSize = bufferSize_x - fulls_[currentWrite_];
    if(availSize > 0)
    {
      // there is room in this buffer
      dbgprintf("filling W(%d)...\n", currentWrite_);
      DUMP_STATUS();
      int dasize = min(availSize, remSize);
      copy(&(readDataSet->data[insize - remSize]), &(readDataSet->data[insize - remSize + dasize]), &(bufs_[currentWrite_][fulls_[currentWrite_]]));
      fulls_[currentWrite_] += dasize;
      remSize -= dasize;
    }
    else
    {
      {
        // try to change buffer
        boost::lock_guard<boost::mutex> lock(mut_);
        int nextWrite_ = currentWrite_ == (numBuffers_x - 1) ? 0 : (currentWrite_ + 1);
        dbgprintf("looking for W(%d)...\n", nextWrite_);
        DUMP_STATUS();
        if(nextWrite_ != currentRead_)
        {
          // update buffer
          currentWrite_ = nextWrite_;
          // notify the reader that the writer has done something
          condW_.notify_one();
          dbgprintf("notified a write\n");
          have_to_wait = false;
        }
      }

      // wait for a free read buffer to write into
      if(have_to_wait)
      {
        dbgprintf("awaiting a read...\n");
        boost::unique_lock<boost::mutex> lockR(mutR_);
        condR_.wait(lockR);
        dbgprintf("awaited a read\n");
      }
    }
  }

  //Release the DataSet
  inBuf_->releaseReadData(readDataSet);
}
예제 #3
0
int CPPTcpServerSocket::accept(int timeout){
    std::lock_guard<std::mutex> lockR(recvLock), lockS(sendLock);
    if (!isOpen()) return -1;
    struct pollfd pfd = {m_sock, POLLIN|POLLPRI, 0};
    if (poll(&pfd, 1, timeout) > 0){
        struct sockaddr_in remote;
        socklen_t size = sizeof(struct sockaddr_in);
        return ::accept(m_sock, (struct sockaddr *)&remote, &size);
    }
    return -1;
}
예제 #4
0
bool CPPTcpClientSocket::connect(short port, unsigned int addr, int timeout, bool spin){
    std::lock_guard<std::mutex> lockR(recvLock), lockS(sendLock);
    m_port = port;
    m_addr = addr;
    struct sockaddr_in remote;
    memset(&remote, 0, sizeof(remote));
    remote.sin_family = AF_INET;
    remote.sin_addr.s_addr = m_addr;
    remote.sin_port = htons(m_port);
    double tremaining = timeout;
    auto start = std::chrono::system_clock::now();
    bool success = false;    
    do{
        if (!open()){
            break;
        }
        setBlocking(false, false);
        if (::connect(m_sock, (struct sockaddr *)&remote, sizeof(remote)) == 0){
            success = true;
            break;
        }
        if (errno != EINPROGRESS){
            break;
        }
        struct pollfd pfd = {m_sock, POLLWRNORM, 0};
        if (poll(&pfd, 1, (int)tremaining) > 0){
            int optionValue = -1;
            socklen_t optionLength = sizeof(optionValue);
            if (getSocketOption(SOL_SOCKET, SO_ERROR, (void *)&optionValue, &optionLength, false) && optionValue == 0){
                success = true;
                break;
            }
        }
        std::chrono::duration<double, std::milli> dur = std::chrono::system_clock::now() - start;
        tremaining = timeout - dur.count();
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
        ::shutdown(m_sock, SHUT_RDWR);
        ::close(m_sock);
        m_sock = -1;
        if (!spin) break;
        
    }while((tremaining > 0.0 || timeout < 0) && !sigClose);
    setBlocking(true, false);
    if (success) return true;
    
    if (m_sock != -1){
        ::shutdown(m_sock, SHUT_RDWR);
        ::close(m_sock);
        m_sock=-1;
    }
    return false;
}