serversocket.cpp
Go to the documentation of this file.
00001 #include "serversocket.h"
00002 #include <QDebug>
00003 
00004 ServerSocket::ServerSocket(int socketDescriptor, QObject *parent)
00005     : QObject(parent)
00006 {
00007     m_bSendingData = false;
00008 
00009     if (!socket.setSocketDescriptor(socketDescriptor)) {
00010         qCritical() << socket.errorString();
00011         return;
00012     }
00013 
00014     connect(&socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
00015     connect(&socket, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
00016 }
00017 
00018 QHostAddress ServerSocket::peerAddress() const
00019 {
00020     return socket.peerAddress();
00021 }
00022 
00023 void ServerSocket::readyRead()
00024 {
00025     QList<QByteArray> cmds = socket.readAll().split('\n');
00026     for (int i = 0; i < cmds.count(); i++){
00027         QString cmd = cmds[i];
00028         emit read(cmd);
00029     }
00030 
00031     /*
00032     char buf[256];
00033     qint64 lineLength = socket.read(buf, sizeof(buf));
00034     if (lineLength == -1) {
00035         qCritical() << "reading from client failed.";
00036     }
00037     else if (lineLength > 0){
00038         buf[lineLength-1] = '\0';
00039         emit read(QString(buf));
00040     }
00041     */
00042 }
00043 
00044 void ServerSocket::clientDisconnected()
00045 {
00046     qDebug() << "disconnected.";
00047     emit disconnected();
00048 }
00049 
00050 void ServerSocket::SendStart()
00051 {
00052     _Send( "BEGIN\n\n" );
00053     m_bSendingData = true;
00054 }
00055 
00056 void ServerSocket::SendData( const void *data, int len )
00057 {
00058     if ( m_bSendingData ){
00059         _Send( QString("DATA %1\n\n").arg(len) );
00060         _Send( data, len );
00061     }
00062 }
00063 
00064 void ServerSocket::SendEnd()
00065 {
00066     if ( m_bSendingData ){
00067         _Send( "END\n\n" );
00068         m_bSendingData = false;
00069     }
00070 }
00071 
00072 void ServerSocket::SendCancel()
00073 {
00074     if ( m_bSendingData ){
00075         _Send( "CANCEL\n\n" );
00076         m_bSendingData = false;
00077     }
00078 }
00079 
00080 void ServerSocket::SendKeyPressed( int val )
00081 {
00082     _Send( QString("KEY_PRESSED %1\n\n").arg(val) );
00083 }
00084 
00085 void ServerSocket::SendEpdOn()
00086 {
00087     _Send( "EPD_ON\n\n" );
00088 }
00089 
00090 void ServerSocket::SendEpdOff()
00091 {
00092     _Send( "EPD_OFF\n\n" );
00093 }
00094 
00095 void ServerSocket::_Send( const QString& str )
00096 {
00097     _Send( str.toAscii(), str.length() );
00098 }
00099 
00100 void ServerSocket::_Send( const void *data, int len )
00101 {
00102     socket.write((const char*)data, len);
00103 }


rospeex_audiomonitor
Author(s): Komei Sugiura
autogenerated on Thu Jun 6 2019 18:53:06