socket write implemented

parent f7c2a279
...@@ -37,6 +37,7 @@ ...@@ -37,6 +37,7 @@
#include <iostream> #include <iostream>
#include "skproto.h" #include "skproto.h"
#include <QTcpSocket> #include <QTcpSocket>
#include <QByteArray>
#include <QtNetwork> #include <QtNetwork>
...@@ -53,21 +54,113 @@ void SkyliveProtocol::startPlugin() ...@@ -53,21 +54,113 @@ void SkyliveProtocol::startPlugin()
} }
void SkyliveProtocol::sendPacket(QString &cmd, QList<QString> &paramlist)
{
QString params;
for(int i=0;i<paramlist.size();i++)
{
if(i>0)
params.append(PARAM_SEPARATOR);
params.append(paramlist[i]);
}
sendPacket(cmd, params);
}
void SkyliveProtocol::sendPacket(const char* cmd, const char* params)
{
QString pcmd(cmd);
QString pparams(params);
sendPacket(pcmd, pparams);
}
void SkyliveProtocol::sendPacket(QString &cmd, QString &params)
{
SKProtoMsg pkt;
pkt.cmd=cmd;
pkt.params=params;
sendPacket(pkt);
}
void SkyliveProtocol::sendPacket(SKProtoMsg &pkt)
{
QByteArray skpkt;
if(tcpSocket->isValid())
{
skpkt.append(PROTO_START);
skpkt.append(pkt.cmd);
skpkt.append(CMD_END);
skpkt.append(pkt.params);
pkt.computed_crc=0;
for(int i=1;i<skpkt.size();i++)
{
pkt.computed_crc+=static_cast<int>(skpkt[i]);
}
skpkt.append(PARAM_END);
skpkt.append(QString::number(pkt.computed_crc));
skpkt.append(PROTO_END);
tcpSocket->write(skpkt);
std::cout << "Packet sent: " << pkt.cmd.toStdString() <<std::endl;
} else {
std::cout << "Cannote send packet: " << pkt.cmd.toStdString() <<std::endl;
}
}
void SkyliveProtocol::processPackets() void SkyliveProtocol::processPackets()
{ {
if(!protoQueue.isEmpty()) if(!protoQueue.isEmpty())
{ {
SKProtoMsg pkt; SKProtoMsg pkt;
pkt = protoQueue.dequeue(); pkt = protoQueue.dequeue();
QString cmd(pkt.cmd); std::cout << "Packet in Queue CRC " << pkt.crc.toInt() << " computed CRC " << pkt.computed_crc << std::endl;
std::cout << "Packages in Queue: " << cmd.toStdString() <<std::endl; if(pkt.crc.toInt()==pkt.computed_crc)
{
std::cout << "Packet CRC OK command: " << pkt.cmd.toStdString() <<std::endl;
if(pkt.cmd=="LOGIN")
{
}
else if(pkt.cmd=="PING")
{
sendPacket("PONG", "NIL");
}
if(pkt.cmd=="CPUBLIC")
{
}
else if(pkt.cmd=="CPRIVAT")
{
}
else if(pkt.cmd=="STATUS")
{
}
else
{
std::cout << "Unknown command from server" <<std::endl;
}
} else {
std::cout << "Packet in Queue has invalid CRC. Discard it" <<std::endl;
}
} else { } else {
if(pktTimer->isActive()) if(pktTimer->isActive())
pktTimer->stop(); pktTimer->stop();
} }
} }
void SkyliveProtocol::clearPkt()
{
protoMsg.computed_crc=0;
protoMsg.cmd.clear();
protoMsg.params.clear();
protoMsg.crc.clear();
}
void SkyliveProtocol::readFromNetwork() void SkyliveProtocol::readFromNetwork()
{ {
char c; char c;
...@@ -85,27 +178,23 @@ void SkyliveProtocol::readFromNetwork() ...@@ -85,27 +178,23 @@ void SkyliveProtocol::readFromNetwork()
switch(c) switch(c)
{ {
case PROTO_START: case PROTO_START:
{ clearPkt();
SKProtoMsg protoMsg;
SM_TCPCLIENT=COMMAND; SM_TCPCLIENT=COMMAND;
break; break;
}
default: default:
break; break;
} }
break; break;
case COMMAND: case COMMAND:
protoMsg.computed_crc+=c; protoMsg.computed_crc+=static_cast<int>(c);
switch(c) switch(c)
{ {
case CMD_END: case CMD_END:
SM_TCPCLIENT=PARAMS; SM_TCPCLIENT=PARAMS;
break; break;
case PROTO_START: case PROTO_START:
{ clearPkt();
SKProtoMsg protoMsg;
break; break;
}
case PARAM_END: case PARAM_END:
case PROTO_END: case PROTO_END:
SM_TCPCLIENT=CONNECTED; SM_TCPCLIENT=CONNECTED;
...@@ -115,18 +204,17 @@ void SkyliveProtocol::readFromNetwork() ...@@ -115,18 +204,17 @@ void SkyliveProtocol::readFromNetwork()
} }
break; break;
case PARAMS: case PARAMS:
protoMsg.computed_crc+=c; protoMsg.computed_crc+=static_cast<int>(c);
switch(c) switch(c)
{ {
case PARAM_END: case PARAM_END:
SM_TCPCLIENT=CRC; SM_TCPCLIENT=CRC;
protoMsg.computed_crc-=static_cast<int>(c);
break; break;
case PROTO_START: case PROTO_START:
{ clearPkt();
SKProtoMsg protoMsg;
SM_TCPCLIENT=COMMAND; SM_TCPCLIENT=COMMAND;
break; break;
}
case CMD_END: case CMD_END:
case PROTO_END: case PROTO_END:
SM_TCPCLIENT=CONNECTED; SM_TCPCLIENT=CONNECTED;
...@@ -139,11 +227,9 @@ void SkyliveProtocol::readFromNetwork() ...@@ -139,11 +227,9 @@ void SkyliveProtocol::readFromNetwork()
switch(c) switch(c)
{ {
case PROTO_START: case PROTO_START:
{ clearPkt();
SKProtoMsg protoMsg;
SM_TCPCLIENT=COMMAND; SM_TCPCLIENT=COMMAND;
break; break;
}
case PROTO_END: case PROTO_END:
protoQueue.enqueue(protoMsg); protoQueue.enqueue(protoMsg);
//processPackets(); //processPackets();
...@@ -177,6 +263,7 @@ void SkyliveProtocol::handle_connect(SKMessage::SKMessage msg) ...@@ -177,6 +263,7 @@ void SkyliveProtocol::handle_connect(SKMessage::SKMessage msg)
void SkyliveProtocol::clientConnected() void SkyliveProtocol::clientConnected()
{ {
SM_TCPCLIENT = CONNECTED; SM_TCPCLIENT = CONNECTED;
} }
void SkyliveProtocol::receiveMessage(SKMessage::SKMessage msg) void SkyliveProtocol::receiveMessage(SKMessage::SKMessage msg)
......
...@@ -41,7 +41,7 @@ ...@@ -41,7 +41,7 @@
#include <QHash> #include <QHash>
#include <QTcpSocket> #include <QTcpSocket>
//#include <QNetworkSession> //#include <QNetworkSession>
#include <QByteArray> //#include <QByteArray>
#include <QQueue> #include <QQueue>
#include <QTimer> #include <QTimer>
#include "pluginsinterfaces.h" #include "pluginsinterfaces.h"
...@@ -54,9 +54,9 @@ ...@@ -54,9 +54,9 @@
struct SKProtoMsg struct SKProtoMsg
{ {
QByteArray cmd; QString cmd;
QByteArray params; QString params;
QByteArray crc; QString crc;
int computed_crc; int computed_crc;
}; };
...@@ -95,13 +95,18 @@ class SkyliveProtocol : public QObject, SkylivexPluginInterface ...@@ -95,13 +95,18 @@ class SkyliveProtocol : public QObject, SkylivexPluginInterface
SKProtoMsg protoMsg; SKProtoMsg protoMsg;
QQueue<SKProtoMsg> protoQueue; QQueue<SKProtoMsg> protoQueue;
QTimer* pktTimer; QTimer* pktTimer;
void clearPkt();
public: public:
void startPlugin(); void startPlugin();
void sendMessage(SKMessage::SKMessage msg); void sendMessage(SKMessage::SKMessage msg);
void registerHandler(QString type, SKHandlerFunction handler); void registerHandler(QString type, SKHandlerFunction handler);
void handle_connect(SKMessage::SKMessage msg); void handle_connect(SKMessage::SKMessage msg);
void sendPacket(const char* cmd, const char* params);
void sendPacket(QString &cmd, QString &params);
void sendPacket(SKProtoMsg &pkt);
void sendPacket(QString &cmd, QList<QString> &paramlist);
private slots: private slots:
void sessionOpened(); void sessionOpened();
void readFromNetwork(); void readFromNetwork();
......
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