project_files/frontlib/net/netconn.h
changeset 7482 d70a5b0d1190
parent 7338 1ed603a54ebd
child 7497 7e1d72fc03c7
equal deleted inserted replaced
7479:c8c552ee3acb 7482:d70a5b0d1190
    15  * You should have received a copy of the GNU General Public License
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program; if not, write to the Free Software
    16  * along with this program; if not, write to the Free Software
    17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    18  */
    18  */
    19 
    19 
       
    20 /**
       
    21  * This file contains functions for communicating with a Hedgewars server to chat, prepare
       
    22  * and play rounds of Hedgewars.
       
    23  *
       
    24  * To use this, first create a netconn object by calling flib_netconn_create. This will
       
    25  * start the connection to the game server (which might fail right away, the function
       
    26  * returns null then). You should also register your callback functions right at the start
       
    27  * to ensure you don't miss any callbacks.
       
    28  *
       
    29  * In order to allow the netconn to run, you should regularly call flib_netconn_tick(), which
       
    30  * performs network I/O and calls your callbacks on interesting events.
       
    31  *
       
    32  * When the connection is closed, you will receive the onDisconnect callback. This is the
       
    33  * signal to destroy the netconn and stop calling tick().
       
    34  *
       
    35  * The connection process lasts from the time you create the netconn until you receive the
       
    36  * onConnected callback (or onDisconnected in case something goes wrong). During that time,
       
    37  * you might receive the onNickTaken and onPasswordRequest callbacks; see their description
       
    38  * for more information on how to handle them. You could also receive other callbacks during
       
    39  * connecting (e.g. about the room list), but it should be safe to ignore them.
       
    40  *
       
    41  * Once you are connected, you are in the lobby, and you can enter rooms and leave them again.
       
    42  * The room and lobby states have different protocols, so many commands only work in either
       
    43  * one or the other. If you are in a room you might also be in a game, but that does not
       
    44  * change the protocol. The functions below are grouped by the states in which they make
       
    45  * sense, or (for the callbacks) the states in which you would typically receive them.
       
    46  *
       
    47  * The state changes from lobby to room when the server tells you that you just entered one,
       
    48  * which will also trigger the onEnterRoom callback. This usually happens in reply to either
       
    49  * a joinRoom, createRoom or playerFollow command.
       
    50  *
       
    51  * The state changes back to lobby when the room is dissolved, when you are kicked from the
       
    52  * room, or when you actively leave the room using flib_netconn_send_leaveRoom. The first
       
    53  * two events will trigger the onLeaveRoom callback.
       
    54  */
       
    55 
    20 #ifndef NETCONN_H_
    56 #ifndef NETCONN_H_
    21 #define NETCONN_H_
    57 #define NETCONN_H_
    22 
    58 
    23 #include "../model/gamesetup.h"
    59 #include "../model/gamesetup.h"
    24 #include "../model/scheme.h"
    60 #include "../model/scheme.h"
    29 #include <stdbool.h>
    65 #include <stdbool.h>
    30 
    66 
    31 #define NETCONN_STATE_CONNECTING 0
    67 #define NETCONN_STATE_CONNECTING 0
    32 #define NETCONN_STATE_LOBBY 1
    68 #define NETCONN_STATE_LOBBY 1
    33 #define NETCONN_STATE_ROOM 2
    69 #define NETCONN_STATE_ROOM 2
    34 #define NETCONN_STATE_INGAME 3
       
    35 #define NETCONN_STATE_DISCONNECTED 10
    70 #define NETCONN_STATE_DISCONNECTED 10
    36 
    71 
    37 #define NETCONN_DISCONNECT_NORMAL 0
    72 #define NETCONN_DISCONNECT_NORMAL 0				// The connection was closed normally
    38 #define NETCONN_DISCONNECT_SERVER_TOO_OLD 1
    73 #define NETCONN_DISCONNECT_SERVER_TOO_OLD 1		// The server has a lower protocol version than we do
    39 #define NETCONN_DISCONNECT_AUTH_FAILED 2 // TODO can you retry instead?
    74 #define NETCONN_DISCONNECT_AUTH_FAILED 2		// You sent a password with flib_netconn_send_password that was not accepted
    40 #define NETCONN_DISCONNECT_CONNLOST 3
    75 #define NETCONN_DISCONNECT_CONNLOST 3			// The network connection was lost
    41 #define NETCONN_DISCONNECT_INTERNAL_ERROR 100
    76 #define NETCONN_DISCONNECT_INTERNAL_ERROR 100	// Something went wrong in frontlib itself
    42 
    77 
    43 #define NETCONN_ROOMLEAVE_ABANDONED 0
    78 #define NETCONN_ROOMLEAVE_ABANDONED 0			// The room was closed because the chief left
    44 #define NETCONN_ROOMLEAVE_KICKED 1
    79 #define NETCONN_ROOMLEAVE_KICKED 1				// You have been kicked from the room
    45 
    80 
    46 #define NETCONN_MSG_TYPE_PLAYERINFO 0
    81 #define NETCONN_MSG_TYPE_PLAYERINFO 0			// A response to flib_netconn_send_playerInfo
    47 #define NETCONN_MSG_TYPE_SERVERMESSAGE 1
    82 #define NETCONN_MSG_TYPE_SERVERMESSAGE 1		// The welcome message when connecting to the lobby
    48 #define NETCONN_MSG_TYPE_WARNING 2
    83 #define NETCONN_MSG_TYPE_WARNING 2				// A general warning message
    49 #define NETCONN_MSG_TYPE_ERROR 3
    84 #define NETCONN_MSG_TYPE_ERROR 3				// A general error message
    50 
    85 
    51 #define NETCONN_MAPCHANGE_FULL 0
    86 #define NETCONN_MAPCHANGE_FULL 0
    52 #define NETCONN_MAPCHANGE_MAP 1
    87 #define NETCONN_MAPCHANGE_MAP 1
    53 #define NETCONN_MAPCHANGE_MAPGEN 2
    88 #define NETCONN_MAPCHANGE_MAPGEN 2
    54 #define NETCONN_MAPCHANGE_DRAWNMAP 3
    89 #define NETCONN_MAPCHANGE_DRAWNMAP 3
    78  * NETCONN_STATE_ROOM and NETCONN_STATE_INGAME states.
   113  * NETCONN_STATE_ROOM and NETCONN_STATE_INGAME states.
    79  */
   114  */
    80 bool flib_netconn_is_chief(flib_netconn *conn);
   115 bool flib_netconn_is_chief(flib_netconn *conn);
    81 
   116 
    82 /**
   117 /**
    83  * Are you in the context of a room, i.e. either in room or ingame state?
       
    84  */
       
    85 bool flib_netconn_is_in_room_context(flib_netconn *conn);
       
    86 
       
    87 /**
       
    88  * Returns the playername. This is *probably* the one provided on creation, but
   118  * Returns the playername. This is *probably* the one provided on creation, but
    89  * if that name was already taken, a different one could have been set by the
   119  * if that name was already taken, a different one could have been set by the
    90  * onNickTaken callback or its default implementation.
   120  * onNickTaken callback or its default implementation.
    91  */
   121  */
    92 const char *flib_netconn_get_playername(flib_netconn *conn);
   122 const char *flib_netconn_get_playername(flib_netconn *conn);
   114  */
   144  */
   115 int flib_netconn_send_teamchat(flib_netconn *conn, const char *msg);
   145 int flib_netconn_send_teamchat(flib_netconn *conn, const char *msg);
   116 
   146 
   117 /**
   147 /**
   118  * Send the password in reply to a password request.
   148  * Send the password in reply to a password request.
       
   149  * If the server does not accept the password, you will be disconnected (NETCONN_DISCONNECT_AUTH_FAILED)
   119  */
   150  */
   120 int flib_netconn_send_password(flib_netconn *conn, const char *passwd);
   151 int flib_netconn_send_password(flib_netconn *conn, const char *passwd);
   121 
   152 
   122 /**
   153 /**
   123  * Request a different nickname.
   154  * Request a different nickname.
   163  * receive an onReadyState callback containing the change.
   194  * receive an onReadyState callback containing the change.
   164  */
   195  */
   165 int flib_netconn_send_toggleReady(flib_netconn *conn);
   196 int flib_netconn_send_toggleReady(flib_netconn *conn);
   166 
   197 
   167 /**
   198 /**
   168  * Add a team to the current room. The message includes the team color, but not
   199  * Add a team to the current room. Apart from the "fixed" team information, this also includes
   169  * the number of hogs. Only makes sense when in room state. If the action succeeds, you will
   200  * the color, but not the number of hogs. Only makes sense when in room state. If the action
   170  * receive an onTeamAccepted callback with the name of the team.
   201  * succeeds, you will receive an onTeamAccepted callback with the name of the team.
   171  */
   202  */
   172 int flib_netconn_send_addTeam(flib_netconn *conn, const flib_team *team);
   203 int flib_netconn_send_addTeam(flib_netconn *conn, const flib_team *team);
   173 
   204 
   174 /**
   205 /**
   175  * Remove the team with the name teamname. Only makes sense when in room state.
   206  * Remove the team with the name teamname. Only makes sense when in room state.
   267  * has disconnected, passing 1 if the round ended normally, 0 otherwise.
   298  * has disconnected, passing 1 if the round ended normally, 0 otherwise.
   268  */
   299  */
   269 int flib_netconn_send_roundfinished(flib_netconn *conn, bool withoutError);
   300 int flib_netconn_send_roundfinished(flib_netconn *conn, bool withoutError);
   270 
   301 
   271 /**
   302 /**
   272  * Ban a player. TODO: Figure out details
   303  * Ban a player. You need to be in the lobby and a server admin for this to work.
   273  */
   304  */
   274 int flib_netconn_send_ban(flib_netconn *conn, const char *playerName);
   305 int flib_netconn_send_ban(flib_netconn *conn, const char *playerName);
   275 
   306 
   276 /**
   307 /**
   277  * Kick a player. TODO: Figure out details
   308  * Kick a player. This has different meanings in the lobby and in a room;
       
   309  * In the lobby, it will kick the player from the server, and you need to be a server admin to do it.
       
   310  * In a room, it will kick the player from the room, and you need to be room chief.
   278  */
   311  */
   279 int flib_netconn_send_kick(flib_netconn *conn, const char *playerName);
   312 int flib_netconn_send_kick(flib_netconn *conn, const char *playerName);
   280 
   313 
   281 /**
   314 /**
   282  * Request information about a player. If the action succeeds, you will
   315  * Request information about a player. If the action succeeds, you will
   284  * the requested information.
   317  * the requested information.
   285  */
   318  */
   286 int flib_netconn_send_playerInfo(flib_netconn *conn, const char *playerName);
   319 int flib_netconn_send_playerInfo(flib_netconn *conn, const char *playerName);
   287 
   320 
   288 /**
   321 /**
   289  * Follow a player. TODO figure out details
   322  * Follow a player. Only valid in the lobby. If the player is in a room (or in a game),
       
   323  * this command is analogous to calling flib_netconn_send_joinRoom with that room.
   290  */
   324  */
   291 int flib_netconn_send_playerFollow(flib_netconn *conn, const char *playerName);
   325 int flib_netconn_send_playerFollow(flib_netconn *conn, const char *playerName);
   292 
   326 
   293 /**
   327 /**
   294  * Signal that you want to start the game. Only makes sense in room state and if you are chief.
   328  * Signal that you want to start the game. Only makes sense in room state and if you are chief.
   295  * TODO details
   329  * The server will check whether all players are ready and whether it believes the setup makes
       
   330  * sense (e.g. more than one clan). If the server is satisfied, you will receive an onRunGame
       
   331  * callback (all other clients in the room are notified the same way). Otherwise the server
       
   332  * might answer with a warning, or might not answer at all.
   296  */
   333  */
   297 int flib_netconn_send_startGame(flib_netconn *conn);
   334 int flib_netconn_send_startGame(flib_netconn *conn);
   298 
   335 
   299 /**
   336 /**
   300  * Allow/forbid players to join the room. Only makes sense in room state and if you are chief.
   337  * Allow/forbid players to join the room. Only makes sense in room state and if you are chief.
   307  * The server does not send a reply.
   344  * The server does not send a reply.
   308  */
   345  */
   309 int flib_netconn_send_toggleRestrictTeams(flib_netconn *conn);
   346 int flib_netconn_send_toggleRestrictTeams(flib_netconn *conn);
   310 
   347 
   311 /**
   348 /**
   312  * Probably does something administrator-y.
   349  * Does something administrator-y. At any rate you need to be an administrator and in the lobby
       
   350  * to use this command.
   313  */
   351  */
   314 int flib_netconn_send_clearAccountsCache(flib_netconn *conn);
   352 int flib_netconn_send_clearAccountsCache(flib_netconn *conn);
   315 
   353 
   316 /**
   354 /**
   317  * Sets a server variable to the indicated value. Only makes sense if you are server admin.
   355  * Sets a server variable to the indicated value. Only makes sense if you are server admin.
   318  * Known variables are MOTD_NEW, MOTD_OLD and LATEST_PROTO.
   356  * Known variables are MOTD_NEW, MOTD_OLD and LATEST_PROTO.
   319  * TODO reply?
       
   320  */
   357  */
   321 int flib_netconn_send_setServerVar(flib_netconn *conn, const char *name, const char *value);
   358 int flib_netconn_send_setServerVar(flib_netconn *conn, const char *name, const char *value);
   322 
   359 
   323 /**
   360 /**
   324  * Queries all server variables. Only makes sense if you are server admin. (TODO: try)
   361  * Queries all server variables. Only makes sense if you are server admin.
   325  * If the action succeeds, you will receive several onServerVar callbacks with the
   362  * If the action succeeds, you will receive several onServerVar callbacks with the
   326  * current values of all server variables.
   363  * current values of all server variables.
   327  */
   364  */
   328 int flib_netconn_send_getServerVars(flib_netconn *conn);
   365 int flib_netconn_send_getServerVars(flib_netconn *conn);
   329 
   366 
   433 void flib_netconn_onLeaveRoom(flib_netconn *conn, void (*callback)(void *context, int reason, const char *message), void *context);
   470 void flib_netconn_onLeaveRoom(flib_netconn *conn, void (*callback)(void *context, int reason, const char *message), void *context);
   434 
   471 
   435 /**
   472 /**
   436  * A new team was added to the room. The person who adds a team does NOT receive this callback (he gets onTeamAccepted instead).
   473  * A new team was added to the room. The person who adds a team does NOT receive this callback (he gets onTeamAccepted instead).
   437  * The team does not contain bindings, stats, weaponset, color or the number of hogs.
   474  * The team does not contain bindings, stats, weaponset, color or the number of hogs.
       
   475  *
       
   476  * If you receive this message and you are the room chief, you are expected to provide a color and hog count for this team using
       
   477  * flib_netconn_send_teamHogCount / teamColor.
   438  */
   478  */
   439 void flib_netconn_onTeamAdd(flib_netconn *conn, void (*callback)(void *context, const flib_team *team), void *context);
   479 void flib_netconn_onTeamAdd(flib_netconn *conn, void (*callback)(void *context, const flib_team *team), void *context);
   440 
   480 
   441 /**
   481 /**
   442  * A team was removed from the room.
   482  * A team was removed from the room.
   454 
   494 
   455 /**
   495 /**
   456  * When you ask for a team to be added, the server might reject it for several reasons, e.g. because it has the same name
   496  * When you ask for a team to be added, the server might reject it for several reasons, e.g. because it has the same name
   457  * as an existing team, or because the room chief restricted adding new teams. If the team is accepted by the server,
   497  * as an existing team, or because the room chief restricted adding new teams. If the team is accepted by the server,
   458  * this callback is fired.
   498  * this callback is fired.
   459  */
   499  *
   460 void flib_netconn_onTeamAccepted(flib_netconn *conn, void (*callback)(void *context, const char *teamName), void *context);
   500  * If you are the room chief, you are expected to provide the hog count for your own team now using flib_netconn_send_teamHogCount.
       
   501  * The color of the team is already set to the one you provided in addTeam, but the QtFrontend apparently always uses 0 there and
       
   502  * instead sets the color after the team is accepted.
       
   503  */
       
   504 void flib_netconn_onTeamAccepted(flib_netconn *conn, void (*callback)(void *context, const char *team), void *context);
   461 
   505 
   462 /**
   506 /**
   463  * The number of hogs in a team has been changed by the room chief. If you are the chief and change the number of hogs yourself,
   507  * The number of hogs in a team has been changed by the room chief. If you are the chief and change the number of hogs yourself,
   464  * you will not receive this callback!
   508  * you will not receive this callback!
   465  */
   509  */
   503  * This callback is called if the server informs us that we have admin rights.
   547  * This callback is called if the server informs us that we have admin rights.
   504  */
   548  */
   505 void flib_netconn_onAdminAccess(flib_netconn *conn, void (*callback)(void *context), void *context);
   549 void flib_netconn_onAdminAccess(flib_netconn *conn, void (*callback)(void *context), void *context);
   506 
   550 
   507 /**
   551 /**
   508  * When you query the server vars with GET_SERVER_VAR (TODO probably only works as admin), the server
   552  * When you query the server vars with flib_netconn_send_getServerVars (only works as admin), the server
   509  * replies with a list of them. This callback is called for each entry in that list.
   553  * replies with a list of them. This callback is called for each entry in that list.
   510  */
   554  */
   511 void flib_netconn_onServerVar(flib_netconn *conn, void (*callback)(void *context, const char *name, const char *value), void *context);
   555 void flib_netconn_onServerVar(flib_netconn *conn, void (*callback)(void *context, const char *name, const char *value), void *context);
   512 
   556 
   513 #endif
   557 #endif