Esempio n. 1
0
    void Connection::onWrite( )
    {
        TRACE_ENTERLEAVE( );

        if ( !m_initialized )
        {
            //
            //  set timeouts if needed
            //
            if ( m_thread.server( ).getConnectionReadTimeout( ) > 0 )
            {
                setReadTimeout( m_thread.server( ).getConnectionReadTimeout( ) );
            }

            if ( m_thread.server( ).getConnectionWriteTimeout( ) > 0 )
            {
                setWriteTimeout( m_thread.server( ).getConnectionWriteTimeout( ) );
            }
        }

        if ( m_close )
        {
            disable( );
            onClose( );
        }
    }
Esempio n. 2
0
bool mdtTcpSocket::open(mdtPortConfig &cfg)
{
  // Close previous opened connection
  close();
  // Open new connection
  lockMutex();
  qDebug() << "Open ....";

  // Set R/W timeouts
  setReadTimeout(cfg.readTimeout());
  setWriteTimeout(cfg.writeTimeout());

  return mdtAbstractPort::open(cfg);
}
Esempio n. 3
0
void TcpConnection::startAsyncWriteResponse
    (const std::vector<asio::const_buffer>& buffers,
     int timeout)
{
  LOG_DEBUG(socket().native() << ": startAsyncWriteResponse");
  setWriteTimeout(timeout);

  boost::shared_ptr<TcpConnection> sft 
    = boost::dynamic_pointer_cast<TcpConnection>(shared_from_this());
  asio::async_write(socket_, buffers,
		    boost::bind(&Connection::handleWriteResponse,
				sft,
				asio::placeholders::error));
}
Esempio n. 4
0
void SslConnection::startAsyncWriteResponse
    (ReplyPtr reply,
     const std::vector<asio::const_buffer>& buffers,
     int timeout)
{
  if (state_ & Writing) {
    LOG_DEBUG(socket().native() << ": state_ = " << state_);
    stop();
    return;
  }

  setWriteTimeout(timeout);

  boost::shared_ptr<SslConnection> sft 
    = boost::dynamic_pointer_cast<SslConnection>(shared_from_this());
  asio::async_write(socket_, buffers,
		    strand_.wrap
		    (boost::bind(&SslConnection::handleWriteResponse,
				 sft,
				 reply,
				 asio::placeholders::error,
				 asio::placeholders::bytes_transferred)));
}
Esempio n. 5
0
bool mdtSerialPortPosix::open(mdtSerialPortConfig &cfg)
{
  QString strNum;

  // Close previous opened device
  this->close();
  // Try to open port
  //  O_RDWR : Read/write access
  //  O_NOCTTY: not a terminal
  //  O_NDELAY: ignore DCD signal
  lockMutex();
  // In read only mode, we handle no lock
  if(cfg.readOnly()){
    pvFd = ::open(pvName.toStdString().c_str(), O_RDONLY | O_NOCTTY | O_NONBLOCK);
  }else{
    pvFd = pvPortLock->openLocked(pvName, O_RDWR | O_NOCTTY | O_NONBLOCK);
  }
  if(pvFd < 0){
    pvPortLock->unlock();
    mdtError e(MDT_UNDEFINED_ERROR, "Unable to open port: " + pvName, mdtError::Error);
    e.setSystemError(errno, strerror(errno));
    MDT_ERROR_SET_SRC(e, "mdtSerialPortPosix");
    e.commit();
    unlockMutex();
    return false;
  }
  // Get current config and save it to pvOriginalTermios
  if(tcgetattr(pvFd, &pvOriginalTermios) < 0){
    ::close(pvFd);
    pvFd = -1;
    pvPortLock->unlock();
    mdtError e(MDT_UNDEFINED_ERROR, "tcgetattr() failed, " + pvName + " is not a serial port, or is not available", mdtError::Error);
    e.setSystemError(errno, strerror(errno));
    MDT_ERROR_SET_SRC(e, "mdtSerialPortPosix");
    e.commit();
    unlockMutex();
    return false;
  }
  // Get current system config on "work copy"
  if(tcgetattr(pvFd, &pvTermios) < 0){
    ::close(pvFd);
    pvFd = -1;
    pvPortLock->unlock();
    mdtError e(MDT_UNDEFINED_ERROR, "tcgetattr() failed, " + pvName + " is not a serial port, or is not available", mdtError::Error);
    e.setSystemError(errno, strerror(errno));
    MDT_ERROR_SET_SRC(e, "mdtSerialPortPosix");
    e.commit();
    unlockMutex();
    return false;
  }
  // Set baud rate
  if(!setBaudRate(cfg.baudRate())){
    ::close(pvFd);
    pvFd = -1;
    pvPortLock->unlock();
    strNum.setNum(cfg.baudRate());
    mdtError e(MDT_UNDEFINED_ERROR, "unsupported baud rate '" + strNum + "' for port " + pvName, mdtError::Error);
    e.setSystemError(errno, strerror(errno));
    MDT_ERROR_SET_SRC(e, "mdtSerialPortPosix");
    e.commit();
    unlockMutex();
    return false;
  }
  // Set local mode and enable the receiver
  pvTermios.c_cflag |= (CLOCAL | CREAD);
  // Set data bits
  if(!setDataBits(cfg.dataBitsCount())){
    ::close(pvFd);
    pvFd = -1;
    pvPortLock->unlock();
    strNum.setNum(cfg.dataBitsCount());
    mdtError e(MDT_UNDEFINED_ERROR, "unsupported data bits count '" + strNum + "' for port " + pvName, mdtError::Error);
    MDT_ERROR_SET_SRC(e, "mdtSerialPortPosix");
    e.commit();
    unlockMutex();
    return false;
  }
  // Set stop bits
  if(!setStopBits(cfg.stopBitsCount())){
    ::close(pvFd);
    pvFd = -1;
    pvPortLock->unlock();
    strNum.setNum(cfg.stopBitsCount());
    mdtError e(MDT_UNDEFINED_ERROR, "unsupported stop bits count '" + strNum + "' for port " + pvName, mdtError::Error);
    MDT_ERROR_SET_SRC(e, "mdtSerialPortPosix");
    e.commit();
    unlockMutex();
    return false;
  }
  // Set parity
  setParity(cfg.parity());
  // Set flow control
  setFlowCtlRtsCts(cfg.flowCtlRtsCtsEnabled());
  setFlowCtlXonXoff(cfg.flowCtlXonXoffEnabled(), cfg.xonChar(), cfg.xoffChar());
  // Set raw data mode
  pvTermios.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
  pvTermios.c_oflag &= ~OPOST;
  // Apply the setup
  if(tcsetattr(pvFd, TCSANOW, &pvTermios) < 0){
    ::close(pvFd);
    pvFd = -1;
    pvPortLock->unlock();
    mdtError e(MDT_UNDEFINED_ERROR, "unable to apply configuration for port " + pvName, mdtError::Error);
    e.setSystemError(errno, strerror(errno));
    MDT_ERROR_SET_SRC(e, "mdtSerialPortPosix");
    e.commit();
    unlockMutex();
    return false;
  }
  // Check if configuration could really be set
  if(!checkConfig(cfg)){
    ::close(pvFd);
    pvFd = -1;
    pvPortLock->unlock();
    unlockMutex();
    return false;
  }

  // Set the read/write timeouts
  setReadTimeout(cfg.readTimeout());
  setWriteTimeout(cfg.writeTimeout());

  return mdtAbstractPort::open(cfg);
}