Commit 3b4a660b authored by Jonathan Michalon's avatar Jonathan Michalon

server: harmonize client message handling and ensure a reply is sent

parent ec99b30c
......@@ -34,6 +34,7 @@ public class OVCC.Network.LoginMessage : VariantMessage
{
QUERY,
OK,
MALFORMED_MESSAGE,
ALREADY_EXISTS,
MISSING_AUTHENTICATION,
UNAUTHORIZED
......
......@@ -95,49 +95,58 @@ public class Server: ThreadedSocketService
}
private bool handle_login (LoginMessage msg, Client client)
throws Error
{
bool ok = true;
debug ("Login tentative for '%s' with '%s'", msg.login, msg.password);
if (msg.status != LoginMessage.State.QUERY) {
critical ("got LoginMessage with status different from QUERY");
return false;
}
/* missing authentication check */
if (msg.password == "") {
/* message state check */
debug ("got LoginMessage with status different from QUERY");
msg.status = LoginMessage.State.MALFORMED_MESSAGE;
} else if (msg.password == "") {
/* empty password not allowed */
debug ("rejecting auth as password is empty");
msg.status = LoginMessage.State.MISSING_AUTHENTICATION;
client.send (msg);
return false;
}
/* already existing name check WARNING: lock needed */
lock (this.clients) {
foreach (Client c in this.clients) {
if (c.login == msg.login) {
ok = false;
debug ("rejecting auth as login already connected");
msg.status = LoginMessage.State.ALREADY_EXISTS;
break;
msg.status = LoginMessage.State.MISSING_AUTHENTICATION;
} else {
lock (this.clients) {
/* forbid duplicates */
foreach (Client c in this.clients) {
if (c.login == msg.login) {
ok = false;
debug ("rejecting auth as login already connected");
msg.status = LoginMessage.State.ALREADY_EXISTS;
break;
}
}
/* actual registration */
if (ok) {
debug ("accepting auth for %s", msg.login);
msg.status = LoginMessage.State.OK;
client.login = msg.login;
client.password = msg.password;
}
}
/* registration */
if (ok) {
debug ("accepting auth for %s", msg.login);
msg.status = LoginMessage.State.OK;
client.login = msg.login;
client.password = msg.password;
}
}
client.send (msg);
try {
client.send (msg);
} catch (Error e) {
warning ("Can't reply to client: %s", e.message);
return false;
}
return ok;
}
private bool handle_join (JoinMessage msg, Client client)
throws Error
{
Game? game = null;
debug ("Game Join tentative for game '%d'", msg.game_index);
if (msg.game_index == -1) {
game = pick_open_game ();
} else {
......@@ -146,37 +155,58 @@ public class Server: ThreadedSocketService
if (game == null || !game.state.is_open ()) {
debug ("Bad game %d, sending fail join message", msg.game_index);
var join = new JoinMessage (msg.game_index);
join.status = JoinMessage.State.FAILED;
client.send (join);
msg.status = JoinMessage.State.FAILED;
} else {
debug ("Accepting game join");
msg.status = JoinMessage.State.OK;
}
try {
client.send (msg);
} catch (Error e) {
warning ("Can't reply to client: %s", e.message);
return false;
}
debug ("Accepting game join");
var join = new JoinMessage (msg.game_index);
join.status = JoinMessage.State.OK;
client.send (join);
client.join (game);
if (msg.status == JoinMessage.State.OK) {
client.join (game);
debug ("Sending game data");
var data = new GamedataMessage (client.game.get_stack_ids(),
client.game.get_player_nicks(),
tiles_data, tileset_data);
try {
client.send (data);
} catch (Error e) {
warning ("Can't reply to client: %s", e.message);
return false;
}
}
debug ("Sending game data");
var data = new GamedataMessage (client.game.get_stack_ids(),
client.game.get_player_nicks(),
tiles_data, tileset_data);
client.send (data);
return true;
}
private bool handle_list_games (ListGamesMessage msg, Client client)
throws Error
{
GameDescription[] list = {};
debug ("A client asked for a list of games, let's reply");
foreach (var t in games) {
list += t.describe ();
}
var data = new ListGamesMessage (list);
client.send (data);
try {
client.send (data);
} catch (Error e) {
warning ("Can't reply to client: %s", e.message);
return false;
}
return true;
}
......@@ -222,35 +252,20 @@ public class Server: ThreadedSocketService
}
switch (msg.message_type) {
case MessageType.LOGIN:
debug ("Login for '%s' with '%s'", (msg as LoginMessage).login,
(msg as LoginMessage).password);
try {
handle_login ((msg as LoginMessage), client);
} catch (Error e) {
/* FIXME send error message */
warning ("Problems handling a client login... left in unknown state");
}
handle_login ((msg as LoginMessage), client);
break;
case MessageType.JOIN:
try {
handle_join (msg as JoinMessage, client);
} catch (Error e) {
/* FIXME send error message */
warning ("Problems handling a client join... left in unknown state");
}
handle_join (msg as JoinMessage, client);
break;
case MessageType.LIST_GAMES:
try {
handle_list_games (msg as ListGamesMessage, client);
} catch (Error e) {
/* FIXME send error message */
warning ("Problems handling a list game action...");
}
handle_list_games (msg as ListGamesMessage, client);
break;
case MessageType.SIGNAL:
client.signal_handle.emit_received (msg as SignalMessage);
break;
case MessageType.DISCONNECT: running = false; break;
case MessageType.DISCONNECT:
running = false;
break;
}
}
lock (this.clients) {
......
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