forked from hal1437/AsahikawaProcon-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TcpClient.cpp
183 lines (160 loc) · 5.83 KB
/
TcpClient.cpp
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "TcpClient.h"
#include <QSettings>
QString TCPClient::VisibilityString(QString str){
QString answer;
for(int i = 0;i < str.size();i++){
if (str[i] == '\n')answer.push_back("\\n");
else if(str[i] == '\r')answer.push_back("\\r");
else answer.push_back(str[i]);
}
return answer;
}
QString TCPClient::WaitResponce(){
int ignore = 0;
while(ignore != this->IGNORE_INVALD){
//レスポンス待ち
qDebug() << "WaitStart:";
if(this->client->waitForReadyRead(this->TIMEOUT)){
//レスポンスあり
QString response = "";
//自動結合
bool f = false;
do{
response += client->readLine();
}while(*(response.end()-1) != '\n' && this->client->waitForReadyRead(this->TIMEOUT));
qDebug() << "" + VisibilityString(response);
//不正文字列:空
if(response == "" || response == "\n" || response == "\r" || response == "\r\n"){
ignore++;
continue;
}
//不正文字列:改行なし
if(response.size() > 0 && *(response.end()-1) != '\n'){
disconnected_flag = true;
qDebug() << QString("[Port") + QString::number(this->client->localPort()) +"]:Noting \\n";
return QString();
}
qDebug()<<"test:"+response;
return response;
}else{
//レスポンスなし
disconnected_flag = true;
//qDebug() << QString("[Port") + QString::number(this->client->localPort()) +"]:Noting responce";
return QString();
}
}
disconnected_flag=true;
//qDebug() << QString("[Port") + QString::number(this->client->localPort()) +"]:Too many invald responce";
return QString();
}
bool TCPClient::WaitGetReady(){
if(disconnected_flag)return false;
//ターン開始文字列
if(client == nullptr)return false;
client->write(QString("@\r\n").toUtf8());
//レスポンス待ち
QString response = WaitResponce();
qDebug() << "res:"+response;
qDebug() << (response == "gr\r\n");
return (response == "gr\r\n");
}
GameSystem::Method TCPClient::WaitReturnMethod(GameSystem::AroundData data){
if(disconnected_flag)return GameSystem::Method();
//周辺情報文字列
if(client == nullptr)return GameSystem::Method{GameSystem::TEAM::UNKNOWN,
GameSystem::Method::ACTION::UNKNOWN,
GameSystem::Method::ROTE::UNKNOWN};
client->write(QString(data.toString() + "\r\n").toUtf8());
qDebug() << "WRM";
qDebug() << data.toString();
//レスポンス待ち
QString response = WaitResponce();
if(response != QString())return GameSystem::Method::fromString(response);
else return GameSystem::Method{GameSystem::TEAM::UNKNOWN,
GameSystem::Method::ACTION::UNKNOWN,
GameSystem::Method::ROTE::UNKNOWN};
}
bool TCPClient::WaitEndSharp(GameSystem::AroundData data){
if(disconnected_flag)return false;
if(client == nullptr)return false;
//周辺情報文字列
client->write(QString(data.toString() + "\r\n").toUtf8());
qDebug() << "WES";
qDebug() << data.toString();
//レスポンス待ち
return(WaitResponce() == "#\r\n");
}
bool TCPClient::OpenSocket(int Port){
this->server->listen(QHostAddress::Any,Port);
return true;
}
bool TCPClient::CloseSocket(){
if(this->client != nullptr)this->client->disconnectFromHost();
this->client = nullptr;
this->server->close();
this->server = new QTcpServer(this);
emit DisConnect();
return true;
}
bool TCPClient::isConnecting(){
return this->server->isListening();
}
void TCPClient::NewConnect(){
this->client = this->server->nextPendingConnection();
this->IP = this->client->localAddress().toString();
connect(this->client,SIGNAL(readyRead()),this,SLOT(GetTeamName()));
connect(this->client,SIGNAL(disconnected()),this,SLOT(DisConnect()));
emit Connected();
}
void TCPClient::DisConnect(){
emit Disconnected();
this->client = nullptr;
this->IP = "";
this->Name = "";
disconnected_flag=true;
}
QString TCPClient::GetTeamName(){
if(this->Name == ""){
//Qstring::fromLocal8bitで文字化け対策しています
//this->Name = QString::fromLocal8Bit(client->readAll());
QByteArray bytebuf = client->readAll();
QString namebuf = bytebuf;
//ここで0xFFFD(utfへの自動変換の失敗)が観測された場合、変換を行う
for(int i = 0; i < namebuf.size(); i++){
QChar buffer = namebuf.at(i);
//不明な文字が発見された場合、文字コードをUTFに変換
if(buffer == 0xFFFD){
namebuf = QString::fromLocal8Bit(bytebuf);
}
}
this->Name = namebuf;
disconnect(this->client,SIGNAL(readyRead()),this,SLOT(GetTeamName()));
emit WriteTeamName();
emit Ready();
return this->Name;
}
return this->Name;
}
TCPClient::TCPClient(QObject *parent) :
BaseClient(parent)
{
QSettings* mSettings;
mSettings = new QSettings( "setting.ini", QSettings::IniFormat ); // iniファイルで設定を保存
mSettings->setIniCodec( "UTF-8" ); // iniファイルの文字コード
QVariant v = mSettings->value( "Timeout" );
if (v.type() != QVariant::Invalid){
TIMEOUT = v.toInt();
}
this->server = new QTcpServer(this);
this->client = nullptr;
//接続最大数を1に固定
this->server->setMaxPendingConnections(1);
//シグナルとスロットを接続
connect(this->server,SIGNAL(newConnection()),this,SLOT(NewConnect()));
}
TCPClient::~TCPClient()
{
if(isConnecting()){
CloseSocket();
}
}