Ejemplo n.º 1
0
void TcpSocketRxComponent::writeOutput()
{
  std::size_t size;

  try
  {
    if(!connected_)
    {
      //Listen for connection if necessary
      acceptor_->accept(*socket_);
      connected_ = true;
    }

    //Get data from socket
    //size = socket_->receive(boost::asio::buffer(d_buffer, x_bufferSize));
    size = boost::asio::read(*socket_, boost::asio::buffer(buffer_, bufferSize_x));
  }
  catch(boost::system::system_error &e)
  {
    LOG(LERROR) << "Error listening on socket: " << e.what();
    boost::this_thread::sleep(boost::posix_time::milliseconds(5000));
    return;
  }

  //Check that we've an integer number of data types in the datagram
  float f = size/(float)sizeof(T);
  float intpart, rem;
  rem = modf(f, &intpart);
  if(rem != 0)
  {
    LOG(LERROR) << "Did not receive an integer number of elements. Input size = " << size;
  }
  int numT = (int)intpart;

  //Get the output buffer
  WriteBuffer< T >* outBuf = castToType<T>(outputBuffers[0]);
  DataSet<T>* writeDataSet = NULL;
  outBuf->getWriteData(writeDataSet, numT);

  //Copy data into output
  T* bufT = (T*)buffer_;
  copy(bufT, bufT+numT, writeDataSet->data.begin());

  //Release the buffer
  outBuf->releaseWriteData(writeDataSet);
}
Ejemplo n.º 2
0
void UdpSocketRxComponent::writeOutput()
{
  if(!bStopping_)
  {
    //Get data from socket
    std::size_t size;
    try
    {
      udp::endpoint sender_endpoint;
      size = socket_->receive_from(boost::asio::buffer(buffer_, bufferSize_x), sender_endpoint);
    }
    catch(boost::system::system_error &e)
    {
      if(!bStopping_)
      {
        LOG(LERROR) << "Error receiving from socket: " << e.what();
      }
      return;
    }

    //Check that we've an integer number of data types in the datagram
    float f = size/(float)sizeof(T);
    float intpart, rem;
    rem = modf(f, &intpart);
    if(rem != 0)
    {
      LOG(LERROR) << "Did not receive an integer number of elements - data will be lost";
    }
    int numT = (int)intpart;

    //Get the output buffer
    WriteBuffer< T >* outBuf = castToType<T>(outputBuffers[0]);
    DataSet<T>* writeDataSet = NULL;
    outBuf->getWriteData(writeDataSet, numT);

    //Copy data into output
    T* bufT = (T*)buffer_;
    copy(bufT, bufT+numT, writeDataSet->data.begin());

    //Release the buffer
    outBuf->releaseWriteData(writeDataSet);
  }
}
void FileRawReaderComponent::readBlock()
{
  //Get the output buffer & work directly on it
  WriteBuffer< T >* outBuf = castToType<T>(outputBuffers[0]);
  DataSet<T>* writeDataSet = NULL;
  outBuf->getWriteData(writeDataSet, blockSize_x);

  char *bytebuf = reinterpret_cast<char*>(&writeDataSet->data[0]);
  ifstream::pos_type toread = blockSize_x * sizeof(T);

  //Read a block (loop if necessary)
  while( toread > 0 )
  {
    hInFile_.read(bytebuf, toread);
    toread -= hInFile_.gcount();
    bytebuf += hInFile_.gcount();
    if( hInFile_.eof() )
    {
      hInFile_.clear();
      hInFile_.seekg(0, ios::beg);
    }
  }

  if (sizeof(T) > 1)
  {
    //Convert endianess
    if (endian_x == "native")
      ; // nothing to do
    else if (endian_x == "little")
      transform(writeDataSet->data.begin(), writeDataSet->data.end(),
                writeDataSet->data.begin(), lit2sys<T> );
    else if (endian_x == "big")
      transform(writeDataSet->data.begin(), writeDataSet->data.end(),
                writeDataSet->data.begin(), big2sys<T> );
  }

  outBuf->releaseWriteData(writeDataSet);
}