Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Donald Haase
Cockatrice
Commits
c46ef255
Commit
c46ef255
authored
Nov 12, 2009
by
Max-Wilhelm Bruker
Browse files
some client code
parent
240ff060
Changes
8
Hide whitespace changes
Inline
Side-by-side
cockatrice/src/client.cpp
View file @
c46ef255
...
...
@@ -8,6 +8,8 @@
Client
::
Client
(
QObject
*
parent
)
:
QObject
(
parent
),
currentItem
(
0
),
status
(
StatusDisconnected
)
{
ProtocolItem
::
initializeHash
();
timer
=
new
QTimer
(
this
);
timer
->
setInterval
(
1000
);
connect
(
timer
,
SIGNAL
(
timeout
()),
this
,
SLOT
(
ping
()));
...
...
@@ -40,36 +42,21 @@ void Client::slotConnected()
setStatus
(
StatusAwaitingWelcome
);
}
void
Client
::
removePendingCommand
()
{
pendingCommands
.
remove
(
static_cast
<
Command
*>
(
sender
())
->
getCmdId
());
}
/*
void Client::loginResponse(ServerResponse response)
void
Client
::
loginResponse
(
ResponseCode
response
)
{
if
(
response
==
RespOk
)
setStatus(Status
Idle
);
setStatus
(
Status
LoggedIn
);
else
{
emit
serverError
(
response
);
disconnectFromServer
();
}
}
void Client::enterGameResponse(ServerResponse response)
{
if (response == RespOk)
setStatus(StatusPlaying);
}
void Client::leaveGameResponse(ServerResponse response)
{
if (response == RespOk)
setStatus(StatusIdle);
}
*/
void
Client
::
readData
()
{
xmlReader
->
addData
(
socket
->
readAll
());
QByteArray
data
=
socket
->
readAll
();
qDebug
()
<<
data
;
xmlReader
->
addData
(
data
);
if
(
currentItem
)
{
if
(
!
currentItem
->
read
(
xmlReader
))
...
...
@@ -80,8 +67,25 @@ void Client::readData()
xmlReader
->
readNext
();
if
(
xmlReader
->
isStartElement
())
{
QString
itemType
=
xmlReader
->
name
().
toString
();
if
(
itemType
==
"cockatrice_server_stream"
)
continue
;
if
(
itemType
==
"cockatrice_server_stream"
)
{
int
serverVersion
=
xmlReader
->
attributes
().
value
(
"version"
).
toString
().
toInt
();
if
(
serverVersion
!=
ProtocolItem
::
protocolVersion
)
{
emit
protocolVersionMismatch
(
ProtocolItem
::
protocolVersion
,
serverVersion
);
disconnectFromServer
();
return
;
}
else
{
xmlWriter
->
writeStartDocument
();
xmlWriter
->
writeStartElement
(
"cockatrice_client_stream"
);
xmlWriter
->
writeAttribute
(
"version"
,
QString
::
number
(
ProtocolItem
::
protocolVersion
));
setStatus
(
StatusLoggingIn
);
Command_Login
*
cmdLogin
=
new
Command_Login
(
userName
,
password
);
connect
(
cmdLogin
,
SIGNAL
(
finished
(
ResponseCode
)),
this
,
SLOT
(
loginResponse
(
ResponseCode
)));
sendCommand
(
cmdLogin
);
continue
;
}
}
QString
itemName
=
xmlReader
->
attributes
().
value
(
"name"
).
toString
();
qDebug
()
<<
"parseXml: startElement: "
<<
"type ="
<<
itemType
<<
", name ="
<<
itemName
;
currentItem
=
ProtocolItem
::
getNewItem
(
itemType
+
itemName
);
...
...
@@ -90,13 +94,9 @@ void Client::readData()
if
(
!
currentItem
->
read
(
xmlReader
))
return
;
else
{
/* Command *command = qobject_cast<Command *>(currentItem);
if (qobject_cast<InvalidCommand *>(command))
sendProtocolItem(new ProtocolResponse(command->getCmdId(), ProtocolResponse::RespInvalidCommand));
else
processCommand(command);
processProtocolItem
(
currentItem
);
currentItem
=
0
;
*/
}
}
}
}
/*
...
...
@@ -265,6 +265,46 @@ void Client::readData()
*/
}
void
Client
::
processProtocolItem
(
ProtocolItem
*
item
)
{
ProtocolResponse
*
response
=
qobject_cast
<
ProtocolResponse
*>
(
item
);
if
(
response
)
{
Command
*
cmd
=
pendingCommands
.
value
(
response
->
getCmdId
(),
0
);
if
(
!
cmd
)
return
;
cmd
->
processResponse
(
response
);
delete
response
;
pendingCommands
.
remove
(
cmd
->
getCmdId
());
delete
cmd
;
return
;
}
GenericEvent
*
genericEvent
=
qobject_cast
<
GenericEvent
*>
(
item
);
if
(
genericEvent
)
{
switch
(
genericEvent
->
getItemId
())
{
}
delete
genericEvent
;
return
;
}
/* GameEvent *gameEvent = qobject_cast<GameEvent *>(item);
if (gameEvent) {
emit gameEventReceived(gameEvent);
delete gameEvent;
return;
}
*/
ChatEvent
*
chatEvent
=
qobject_cast
<
ChatEvent
*>
(
item
);
if
(
chatEvent
)
{
qDebug
()
<<
"chatEventReceived()"
;
emit
chatEventReceived
(
chatEvent
);
delete
chatEvent
;
}
}
void
Client
::
setStatus
(
const
ClientStatus
_status
)
{
if
(
_status
!=
status
)
{
...
...
@@ -272,26 +312,18 @@ void Client::setStatus(const ClientStatus _status)
emit
statusChanged
(
_status
);
}
}
/*
PendingComman
d
*
Client::
cmd(const QString &s, Pending
Command *
_pc
)
voi
d
Client
::
sendCommand
(
Command
*
cmd
)
{
msg(QString("%1|%2").arg(++MsgId).arg(s));
PendingCommand *pc;
if (_pc) {
pc = _pc;
pc->setMsgId(MsgId);
} else
pc = new PendingCommand(MsgId);
pendingCommands.insert(MsgId, pc);
connect(pc, SIGNAL(finished(ServerResponse)), this, SLOT(removePendingCommand()));
return pc;
cmd
->
write
(
xmlWriter
);
pendingCommands
.
insert
(
cmd
->
getCmdId
(),
cmd
);
}
*/
void
Client
::
connectToServer
(
const
QString
&
hostname
,
unsigned
int
port
,
const
QString
&
_
play
erName
,
const
QString
&
_password
)
void
Client
::
connectToServer
(
const
QString
&
hostname
,
unsigned
int
port
,
const
QString
&
_
us
erName
,
const
QString
&
_password
)
{
disconnectFromServer
();
play
erName
=
_
play
erName
;
us
erName
=
_
us
erName
;
password
=
_password
;
socket
->
connectToHost
(
hostname
,
port
);
setStatus
(
StatusConnecting
);
...
...
@@ -326,10 +358,8 @@ void Client::ping()
if
(
maxTime
>=
maxTimeout
)
{
emit
serverTimeout
();
disconnectFromServer
();
}
/* else
cmd("ping");
*/
}
else
sendCommand
(
new
Command_Ping
);
}
/*
PendingCommand *Client::chatListChannels()
...
...
cockatrice/src/client.h
View file @
c46ef255
...
...
@@ -5,12 +5,15 @@
#include
<QColor>
#include
<QStringList>
#include
<QHash>
#include
"protocol_datastructures.h"
class
QTimer
;
class
Command
;
class
QXmlStreamReader
;
class
QXmlStreamWriter
;
class
ProtocolItem
;
class
ChatEvent
;
enum
ClientStatus
{
StatusDisconnected
,
...
...
@@ -24,26 +27,23 @@ class Client : public QObject {
Q_OBJECT
signals:
void
statusChanged
(
ClientStatus
_status
);
void
welcomeMsgReceived
(
QString
welcomeMsg
);
// void gameListEvent(const ServerGame &game);
void
playerIdReceived
(
int
id
,
QString
name
);
//
void playerIdReceived(int id, QString name);
// void gameEvent(const ServerEventData &msg);
// void chatEvent(const ChatEventData &msg);
void
maxPingTime
(
int
seconds
,
int
maxSeconds
);
void
serverTimeout
();
void
logSocketError
(
const
QString
&
errorString
);
//
void serverError(
Server
Response resp);
void
protocolVersionMismatch
();
void
serverError
(
Response
Code
resp
);
void
protocolVersionMismatch
(
int
clientVersion
,
int
serverVersion
);
void
protocolError
();
void
chatEventReceived
(
ChatEvent
*
event
);
private
slots
:
void
slotConnected
();
void
readData
();
void
slotSocketError
(
QAbstractSocket
::
SocketError
error
);
void
ping
();
void
removePendingCommand
();
// void loginResponse(ServerResponse response);
// void enterGameResponse(ServerResponse response);
// void leaveGameResponse(ServerResponse response);
void
loginResponse
(
ResponseCode
response
);
private:
static
const
int
maxTimeout
=
10
;
...
...
@@ -54,16 +54,18 @@ private:
QXmlStreamWriter
*
xmlWriter
;
ProtocolItem
*
currentItem
;
ClientStatus
status
;
QString
play
erName
,
password
;
QString
us
erName
,
password
;
void
setStatus
(
ClientStatus
_status
);
void
processProtocolItem
(
ProtocolItem
*
item
);
public:
Client
(
QObject
*
parent
=
0
);
~
Client
();
ClientStatus
getStatus
()
const
{
return
status
;
}
QString
peerName
()
const
{
return
socket
->
peerName
();
}
void
connectToServer
(
const
QString
&
hostname
,
unsigned
int
port
,
const
QString
&
_
play
erName
,
const
QString
&
_password
);
void
connectToServer
(
const
QString
&
hostname
,
unsigned
int
port
,
const
QString
&
_
us
erName
,
const
QString
&
_password
);
void
disconnectFromServer
();
void
sendCommand
(
Command
*
cmd
);
public
slots
:
void
chatListChannels
()
{
}
...
...
cockatrice/src/messagelogwidget.cpp
View file @
c46ef255
...
...
@@ -17,9 +17,9 @@ void MessageLogWidget::logConnecting(QString hostname)
append
(
tr
(
"Connecting to %1..."
).
arg
(
sanitizeHtml
(
hostname
)));
}
void
MessageLogWidget
::
logConnected
(
QString
welcomeMsg
)
void
MessageLogWidget
::
logConnected
()
{
append
(
tr
(
"Connected
: %1"
).
arg
(
welcomeMsg
));
append
(
tr
(
"Connected
."
));
}
void
MessageLogWidget
::
logDisconnected
()
...
...
@@ -40,9 +40,9 @@ void MessageLogWidget::logServerError(ResponseCode response)
}
}
void
MessageLogWidget
::
logProtocolVersionMismatch
()
void
MessageLogWidget
::
logProtocolVersionMismatch
(
int
clientVersion
,
int
serverVersion
)
{
append
(
tr
(
"Protocol version mismatch.
"
));
append
(
tr
(
"Protocol version mismatch.
Client: %1, Server: %2"
).
arg
(
clientVersion
).
arg
(
serverVersion
));
}
void
MessageLogWidget
::
logProtocolError
()
...
...
cockatrice/src/messagelogwidget.h
View file @
c46ef255
...
...
@@ -17,11 +17,11 @@ private:
QString
trZoneName
(
CardZone
*
zone
,
Player
*
player
,
bool
hisOwn
,
GrammaticalCase
gc
)
const
;
public
slots
:
void
logConnecting
(
QString
hostname
);
void
logConnected
(
QString
welcomeMsg
);
void
logConnected
();
void
logDisconnected
();
void
logSocketError
(
const
QString
&
errorString
);
void
logServerError
(
ResponseCode
response
);
void
logProtocolVersionMismatch
();
void
logProtocolVersionMismatch
(
int
clientVersion
,
int
serverVersion
);
void
logProtocolError
();
private
slots
:
void
logPlayerListReceived
(
QStringList
players
);
...
...
cockatrice/src/window_main.cpp
View file @
c46ef255
...
...
@@ -98,6 +98,7 @@ void MainWindow::statusChanged(ClientStatus _status)
emit
logDisconnected
();
break
;
case
StatusLoggingIn
:
emit
logConnected
();
aConnect
->
setEnabled
(
false
);
aDisconnect
->
setEnabled
(
true
);
break
;
...
...
@@ -341,11 +342,11 @@ MainWindow::MainWindow(QTranslator *_translator, QWidget *parent)
connect
(
client
,
SIGNAL
(
statusChanged
(
ClientStatus
)),
this
,
SLOT
(
statusChanged
(
ClientStatus
)));
connect
(
this
,
SIGNAL
(
logConnecting
(
QString
)),
messageLog
,
SLOT
(
logConnecting
(
QString
)));
connect
(
client
,
SIGNAL
(
welcomeMsgReceived
(
QString
)),
messageLog
,
SLOT
(
logConnected
(
QString
)));
connect
(
this
,
SIGNAL
(
logConnected
(
)),
messageLog
,
SLOT
(
logConnected
()));
connect
(
this
,
SIGNAL
(
logDisconnected
()),
messageLog
,
SLOT
(
logDisconnected
()));
connect
(
client
,
SIGNAL
(
logSocketError
(
const
QString
&
)),
messageLog
,
SLOT
(
logSocketError
(
const
QString
&
)));
connect
(
client
,
SIGNAL
(
serverError
(
Server
Response
)),
messageLog
,
SLOT
(
logServerError
(
Server
Response
)));
connect
(
client
,
SIGNAL
(
protocolVersionMismatch
()),
messageLog
,
SLOT
(
logProtocolVersionMismatch
()));
connect
(
client
,
SIGNAL
(
serverError
(
Response
Code
)),
messageLog
,
SLOT
(
logServerError
(
Response
Code
)));
connect
(
client
,
SIGNAL
(
protocolVersionMismatch
(
int
,
int
)),
messageLog
,
SLOT
(
logProtocolVersionMismatch
(
int
,
int
)));
connect
(
client
,
SIGNAL
(
protocolError
()),
messageLog
,
SLOT
(
logProtocolError
()));
connect
(
phasesToolbar
,
SIGNAL
(
signalSetPhase
(
int
)),
client
,
SLOT
(
setActivePhase
(
int
)));
connect
(
phasesToolbar
,
SIGNAL
(
signalNextTurn
()),
client
,
SLOT
(
nextTurn
()));
...
...
cockatrice/src/window_main.h
View file @
c46ef255
...
...
@@ -75,6 +75,7 @@ private slots:
void
actExit
();
signals:
void
logConnecting
(
QString
hostname
);
void
logConnected
();
void
logDisconnected
();
private:
void
retranslateUi
();
...
...
common/protocol.cpp
View file @
c46ef255
...
...
@@ -84,6 +84,11 @@ void Command::extractParameters()
cmdId
=
-
1
;
}
void
Command
::
processResponse
(
ProtocolResponse
*
response
)
{
emit
finished
(
response
->
getResponseCode
());
}
QHash
<
QString
,
ResponseCode
>
ProtocolResponse
::
responseHash
;
ProtocolResponse
::
ProtocolResponse
(
int
_cmdId
,
ResponseCode
_responseCode
)
...
...
common/protocol.h
View file @
c46ef255
...
...
@@ -13,6 +13,8 @@ class QXmlStreamReader;
class
QXmlStreamWriter
;
class
QXmlStreamAttributes
;
class
ProtocolResponse
;
enum
ItemId
{
ItemId_Event_ChatListChannels
=
ItemId_Other
+
1
,
ItemId_Event_ChatListPlayers
=
ItemId_Other
+
2
,
...
...
@@ -51,6 +53,8 @@ public:
class
Command
:
public
ProtocolItem
{
Q_OBJECT
signals:
void
finished
(
ResponseCode
response
);
private:
int
cmdId
;
int
ticks
;
...
...
@@ -62,6 +66,7 @@ public:
Command
(
const
QString
&
_itemName
=
QString
(),
int
_cmdId
=
-
1
);
int
getCmdId
()
const
{
return
cmdId
;
}
int
tick
()
{
return
++
ticks
;
}
void
processResponse
(
ProtocolResponse
*
response
);
};
class
InvalidCommand
:
public
Command
{
...
...
@@ -121,6 +126,8 @@ public:
int
getItemId
()
const
{
return
ItemId_Other
;
}
static
void
initializeHash
();
static
ProtocolItem
*
newItem
()
{
return
new
ProtocolResponse
;
}
int
getCmdId
()
const
{
return
cmdId
;
}
ResponseCode
getResponseCode
()
const
{
return
responseCode
;
}
};
class
GenericEvent
:
public
ProtocolItem
{
...
...
@@ -157,12 +164,12 @@ public:
ChatEvent
(
const
QString
&
_eventName
,
const
QString
&
_channel
);
};
class
Event_ChatListChannels
:
public
Generic
Event
{
class
Event_ChatListChannels
:
public
Chat
Event
{
Q_OBJECT
private:
QList
<
ServerChatChannelInfo
>
channelList
;
public:
Event_ChatListChannels
()
:
Generic
Event
(
"chat_list_channels"
)
{
}
Event_ChatListChannels
()
:
Chat
Event
(
"chat_list_channels"
,
QString
()
)
{
}
int
getItemId
()
const
{
return
ItemId_Event_ChatListChannels
;
}
void
addChannel
(
const
QString
&
_name
,
const
QString
&
_description
,
int
_playerCount
,
bool
_autoJoin
)
{
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment