-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
executable file
·128 lines (103 loc) · 4.28 KB
/
client.cpp
File metadata and controls
executable file
·128 lines (103 loc) · 4.28 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include <QtNetwork>
#include "client.h"
#include "network.h"
//WITHOUT GUI, a parent window -> the socket has to be deleted manually!!
Client::Client(Network *parent, QString qIPAddress, quint16 port)
: _qIPAddress(qIPAddress),
_port(port){
//Input std::string to int to qint16 -> the params the method takes
tcpSocket = new QTcpSocket(this);
//data stream "in"
iostream.setDevice(tcpSocket);
//Make sure that other connections (that might still be in place) are disconnected (sockets deleted)
tcpSocket->abort();
//WHERE THE MAGIC HAPPENS!
tcpSocket->connectToHost(_qIPAddress, _port);
//CONNECTS
//NETWORK-INTERNAL
//SIGNAL readyRead is emitted very time A BLOCK IS RECEIVED (from the byte-array)
connect(tcpSocket, &QIODevice::readyRead, this, &Client::receive);
//To know whether error happened -> QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error) gibt es nicht
connect(tcpSocket, static_cast<void (QAbstractSocket::*)(QAbstractSocket::SocketError)>(&QAbstractSocket::error),this, &Client::displayError);
//After disconnection, delete Socket
connect(tcpSocket, &QAbstractSocket::disconnected, tcpSocket, &QObject::deleteLater);
//Tell logic that disconnected
connect(tcpSocket, &QAbstractSocket::disconnected, this, &Net::setOpponentDisconnected);
//NETWORK-LOGIC
//Incoming: Signal FROM logic
connect(parent, &Network::netSendRequestStartRound,
this, &Client::netSendRequestStartRound);
connect(parent, &Network::netSendMove,
this, &Client::netSendMove);
connect(parent, &Network::netSendResponseOnRequest,
this, &Client::netSendResponseOnRequest);
connect(parent, &Network::netSendResponseOnMove,
this, &Client::netSendResponseOnMove);
connect(parent, &Network::netSendNickName,
this, &Client::netSendNickName);
connect(parent, &Network::netSendChat,
this, &Client::netSendChat);
//Outgoing: Signal TO logic
connect(this, &Client::netConnected,
parent, &Network::netConnected);
connect(this, &Client::netRecGameParameters,
parent, &Network::netRecGameParameters);
connect(this, &Client::netRecRequestStartRound,
parent, &Network::netRecRequestStartRound);
connect(this, &Client::netRecMove,
parent, &Network::netRecMove);
connect(this, &Client::netRecResponseOnRequest,
parent, &Network::netRecResponseOnRequest);
connect(this, &Client::netRecResponseOnMove,
parent, &Network::netRecResponseOnMove);
connect(this, &Client::netRecChatMsg,
parent, &Network::netRecChat);
connect(this, &Client::netRecErrorLog,
parent, &Network::netRecChat);
connect(this, &Client::netRecNickName,
parent, &Network::netRecNickName);
std::cout<<"CLIENT: Session connected, TcpSocket and Datatream init."<<std::endl;
emit netConnected(conSuccess);
}
Client::~Client(){
tcpSocket->abort();
//Disconnect!
tcpSocket->disconnectFromHost();
}
/*
void Client::readFortune()
{
QString nextFortune;
QString DataAsString = QTextCodec::codecForMib(1015)->toUnicode(tcpSocket->readAll());
std::cout<<"RECEIVED: "<<DataAsString.toStdString()<<std::endl;
in.startTransaction();
QString nextFortune;
in >> nextFortune;
if (!in.commitTransaction())
return;
if (nextFortune == currentFortune) {
QTimer::singleShot(0, this, &Client::requestNewFortune);
return;
}
currentFortune = nextFortune;
std::cout<<"RECEIVED: "<<currentFortune.toStdString()<<std::endl;
int iBytesAvailable = tcpSocket->bytesAvailable();
while(iBytesAvailable > 0){
if(tcpSocket->isValid()){
char* pzBuff = new char[iBytesAvailable];
int iReadBytes = tcpSocket->read(pzBuff, iBytesAvailable);
if( iReadBytes > 0 ){
std::cout<<"RECEIVED: "<<nextFortune.fromUtf8(tcpSocket->readAll())<<std::endl;
}else{
std::cerr<<"read bytes count is not valid !! "<<std::endl;
}
iBytesAvailable = tcpSocket->bytesAvailable();
}
}
//WRITE
test = '0x02';
iostream.writeRawData(&test, 1);
std::cout<<"SEND: "<<test<<std::endl;
}
*/