Starting to write networking

parent c29db5c4
......@@ -36,7 +36,8 @@
#include "ipcmsg.h"
#include <iostream>
#include "skproto.h"
#include <QTcpSocket>
#include <QtNetwork>
void SkyliveProtocol::startPlugin()
......@@ -46,14 +47,44 @@ void SkyliveProtocol::startPlugin()
}
void SkyliveProtocol::readFromNetwork()
{
std::cout << "Received From Skylive Serve";
QDataStream in(tcpSocket);
in.setVersion(QDataStream::Qt_4_0);
if(blockSize == 0)
{
if(tcpSocket->bytesAvailable() < (int)sizeof(quint16))
return;
in >> blockSize;
}
if(tcpSocket->bytesAvailable() < blockSize)
return;
QString receivedTCP;
in >> receivedTCP;
std::cout << "Received From Skylive Server" << receivedTCP.toStdString() << std::endl;
}
void SkyliveProtocol::handle_connect(SKMessage::SKMessage msg)
{
std::cout << "SkyliveProtocol connect : " << msg.handle.toStdString() << std::endl;
tcpSocket = new QTcpSocket(this);
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readFromNetwork()));
connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError)));
blockSize=0;
tcpSocket->abort();
tcpSocket->connectToHost("localhost", 8081);
}
void SkyliveProtocol::receiveMessage(SKMessage::SKMessage msg)
{
std::cout << "SkyliveProtocol msg received: " << msg.handle.toStdString() << std::endl;
if(_handlers.contains(msg.handle))
{
SKHandlerFunction mf =_handlers[msg.handle];
(this->*mf)(msg);
}
}
void SkyliveProtocol::sendMessage(SKMessage::SKMessage msg)
......@@ -62,18 +93,25 @@ void SkyliveProtocol::sendMessage(SKMessage::SKMessage msg)
}
void SkyliveProtocol::execute(SKMessage::SKMessage &msg)
{
if(_handlers.contains(msg.handle))
{
SKHandlerFunction mf =_handlers[msg.handle];
(this->*mf)(msg);
}
}
void SkyliveProtocol::registerHandler(QString type, SKHandlerFunction handler)
{
_handlers[type] = handler;
}
void SkyliveProtocol::displayError(QAbstractSocket::SocketError socketError)
{
switch (socketError) {
case QAbstractSocket::RemoteHostClosedError:
break;
case QAbstractSocket::HostNotFoundError:
std::cout << "Host not found" << std::endl;
break;
case QAbstractSocket::ConnectionRefusedError:
std::cout << "connection refused" << std::endl;
break;
default:
std::cout << "networ error: " << std::endl;
}
}
......@@ -35,9 +35,12 @@
#include <QObject>
#include <QtPlugin>
#include <QHash>
#include <QTcpSocket>
#include <QNetworkSession>
#include "pluginsinterfaces.h"
#include "ipcmsg.h"
class SkyliveProtocol;
typedef void (SkyliveProtocol::*SKHandlerFunction)(SKMessage::SKMessage);
......@@ -49,13 +52,19 @@ class SkyliveProtocol : public QObject, SkylivexPluginInterface
private:
QHash<QString, SKHandlerFunction> _handlers;
QTcpSocket *tcpSocket;
QNetworkSession *networkSession;
quint16 blockSize;
public:
void startPlugin();
void sendMessage(SKMessage::SKMessage msg);
void registerHandler(QString type, SKHandlerFunction handler);
void execute(SKMessage::SKMessage &msg);
void handle_connect(SKMessage::SKMessage msg);
private slots:
void sessionOpened();
void readFromNetwork();
void displayError(QAbstractSocket::SocketError);
public slots:
void receiveMessage(SKMessage::SKMessage msg);
......
......@@ -78,8 +78,8 @@ int main(int argc, char *argv[])
skxprocess->start();
// connect core with the mainwin
//QObject::connect(skx, SIGNAL(msgForMainWin(std::string&)), &mainw, SLOT(msgFromCore(std::string&)), Qt::QueuedConnection);
QObject::connect(skx, SIGNAL(msgForMainWin(SKMessage::SKMessage&)), &mainw, SLOT(msgFromCore(SKMessage::SKMessage&)));
QObject::connect(&mainw, SIGNAL(putMessage(SKMessage::SKMessage&)), skx, SLOT(receiveFromMainWin(SKMessage::SKMessage&)));
// and then.. go!
return skylivexapp.exec();
......
......@@ -76,7 +76,7 @@ class MainWin : public QWebView
void msgFromCore(SKMessage::SKMessage &msg);
signals:
void putMessage(SKMessage::SKMessage msg);
void putMessage(SKMessage::SKMessage &msg);
};
......
......@@ -122,10 +122,12 @@ void SkyliveX::sendMessage(SKMessage::SKMessage &msg)
void SkyliveX::receiveFromMainWin(SKMessage::SKMessage &msg)
{
emit msgForPlugins(msg);
std::cout << "received from MainWin " << msg.handle.toStdString() << std::endl;
//emit msgForPlugins(msg);
}
void SkyliveX::receiveFromPlugins(SKMessage::SKMessage msg)
{
std::cout << "received from plugins " << msg.handle.toStdString() << std::endl;
sendMessage(msg);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment