Example #1
0
void ByteArray::copyFrom(ByteArray & buffer)
{
  if (buffer.getBufferSize() != 0)
  {
    this->setBufferSize(buffer.getBufferSize());
    memcpy(this->getRawDataPtr(), buffer.getRawDataPtr(), this->buffer_size_);
  }
  else
  {
    LOG_WARN("Byte array copy not performed, buffer to copy is empty");
  }
}
Example #2
0
bool UdpServer::makeConnect()
{
  ByteArray send;
  char sendHS = this->CONNECT_HANDSHAKE;
  char recvHS = 0;
  int bytesRcvd = 0;
  const int timeout = 1000;  // Time (ms) between handshake sends
  bool rtn = false;
  
  send.load((void*)&sendHS, sizeof(sendHS));
    
  if (!this->isConnected())
  {
    this->setConnected(false);
    
    // Listen for handshake.  Once received, break
    // listen loop.
    do
    {
      ByteArray recv;
      recvHS = 0;
      if (this->isReadyReceive(timeout))
      {
        bytesRcvd = this->rawReceiveBytes(this->buffer_, 0);
        
        if (bytesRcvd > 0)
        {
          LOG_DEBUG("UDP server received %d bytes while waiting for handshake", bytesRcvd);
          recv.init(&this->buffer_[0], bytesRcvd);
          recv.unload((void*)&recvHS, sizeof(recvHS));
        }
      }
      
    }
    while(recvHS != sendHS);
    
    // Send a reply handshake
    this->rawSendBytes(send.getRawDataPtr(), send.getBufferSize());
    this->setConnected(true);
    rtn = true;
    
  }
  else
  {
    LOG_WARN("Tried to connect when socket already in connected state");
    rtn = true;
  }

  return rtn;
}
bool SimpleSocket::sendBytes(ByteArray & buffer)
{
  int rc = this->SOCKET_FAIL;
  bool rtn = false;

  if (this->isConnected())
  {
    // Nothing restricts the ByteArray from being larger than the what the socket
    // can handle.
    if (this->MAX_BUFFER_SIZE > (int)buffer.getBufferSize())
    {
    
      rc = rawSendBytes(buffer.getRawDataPtr(), buffer.getBufferSize());
      if (this->SOCKET_FAIL != rc)
      {
        rtn = true;
      }
      else
      {
        rtn = false;
        logSocketError("Socket sendBytes failed", rc);
      }
      
      }
    else
    {
      LOG_ERROR("Buffer size: %u, is greater than max socket size: %u", buffer.getBufferSize(), this->MAX_BUFFER_SIZE);
      rtn = false;
    }

  }
  else
  {
    rtn = false;
    LOG_WARN("Not connected, bytes not sent");
  }

  if (!rtn)
    {
      this->setConnected(false);
    }

  return rtn;
  
}
bool UdpClient::makeConnect()
{
  ByteArray send;
  char sendHS = this->CONNECT_HANDSHAKE;
  char recvHS = 0;
  bool rtn = false;
  const int timeout = 1000;  // Time (ms) between handshake sends
  int bytesRcvd = 0;
  
  if (!this->isConnected())
  {
    this->setConnected(false);
    send.load((void*)&sendHS, sizeof(sendHS));
  
    do
    {
      ByteArray recv;
      recvHS = 0;
      LOG_DEBUG("UDP client sending handshake");
      this->rawSendBytes(send.getRawDataPtr(), send.getBufferSize());
      if (this->isReadyReceive(timeout))
      {
        bytesRcvd = this->rawReceiveBytes(this->buffer_, 0);
 	LOG_DEBUG("UDP client received possible handshake");	
        recv.init(&this->buffer_[0], bytesRcvd);
        recv.unload((void*)&recvHS, sizeof(recvHS));
      }
    }
    while(recvHS != sendHS);
    LOG_INFO("UDP client connected");
    rtn = true;
    this->setConnected(true);
    
  }
  else
  {
    rtn = true;
    LOG_WARN("Tried to connect when socket already in connected state");
  }

  return rtn;
}
Example #5
0
bool ByteArray::load(ByteArray &value)
{
  LOG_COMM("Executing byte array load through byte array");
  return this->load(value.getRawDataPtr(), value.getBufferSize());
}