/*************************************************************************** * Copyright (C) 1980-2005 Artur Wiebe * * artur@wiebenet.de * * * * 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., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "clientsocket.h" #include "serversocket.h" #include "config.h" #include #define JOC_TIMEOUT 3000 myClientSocket::myClientSocket(const QString& host, int port) { connect(this, SIGNAL(readyRead()), this, SLOT(slotRead())); connect(this, SIGNAL(connectionClosed()), this, SLOT(slotConnectionClosed())); connect(this, SIGNAL(connected()), this, SLOT(slotConnected())); m_state = sDisconnected; connectToHost(host, port); } myClientSocket::~myClientSocket() { } void myClientSocket::slotConnectionClosed() { m_state = sDisconnected; emit discInd(); } void myClientSocket::slotConnected() { m_state = sConnected; send(S_VER, APPNAME VERSION); } void myClientSocket::disc() { if(m_state!=sDisconnected) { send(S_DISC, ""); m_state = sDisconnected; } } void myClientSocket::joinOrCreate(int type) { if(m_state==sDisconnected) { return; } m_state = sJoinOrCreate; if(type==CLIENT_CREATE) send(S_CREATE, ""); else send(S_JOIN, ""); QTimer::singleShot(JOC_TIMEOUT, this, SLOT(slotJocTimeout())); } bool myClientSocket::sendText(const QString& data) { return send(S_DATA, data); } bool myClientSocket::send(const QString& cmd, const QString& data) { if(m_state == sDisconnected) { qDebug("myClientSocket::sendText: not yet connected."); return false; } QString line = QString("%1%2%3").arg(cmd).arg(data).arg(NETWORK_NL); int len = writeBlock(line.latin1(), line.length()); flush(); if(len < 0) { qDebug("myClientSocket::sendText: failed"); return false; } if((unsigned int)len != line.length()) { qDebug("myClientSocket::sendText (wanted/sent)(%d,%d", line.length(), len); return false; } return true; } void myClientSocket::slotRead() { while(canReadLine()) { QString data = readLine(); data.truncate(data.length()-1); switch(m_state) { case sConnected: if(data==C_ACK) emit connInd(); else disc(); break; case sJoinOrCreate: if(data==C_NACK) m_state = sConnected; else m_state = sConnectionOk; emit joc(data==C_ACK); break; case sConnectionOk: emit dataInd(data); break; default: qDebug("myClientSocket::slotRead() received while in wrong state."); break; } } } void myClientSocket::slotJocTimeout() { if(m_state==sJoinOrCreate || m_state==sDisconnected) { m_state = sConnected; emit joc(false); } }