project_files/frontlib/net/netconn.h
changeset 7269 5b0aeef8ba2a
parent 7234 613998625a3c
child 7271 5608ac657362
equal deleted inserted replaced
7267:710f3ced8934 7269:5b0aeef8ba2a
     1 #ifndef NETCONN_H_
     1 #ifndef NETCONN_H_
     2 #define NETCONN_H_
     2 #define NETCONN_H_
     3 
     3 
     4 #include "../model/gamesetup.h"
     4 #include "../model/gamesetup.h"
       
     5 #include "../model/cfg.h"
       
     6 #include "../model/roomlist.h"
     5 
     7 
     6 #include <stddef.h>
     8 #include <stddef.h>
     7 #include <stdint.h>
     9 #include <stdint.h>
     8 #include <stdbool.h>
    10 #include <stdbool.h>
     9 
    11 
    10 #define NETCONN_STATE_AWAIT_CONNECTED 0
    12 #define NETCONN_STATE_CONNECTING 0
    11 #define NETCONN_STATE_LOBBY 1
    13 #define NETCONN_STATE_LOBBY 1
    12 #define NETCONN_STATE_ROOM 2
    14 #define NETCONN_STATE_ROOM 2
    13 #define NETCONN_STATE_INGAME 3
    15 #define NETCONN_STATE_INGAME 3
    14 #define NETCONN_STATE_DISCONNECTED 10
    16 #define NETCONN_STATE_DISCONNECTED 10
    15 
    17 
    16 #define NETCONN_ERROR_SERVER_TOO_OLD 1
    18 #define NETCONN_DISCONNECT_NORMAL 0
    17 #define NETCONN_ERROR_FROM_SERVER 2
    19 #define NETCONN_DISCONNECT_SERVER_TOO_OLD 1
       
    20 #define NETCONN_DISCONNECT_AUTH_FAILED 2
       
    21 #define NETCONN_DISCONNECT_INTERNAL_ERROR 100
       
    22 
       
    23 #define NETCONN_ROOMLEAVE_ABANDONED 0
       
    24 #define NETCONN_ROOMLEAVE_KICKED 1
       
    25 
       
    26 #define NETCONN_MSG_TYPE_PLAYERINFO 0
       
    27 #define NETCONN_MSG_TYPE_SERVERMESSAGE 1
       
    28 #define NETCONN_MSG_TYPE_WARNING 2
       
    29 #define NETCONN_MSG_TYPE_ERROR 3
       
    30 
    18 
    31 
    19 struct _flib_netconn;
    32 struct _flib_netconn;
    20 typedef struct _flib_netconn flib_netconn;
    33 typedef struct _flib_netconn flib_netconn;
    21 
    34 
    22 flib_netconn *flib_netconn_create(const char *playerName, const char *host, uint16_t port);
    35 flib_netconn *flib_netconn_create(const char *playerName, flib_cfg_meta *metacfg, const char *host, uint16_t port);
    23 void flib_netconn_destroy(flib_netconn *conn);
    36 void flib_netconn_destroy(flib_netconn *conn);
    24 
       
    25 /**
       
    26  * This is called when we can't stay connected due to a problem, e.g. because the
       
    27  * server version is too old, or we are unexpectedly disconnected.
       
    28  *
       
    29  * Once this callback has been called, you should destroy the flib_netconn.
       
    30  */
       
    31 void flib_netconn_onError(flib_netconn *conn, void (*callback)(void *context, int errorCode, const char *errormsg), void* context);
       
    32 
       
    33 /**
       
    34  * This is called when we receive a CONNECTED message from the server, which should be the first
       
    35  * message arriving from the server.
       
    36  */
       
    37 void flib_netconn_onConnected(flib_netconn *conn, void (*callback)(void *context, const char *serverMessage), void* context);
       
    38 
    37 
    39 /**
    38 /**
    40  * Perform I/O operations and call callbacks if something interesting happens.
    39  * Perform I/O operations and call callbacks if something interesting happens.
    41  * Should be called regularly.
    40  * Should be called regularly.
    42  */
    41  */
    43 void flib_netconn_tick(flib_netconn *conn);
    42 void flib_netconn_tick(flib_netconn *conn);
    44 
    43 
       
    44 
       
    45 /**
       
    46  * Return the current roomlist. Don't free or modify.
       
    47  */
       
    48 const flib_roomlist *flib_netconn_get_roomlist(flib_netconn *conn);
       
    49 
       
    50 /**
       
    51  * Are you currently the owner of this room? The return value only makes sense in
       
    52  * NETCONN_STATE_ROOM and NETCONN_STATE_INGAME states.
       
    53  */
       
    54 bool flib_netconn_is_chief(flib_netconn *conn);
       
    55 
       
    56 /**
       
    57  * quitmsg may be null
       
    58  */
       
    59 int flib_netconn_send_quit(flib_netconn *conn, const char *quitmsg);
       
    60 int flib_netconn_send_chat(flib_netconn *conn, const char *chat);
       
    61 
       
    62 /**
       
    63  * Note: Most other functions in this lib accept UTF-8, but the password needs to be
       
    64  * sent as latin1
       
    65  */
       
    66 int flib_netconn_send_password(flib_netconn *conn, const char *latin1Passwd);
       
    67 
       
    68 /**
       
    69  * Request a different nickname.
       
    70  * This function only makes sense in reaction to an onNickTaken callback, because the netconn automatically
       
    71  * requests the nickname you provide on creation, and once the server accepts the nickname (onNickAccept)
       
    72  * it can no longer be changed.
       
    73  *
       
    74  * As a response to the nick change request, the server will either reply with a confirmation (onNickAccept)
       
    75  * or a rejection (onNickTaken). Note that the server confirms a nick even if it is password protected, the
       
    76  * password request happens afterwards.
       
    77  */
       
    78 int flib_netconn_send_nick(flib_netconn *conn, const char *nick);
       
    79 
       
    80 /**
       
    81  * Callback for several informational messages that should be displayed to the user
       
    82  * (e.g. in the chat window), but do not require a reaction. If a game is running, you might
       
    83  * want to redirect some of these messages to the engine as well so the user will see them.
       
    84  */
       
    85 void flib_netconn_onMessage(flib_netconn *conn, void (*callback)(void *context, int msgtype, const char *msg), void* context);
       
    86 
       
    87 /**
       
    88  * We received a chat message. Where this message belongs depends on the current state (lobby/room/game). In particular,
       
    89  * if a game is running the message should be passed to the engine.
       
    90  */
       
    91 void flib_netconn_onChat(flib_netconn *conn, void (*callback)(void *context, const char *nick, const char *msg), void* context);
       
    92 
       
    93 /**
       
    94  * This is called when we receive a CONNECTED message from the server, which should be the first
       
    95  * message arriving from the server.
       
    96  */
       
    97 void flib_netconn_onConnected(flib_netconn *conn, void (*callback)(void *context), void* context);
       
    98 
       
    99 /**
       
   100  * This is *always* the last callback (unless the netconn is destroyed early), and the netconn should be destroyed when it is received.
       
   101  * The reason is one of the NETCONN_DISCONNECT_ constants. Sometime a message is included as well, but that parameter might
       
   102  * also be NULL.
       
   103  */
       
   104 void flib_netconn_onDisconnected(flib_netconn *conn, void (*callback)(void *context, int reason, const char *message), void* context);
       
   105 
       
   106 /**
       
   107  * Callbacks for room list updates. The room list is managed automatically and can be queried with
       
   108  * flib_netconn_get_roomlist() as soon as the onConnected callback is fired. These callbacks
       
   109  * provide notification about changes.
       
   110  */
       
   111 void flib_netconn_onRoomAdd(flib_netconn *conn, void (*callback)(void *context, const flib_roomlist_room *room), void* context);
       
   112 void flib_netconn_onRoomDelete(flib_netconn *conn, void (*callback)(void *context, const char *name), void* context);
       
   113 void flib_netconn_onRoomUpdate(flib_netconn *conn, void (*callback)(void *context, const char *oldName, const flib_roomlist_room *room), void* context);
       
   114 
       
   115 /**
       
   116  * Callbacks for players joining or leaving a room or the lobby. If join is true it's a join, otherwise a leave.
       
   117  * NOTE: partMessage is null if no parting message was given.
       
   118  */
       
   119 void flib_netconn_onLobbyJoin(flib_netconn *conn, void (*callback)(void *context, const char *nick), void* context);
       
   120 void flib_netconn_onLobbyLeave(flib_netconn *conn, void (*callback)(void *context, const char *nick, const char *partMessage), void* context);
       
   121 void flib_netconn_onRoomJoin(flib_netconn *conn, void (*callback)(void *context, const char *nick), void* context);
       
   122 void flib_netconn_onRoomLeave(flib_netconn *conn, void (*callback)(void *context, const char *nick, const char *partMessage), void* context);
       
   123 
       
   124 /**
       
   125  * onNickTaken is called on connecting to the server, if it turns out that there is already a player with the same nick.
       
   126  * In order to proceed, a new nickname needs to be sent to the server using flib_netconn_send_nick() (or of course you can
       
   127  * bail out and send a QUIT). If you don't set a callback, the netconn will automatically react by generating a new name.
       
   128  * Once the server accepts a name, you will be informed with an onNickAccept callback.
       
   129  */
       
   130 void flib_netconn_onNickTaken(flib_netconn *conn, void (*callback)(void *context, const char *nick), void* context);
       
   131 
       
   132 /**
       
   133  * onNickAccept informs that your nickname has been accepted by the server, i.e. there was nobody with that nick already
       
   134  * on the server.
       
   135  * Note that a nick request is sent automatically by the netconn when you join the server, so you should receive this
       
   136  * callback shortly after connecting.
       
   137  */
       
   138 void flib_netconn_onNickAccept(flib_netconn *conn, void (*callback)(void *context, const char *nick), void* context);
       
   139 
       
   140 /**
       
   141  * When connecting with a registered nickname, the server will ask for a password before admitting you in.
       
   142  * This callback is called when that happens. As a reaction, you can send the password using
       
   143  * flib_netconn_send_password or choose a different nick. If you don't register a callback,
       
   144  * the default behavior is to just quit in a way that will cause a disconnect with NETCONN_DISCONNECT_AUTH_FAILED.
       
   145  */
       
   146 void flib_netconn_onPasswordRequest(flib_netconn *conn, void (*callback)(void *context, const char *nick), void* context);
       
   147 
       
   148 /**
       
   149  * This callback informs about changes to your room chief status, i.e. whether you are allowed to
       
   150  * modify the current room. Generally when you create a room you start out being room chief, and
       
   151  * when you join an existing room you are not. However, in some situations room ownership can change,
       
   152  * and if that happens this callback is called with the new status.
       
   153  *
       
   154  * Note: This callback does not automatically fire when joining a room. You can always query the
       
   155  * current chief status using flib_netconn_is_chief().
       
   156  */
       
   157 void flib_netconn_onRoomChiefStatus(flib_netconn *conn, void (*callback)(void *context, bool chief), void* context);
       
   158 
       
   159 /**
       
   160  * One of the players in the room (possibly you!) changed their ready state.
       
   161  */
       
   162 void flib_netconn_onReadyStateCb(flib_netconn *conn, void (*callback)(void *context, const char *nick, bool ready), void* context);
       
   163 
       
   164 /**
       
   165  * You just left the lobby and entered a room.
       
   166  * If chief is true, you can and should send a full configuration for the room now.
       
   167  */
       
   168 void flib_netconn_onEnterRoomCb(flib_netconn *conn, void (*callback)(void *context, bool chief), void *context);
       
   169 
       
   170 /**
       
   171  * You just left a room and entered the lobby again.
       
   172  * reason is one of the NETCONN_ROOMLEAVE_ constants.
       
   173  * This will not be called when you actively leave a room using PART.
       
   174  */
       
   175 void flib_netconn_onLeaveRoomCb(flib_netconn *conn, void (*callback)(void *context, int reason, const char *message), void *context);
       
   176 
       
   177 void flib_netconn_onTeamAddCb(flib_netconn *conn, void (*callback)(void *context, flib_team *team), void *context);
       
   178 
    45 #endif
   179 #endif