Skip to content
Snippets Groups Projects
Select Git revision
  • e178e39808e9ff359789d7c6c88ef9d4e012ef13
  • master default protected
  • release/202005
  • release/202001
  • release/201912
  • release/201911
  • release/releaseWindowsTestOne
  • release/windowsReleaseTest
  • release/releaseTest
  • release/releaseWindowsTest
  • release/201910
  • release/qt/201910
  • release/windows-test/201910
  • release/201908
  • release/201906
  • release/201905
  • release/201904
  • release/201903
  • release/201902
  • release/201901
  • release/201812
  • 4.0.0
  • 2.2.0
  • 2.1.0
  • 2.0.1
  • 2.0.0
  • 1.4.1
  • 1.4.0
  • 1.3.0
  • 1.2.0
  • 1.1.0
31 results

TCPSessionIO.cpp

Blame
  • user avatar
    jpbl authored
    - We now have an error when we get a disconnection.
    e178e398
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    TCPSessionIO.cpp 3.20 KiB
    /**
     *  Copyright (C) 2004-2005 Savoir-Faire Linux inc.
     *  Author: Jean-Philippe Barrette-LaPierre
     *             <jean-philippe.barrette-lapierre@savoirfairelinux.com>
     *                                                                              
     *  This program is free software; you can redistribute it and/or modify
     *  it under the terms of the GNU General Public License as published by
     *  the Free Software Foundation; either version 2 of the License, or
     *  (at your option) any later version.
     *                                                                              
     *  This program is distributed in the hope that it will be useful,
     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     *  GNU General Public License for more details.
     *                                                                              
     *  You should have received a copy of the GNU General Public License
     *  along with this program; if not, write to the Free Software
     *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
     */
    
    #include "globals.h"
    #include "DebugOutput.hpp"
    #include "Requester.hpp"
    #include "TCPSessionIO.hpp"
    
    
    TCPSessionIO::TCPSessionIO(const QString &hostname, Q_UINT16 port)
      : mSocket(new QSocket(this))
      , mHostname(hostname)
      , mPort(port)
    {
      QObject::connect(mSocket, SIGNAL(readyRead()),
    		   this, SLOT(receive()));
      QObject::connect(mSocket, SIGNAL(connected()),
    		   this, SLOT(sendWaitingRequests()));
      QObject::connect(mSocket, SIGNAL(connected()),
    		   this, SIGNAL(connected()));
      QObject::connect(mSocket, SIGNAL(connectionClosed()),
    		   this, SIGNAL(disconnected()));
      QObject::connect(mSocket, SIGNAL(error(int)),
    		   this, SLOT(error(int)));
      QObject::connect(mSocket, SIGNAL(error(int)),
    		   this, SIGNAL(disconnected()));
    }
    
    TCPSessionIO::~TCPSessionIO()
    {}
    
    void 
    TCPSessionIO::error(int err)
    {
      DebugOutput::instance() << QObject::tr("TCPSessionIO: Error number %1. \n")
        .arg(err);
      mSocket->close();
    }
    
    void 
    TCPSessionIO::receive()
    {
      QString s;
      receive(s);
      Requester::instance().receiveAnswer(s);
    }
    
    void
    TCPSessionIO::connect()
    {
      DebugOutput::instance() << QObject::tr("TCPSessionIO: Tring to connect to %1:%2.\n")
        .arg(mHostname)
        .arg(mPort);
      mSocket->connectToHost(mHostname, mPort);
    }
    
    void
    TCPSessionIO::sendWaitingRequests()
    {
      _debug("TCPSessionIO: Connected.\n");
      QTextStream stream(mSocket);
      QMutexLocker guard(&mStackMutex);
      while(mSocket->state() == QSocket::Connected &&
    	mStack.size() > 0) {
        stream << *mStack.begin();
        mStack.pop_front();
      }
    }
    
    void
    TCPSessionIO::send(const QString &request)
    {
      QTextStream stream(mSocket);
      if(mSocket->state() == QSocket::Connected) {
        DebugOutput::instance() << QObject::tr("TCPSessioIO: Sending request to sflphone: %1")
          .arg(request);
        stream << request;
      }
      else {
        mStackMutex.lock();
        mStack.push_back(request);
        mStackMutex.unlock();
      }
    }
    
    void
    TCPSessionIO::receive(QString &answer)
    {
      if(mSocket->isReadable()) {
        QTextStream stream(mSocket);
        answer = stream.readLine();
        DebugOutput::instance() << QObject::tr("TCPSessionIO: Received answer from sflphone: %1\n")
          .arg(answer);
      }
    }