project_files/frontlib/net/netconn_send.c
changeset 7580 c92596feac0d
parent 7504 ed1d52c5aa94
child 10017 de822cd3df3a
equal deleted inserted replaced
7578:d5c880e6d772 7580:c92596feac0d
    52 		return -1;
    52 		return -1;
    53 	}
    53 	}
    54 	return flib_netbase_sendf(conn->netBase, "%s\n%i\n\n", cmdname, param);
    54 	return flib_netbase_sendf(conn->netBase, "%s\n%i\n\n", cmdname, param);
    55 }
    55 }
    56 
    56 
    57 int flib_netconn_send_quit(flib_netconn *conn, const char *quitmsg) {
       
    58 	return sendStr(conn, "QUIT", (quitmsg && *quitmsg) ? quitmsg : "User quit");
       
    59 }
       
    60 
       
    61 int flib_netconn_send_chat(flib_netconn *conn, const char *chat) {
       
    62 	if(!flib_strempty(chat)) {
       
    63 		return sendStr(conn, "CHAT", chat);
       
    64 	}
       
    65 	return 0;
       
    66 }
       
    67 
       
    68 int flib_netconn_send_teamchat(flib_netconn *conn, const char *chat) {
       
    69 	if(!flib_strempty(chat)) {
       
    70 		return sendStr(conn, "TEAMCHAT", chat);
       
    71 	}
       
    72 	return 0;
       
    73 }
       
    74 
       
    75 int flib_netconn_send_nick(flib_netconn *conn, const char *nick) {
    57 int flib_netconn_send_nick(flib_netconn *conn, const char *nick) {
    76 	int result = -1;
    58 	int result = -1;
    77 	if(!log_badargs_if2(conn==NULL, flib_strempty(nick))) {
    59 	if(!log_badargs_if2(conn==NULL, flib_strempty(nick))) {
    78 		char *tmpName = flib_strdupnull(nick);
    60 		char *tmpName = flib_strdupnull(nick);
    79 		if(tmpName) {
    61 		if(tmpName) {
   105 		result = flib_netbase_sendf(conn->netBase, "%s\n%s\n\n", "PASSWORD", md5hex);
    87 		result = flib_netbase_sendf(conn->netBase, "%s\n%s\n\n", "PASSWORD", md5hex);
   106 	}
    88 	}
   107 	return result;
    89 	return result;
   108 }
    90 }
   109 
    91 
       
    92 int flib_netconn_send_quit(flib_netconn *conn, const char *quitmsg) {
       
    93 	return sendStr(conn, "QUIT", (quitmsg && *quitmsg) ? quitmsg : "User quit");
       
    94 }
       
    95 
       
    96 int flib_netconn_send_chat(flib_netconn *conn, const char *chat) {
       
    97 	if(!flib_strempty(chat)) {
       
    98 		return sendStr(conn, "CHAT", chat);
       
    99 	}
       
   100 	return 0;
       
   101 }
       
   102 
       
   103 int flib_netconn_send_kick(flib_netconn *conn, const char *playerName) {
       
   104 	return sendStr(conn, "KICK", playerName);
       
   105 }
       
   106 
       
   107 int flib_netconn_send_playerInfo(flib_netconn *conn, const char *playerName) {
       
   108 	return sendStr(conn, "INFO", playerName);
       
   109 }
       
   110 
   110 int flib_netconn_send_request_roomlist(flib_netconn *conn) {
   111 int flib_netconn_send_request_roomlist(flib_netconn *conn) {
   111 	return sendVoid(conn, "LIST");
   112 	return sendVoid(conn, "LIST");
   112 }
   113 }
   113 
   114 
   114 int flib_netconn_send_joinRoom(flib_netconn *conn, const char *room) {
   115 int flib_netconn_send_joinRoom(flib_netconn *conn, const char *room) {
   117 		return 0;
   118 		return 0;
   118 	}
   119 	}
   119 	return -1;
   120 	return -1;
   120 }
   121 }
   121 
   122 
       
   123 int flib_netconn_send_playerFollow(flib_netconn *conn, const char *playerName) {
       
   124 	return sendStr(conn, "FOLLOW", playerName);
       
   125 }
       
   126 
   122 int flib_netconn_send_createRoom(flib_netconn *conn, const char *room) {
   127 int flib_netconn_send_createRoom(flib_netconn *conn, const char *room) {
   123 	if(!sendStr(conn, "CREATE_ROOM", room)) {
   128 	if(!sendStr(conn, "CREATE_ROOM", room)) {
   124 		conn->isChief = true;
   129 		conn->isChief = true;
   125 		return 0;
   130 		return 0;
   126 	}
   131 	}
   127 	return -1;
   132 	return -1;
   128 }
   133 }
   129 
   134 
   130 int flib_netconn_send_renameRoom(flib_netconn *conn, const char *roomName) {
   135 int flib_netconn_send_ban(flib_netconn *conn, const char *playerName) {
   131 	return sendStr(conn, "ROOM_NAME", roomName);
   136 	return sendStr(conn, "BAN", playerName);
   132 }
   137 }
   133 
   138 
       
   139 int flib_netconn_send_clearAccountsCache(flib_netconn *conn) {
       
   140 	return sendVoid(conn, "CLEAR_ACCOUNTS_CACHE");
       
   141 }
       
   142 
       
   143 int flib_netconn_send_setServerVar(flib_netconn *conn, const char *name, const char *value) {
       
   144 	if(log_badargs_if3(conn==NULL, flib_strempty(name), flib_strempty(value))) {
       
   145 		return -1;
       
   146 	}
       
   147 	return flib_netbase_sendf(conn->netBase, "%s\n%s\n%s\n\n", "SET_SERVER_VAR", name, value);
       
   148 }
       
   149 
       
   150 int flib_netconn_send_getServerVars(flib_netconn *conn) {
       
   151 	return sendVoid(conn, "GET_SERVER_VAR");
       
   152 }
   134 int flib_netconn_send_leaveRoom(flib_netconn *conn, const char *str) {
   153 int flib_netconn_send_leaveRoom(flib_netconn *conn, const char *str) {
   135 	int result = -1;
   154 	int result = -1;
   136 	if(conn->netconnState==NETCONN_STATE_ROOM) {
   155 	if(conn->netconnState==NETCONN_STATE_ROOM) {
   137 		result = (str && *str) ? sendStr(conn, "PART", str) : sendVoid(conn, "PART");
   156 		result = (str && *str) ? sendStr(conn, "PART", str) : sendVoid(conn, "PART");
   138 		if(!result) {
   157 		if(!result) {
   196 		return 0;
   215 		return 0;
   197 	}
   216 	}
   198 	return -1;
   217 	return -1;
   199 }
   218 }
   200 
   219 
   201 int flib_netconn_send_engineMessage(flib_netconn *conn, const uint8_t *message, size_t size) {
   220 int flib_netconn_send_renameRoom(flib_netconn *conn, const char *roomName) {
   202 	int result = -1;
   221 	return sendStr(conn, "ROOM_NAME", roomName);
   203 	if(!log_badargs_if2(conn==NULL, message==NULL && size>0)) {
       
   204 		char *base64encout = NULL;
       
   205 		base64_encode_alloc((const char*)message, size, &base64encout);
       
   206 		if(base64encout) {
       
   207 			result = flib_netbase_sendf(conn->netBase, "EM\n%s\n\n", base64encout);
       
   208 		}
       
   209 		free(base64encout);
       
   210 	}
       
   211 	return result;
       
   212 }
   222 }
   213 
   223 
   214 int flib_netconn_send_teamHogCount(flib_netconn *conn, const char *teamname, int hogcount) {
   224 int flib_netconn_send_teamHogCount(flib_netconn *conn, const char *teamname, int hogcount) {
   215 	if(!log_badargs_if5(conn==NULL, flib_strempty(teamname), hogcount<1, hogcount>HEDGEHOGS_PER_TEAM, !conn->isChief)
   225 	if(!log_badargs_if5(conn==NULL, flib_strempty(teamname), hogcount<1, hogcount>HEDGEHOGS_PER_TEAM, !conn->isChief)
   216 			&& !flib_netbase_sendf(conn->netBase, "HH_NUM\n%s\n%i\n\n", teamname, hogcount)) {
   226 			&& !flib_netbase_sendf(conn->netBase, "HH_NUM\n%s\n%i\n\n", teamname, hogcount)) {
   445 		netconn_setScheme(conn, scheme);
   455 		netconn_setScheme(conn, scheme);
   446 	}
   456 	}
   447 	return result;
   457 	return result;
   448 }
   458 }
   449 
   459 
       
   460 int flib_netconn_send_startGame(flib_netconn *conn) {
       
   461 	return sendVoid(conn, "START_GAME");
       
   462 }
       
   463 
       
   464 int flib_netconn_send_toggleRestrictJoins(flib_netconn *conn) {
       
   465 	return sendVoid(conn, "TOGGLE_RESTRICT_JOINS");
       
   466 }
       
   467 
       
   468 int flib_netconn_send_toggleRestrictTeams(flib_netconn *conn) {
       
   469 	return sendVoid(conn, "TOGGLE_RESTRICT_TEAMS");
       
   470 }
       
   471 
       
   472 int flib_netconn_send_teamchat(flib_netconn *conn, const char *chat) {
       
   473 	if(!flib_strempty(chat)) {
       
   474 		return sendStr(conn, "TEAMCHAT", chat);
       
   475 	}
       
   476 	return 0;
       
   477 }
       
   478 
       
   479 int flib_netconn_send_engineMessage(flib_netconn *conn, const uint8_t *message, size_t size) {
       
   480 	int result = -1;
       
   481 	if(!log_badargs_if2(conn==NULL, message==NULL && size>0)) {
       
   482 		char *base64encout = NULL;
       
   483 		base64_encode_alloc((const char*)message, size, &base64encout);
       
   484 		if(base64encout) {
       
   485 			result = flib_netbase_sendf(conn->netBase, "EM\n%s\n\n", base64encout);
       
   486 		}
       
   487 		free(base64encout);
       
   488 	}
       
   489 	return result;
       
   490 }
       
   491 
   450 int flib_netconn_send_roundfinished(flib_netconn *conn, bool withoutError) {
   492 int flib_netconn_send_roundfinished(flib_netconn *conn, bool withoutError) {
   451 	return sendInt(conn, "ROUNDFINISHED", withoutError ? 1 : 0);
   493 	return sendInt(conn, "ROUNDFINISHED", withoutError ? 1 : 0);
   452 }
   494 }
   453 
   495 
   454 int flib_netconn_send_ban(flib_netconn *conn, const char *playerName) {
       
   455 	return sendStr(conn, "BAN", playerName);
       
   456 }
       
   457 
       
   458 int flib_netconn_send_kick(flib_netconn *conn, const char *playerName) {
       
   459 	return sendStr(conn, "KICK", playerName);
       
   460 }
       
   461 
       
   462 int flib_netconn_send_playerInfo(flib_netconn *conn, const char *playerName) {
       
   463 	return sendStr(conn, "INFO", playerName);
       
   464 }
       
   465 
       
   466 int flib_netconn_send_playerFollow(flib_netconn *conn, const char *playerName) {
       
   467 	return sendStr(conn, "FOLLOW", playerName);
       
   468 }
       
   469 
       
   470 int flib_netconn_send_startGame(flib_netconn *conn) {
       
   471 	return sendVoid(conn, "START_GAME");
       
   472 }
       
   473 
       
   474 int flib_netconn_send_toggleRestrictJoins(flib_netconn *conn) {
       
   475 	return sendVoid(conn, "TOGGLE_RESTRICT_JOINS");
       
   476 }
       
   477 
       
   478 int flib_netconn_send_toggleRestrictTeams(flib_netconn *conn) {
       
   479 	return sendVoid(conn, "TOGGLE_RESTRICT_TEAMS");
       
   480 }
       
   481 
       
   482 int flib_netconn_send_clearAccountsCache(flib_netconn *conn) {
       
   483 	return sendVoid(conn, "CLEAR_ACCOUNTS_CACHE");
       
   484 }
       
   485 
       
   486 int flib_netconn_send_setServerVar(flib_netconn *conn, const char *name, const char *value) {
       
   487 	if(log_badargs_if3(conn==NULL, flib_strempty(name), flib_strempty(value))) {
       
   488 		return -1;
       
   489 	}
       
   490 	return flib_netbase_sendf(conn->netBase, "%s\n%s\n%s\n\n", "SET_SERVER_VAR", name, value);
       
   491 }
       
   492 
       
   493 int flib_netconn_send_getServerVars(flib_netconn *conn) {
       
   494 	return sendVoid(conn, "GET_SERVER_VAR");
       
   495 }