summaryrefslogtreecommitdiff
path: root/qt/consoleserver/server.cpp
blob: 401ee8dc923ddbac1507c294d0acb06e2ebbaf99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "server.h"

server::server(QObject *parent) : QObject(parent)
{
	srv = new QTcpServer();
	connect(srv, SIGNAL(newConnection()), this, SLOT(new_connection()));
	qDebug() << "server loaded but not started";
}

bool server::server_start()
{
	qDebug() << "server started";
	if (srv->listen(QHostAddress::Any, 3001))
	{
		qDebug() << "listening on 3001"; 
		return true;
	}
	else
	{
		return false;
	}
}

bool server::server_stop()
{
	srv->close();
	if (srv->isListening())
		return false;
	return true;
}

void server::new_connection()
{
	QTcpSocket *socket;
	socket = srv->nextPendingConnection();
	socket->waitForReadyRead(3000);
	handle(QString(socket->readAll()).remove(QRegExp("[\\n\\t\\r]")));
	socket->close();
	delete socket;
}

void server::handle(QString msg)
{
	qDebug() << "received" << msg;
	if (msg.contains(";"))
	{
		QStringList tmp = msg.split(";");
		for (int x = 0; x < tmp.size(); x++)
		{
			emit sendcmd(tmp.at(x));
		}
	}
	else
	{
		emit sendcmd(msg);
	}
}