📄 server_8h-source.html
字号:
00134 <span class="comment">// the disconnect packet</span>00135 <a class="code" href="classbuffer__c.html">buffer_c</a> disconnect_packet_cache;00136 00137 <span class="comment">// time of last packet received by server from this client. to check client timeouts.</span>00138 <span class="keywordtype">double</span> last_received_packet;00139 00140 <span class="comment">// custom data given by gameserver in the "accept client" callback</span>00141 <a class="code" href="classbuffer__c.html">buffer_c</a> connect_answer_cache;00142 00143 <span class="comment">// NEW (v1.3.0): store reason/error code to pass by leet_client_disconnected(). 0 = generic/unspecified reason</span>00144 <span class="keywordtype">int</span> disconnection_reason_code;00145 00146 <span class="comment">//ctor</span>00147 remote_client_c(server_c *svr, <a class="code" href="classconsole__c.html">console_c</a> *conio = 0) {00148 server = svr;00149 station.set_console(conio);00150 00151 <span class="comment">//reader_thread = (NLthreadID)NL_INVALID;</span>00152 remote_addr.valid = NL_FALSE;00153 told_disconnect = <span class="keyword">false</span>;00154 server_disconnected = <span class="keyword">false</span>;00155 last_received_packet = <a class="code" href="utils_8h.html#a13">get_time</a>();00156 channel_port = LEET_CHANNEL_PORT_PUSH;00157 disconnect_client = <span class="keyword">false</span>;00158 disconnection_reason_code = 0;00159 }00160 00161 <span class="comment">//dtor</span>00162 <span class="keyword">virtual</span> ~remote_client_c() {00163 }00164 };00165 00166 <span class="comment">// leetserver class</span>00167 <span class="keyword">class </span>server_c : <span class="keyword">public</span> leet_c, <span class="keyword">public</span> station_callback_i {00168 <span class="keyword">public</span>:00169 00170 <span class="comment">//ctor, setting behavior. behavior will **NOT** be deleted by the server's destructor!</span>00171 server_c(server_behavior_i *beh, <a class="code" href="classconsole__c.html">console_c</a> *conio = 0);00172 00173 <span class="comment">//dtor</span>00174 <span class="keyword">virtual</span> ~server_c();00175 00176 <span class="comment">// configure usage of packet compression. to turn on the sending of compressed packets at any time, </span>00177 <span class="comment">// pass a value >=0 as argument, which specifies the minimum size that the packet must have, in bytes, </span>00178 <span class="comment">// to be compressed (pass 0 to compress all outgoing packets). to turn off the sending of compressed </span>00179 <span class="comment">// packets at any time, call this method with no parameters (or a negative value as parameter). </span>00180 <span class="comment">// NOTE: this call resets the compression stats.</span>00181 <span class="keywordtype">void</span> set_compression(<span class="keywordtype">int</span> minimum_packet_size = -1);00182 00186 <span class="keywordtype">void</span> set_default_send_message_policy(<span class="keywordtype">int</span> send_policy) { default_send_message_policy = send_policy; }00187 00188 <span class="comment">//start server at port, with a maximum acceptable number of players. returns 0 if fails, 1 on success.</span>00189 <span class="comment">//channel_port_num: port for TCP listen socket. set to LEET_CHANNEL_PORT_PUSH to make the client listen</span>00190 <span class="comment">//for connections instead. </span>00191 <span class="comment">//threads_prio: when using ZIG_SERVER_THREADING_1_1, the thread priority assignment strategy to be used </span>00192 <span class="comment">//(see zig.h)</span>00193 <span class="comment">//threadint_strategy: the threading strategy to be used for dealing with multiple clients (see zig.h) </span>00194 <span class="keywordtype">int</span> start(<span class="keywordtype">int</span> port_num, <span class="keywordtype">int</span> max_players, <span class="keywordtype">int</span> channel_port_num, <span class="keywordtype">int</span> threads_prio = <a class="code" href="zig_8h.html#a25a13">ZIGTHREADSPRIO_NET_HIGH</a>, <span class="keywordtype">int</span> threading_strategy = <a class="code" href="zig_8h.html#a24a9">ZIG_SERVER_THREADING_1_N</a>);00195 00196 <span class="comment">//stops the server. parameter is the number of seconds to wait for finishing all stuff nicely.</span>00197 <span class="keywordtype">void</span> stop(<span class="keywordtype">int</span> timeout = 3);00198 00199 <span class="comment">// ZIG_SERVER_THREADING_0: when this option is passed to start(), then server_c will NOT create </span>00200 <span class="comment">// background threads for polling client sockets, polling the server request socket or check </span>00201 <span class="comment">// for client timeouts. in this situation you must call this method periodically to make </span>00202 <span class="comment">// server_c do all of this. this method does nothing if the server is not started. </span>00203 <span class="comment">// RETURN VALUE: returns the number of client sockets that had pending packets to be </span>00204 <span class="comment">// processed (this call only processes a maximum of ONE pending packet per client, so </span>00205 <span class="comment">// you might want to call it several times in a row, altough generally it should not be </span>00206 <span class="comment">// necessary to do so)</span>00207 <span class="keywordtype">int</span> poll_sockets();00208 00209 <span class="comment">// similar to poll_sockets(), but processes ALL data pending on a client's socket before </span>00210 <span class="comment">// iterating to the next client. if the parameter "drop_unreliable" is set to true, then </span>00211 <span class="comment">// the unreliable blocks of data in client packets are all dropped as quickly as possible.</span>00212 <span class="comment">// if the parameter "drop_requests" is set to true, then the "request" socket of the server </span>00213 <span class="comment">// (which receives serverinfo and connection requests) is also emptied, having all of its </span>00214 <span class="comment">// packets discarded. </span>00215 <span class="comment">// NOTE: poll_all_sockets() only deals with incoming connected client data; it does not </span>00216 <span class="comment">// do the other checks that poll_sockets() also does (process incoming client connection</span>00217 <span class="comment">// requests, check for client timeouts and disconnections, etc.).</span>00218 <span class="keywordtype">void</span> poll_all_sockets(<span class="keywordtype">bool</span> drop_requests, <span class="keywordtype">bool</span> drop_unreliable);00219 00220 <span class="comment">//set client timeout. client will be disconnected automatically if it does not send any packets</span>00221 <span class="comment">//in the specified timeout in seconds.</span>00222 <span class="keywordtype">void</span> set_client_timeout(<span class="keywordtype">int</span> timeout = 30) { client_timeout = timeout; }00223 00224 <span class="comment">//disconnects a specific client. timeout = seconds to wait before just shooting the client</span>00225 <span class="comment">//return: 0 fail (no such client?), 1 success.</span>00226 <span class="keywordtype">int</span> disconnect_client(<span class="keywordtype">int</span> client_id, <span class="keywordtype">int</span> timeout = 2);00227 00228 <span class="comment">//disconnects a specific client. timeout = seconds to wait before just shooting the client</span>00229 <span class="comment">//return: 0 fail (no such client?), 1 success.</span>00230 <span class="comment">//additional parameter: custom disconnect data to be passed to the client.</span>00231 <span class="keywordtype">int</span> disconnect_client(<span class="keywordtype">int</span> client_id, <a class="code" href="classbuffer__c.html">buffer_c</a> reason, <span class="keywordtype">int</span> timeout = 2);00232 00233 <span class="comment">//send a game frame to a given client. must be called regularly (~100ms), but there are no hard</span>00234 <span class="comment">//requirements for the interval.</span>00235 <span class="comment">//returns 0 on failure, 1 on success.</span>00236 <span class="comment">//returns the allocated packet id in the optional packet_id pointer</span>00237 <span class="keywordtype">int</span> send_frame(<span class="keywordtype">int</span> client_id, <a class="code" href="classbuffer__c.html">buffer_c</a> &framebuf, <span class="keywordtype">int</span> *packet_id = 0);00238 00239 <span class="comment">//sends the given reliable message to the given client. reliable = heavy, do not use for frequent</span>00240 <span class="comment">//world update data. use for gamestate changes, talk messages and other stuff the client can't miss, or</span>00241 <span class="comment">//stuff he can even miss but it's better if he doesn't and the message is so infrequent and small that</span>00242 <span class="comment">//it's worth it.</span>00243 <span class="comment">//returns 0 on failure, 1 on success</span>00244 <span class="keywordtype">int</span> send_message(<span class="keywordtype">int</span> client_id, <a class="code" href="classbuffer__c.html">buffer_c</a> &message, <span class="keywordtype">int</span> policy = RSP_USE_DEFAULT);00245 00246 <span class="comment">//broadcasts the given reliable message to all active clients. return: 0 fail, 1 success.</span>00247 <span class="keywordtype">int</span> broadcast_message(<a class="code" href="classbuffer__c.html">buffer_c</a> &message, <span class="keywordtype">int</span> policy = RSP_USE_DEFAULT);00248 00249 <span class="comment">//retrieve the next pending message from a client</span>00250 <span class="comment">//if no more messages returns buffer.code == 0, else returns buffer.code == 1</span>00251 <span class="comment">//returns buffer.code == -1 if client_id not found</span>00252 <a class="code" href="classbuffer__c.html">buffer_c</a> receive_message(<span class="keywordtype">int</span> client_id);00253 00254 <span class="comment">//retrieve the next pending message from a client. returns 0 if no more messages, 1 if message read ok.</span>00255 <span class="comment">//buffer paramenter: a pre-allocated buffer for putting the message in</span>00256 <span class="comment">//returns -1 if client_id not found</span>00257 <span class="keywordtype">int</span> receive_message(<span class="keywordtype">int</span> client_id, <a class="code" href="classbuffer__c.html">buffer_c</a> &msgbuf);00258 00259 <span class="comment">//---- UDP REQUEST/RESPONSE API --------------------------------------</span>00260 00261 <span class="comment">//make a blocking UDP request to a connected client. see station_c::make_request() at station.h</span>00262 <a class="code" href="classbuffer__c.html">buffer_c</a> make_request(<span class="keywordtype">int</span> client_id, <a class="code" href="classbuffer__c.html">buffer_c</a> &req, <span class="keywordtype">int</span> timeout_ms, <span class="keyword">volatile</span> <span class="keywordtype">bool</span> *cancel_flag, <span class="keywordtype">int</span> interval_ms = 200, <span class="keywordtype">int</span> send_ms = -1);00263 00264 <span class="comment">//---- TCP CHANNELS API ----------------------------------------------</span>00265 00266 <span class="comment">//call station of the right client to open a new channel.</span>00267 <span class="comment">//returns the ID of the channel that will be created or 0 if a channel can't be opened.</span>00268 <span class="keywordtype">int</span> open_channel(<span class="keywordtype">int</span> client_id);00269 00270 <span class="comment">//close channel. returns 1 if ok, or 0 if failed.</span>00271 <span class="keywordtype">int</span> close_channel(<span class="keywordtype">int</span> client_id, <span class="keywordtype">int</span> channel_id);00272 00273 <span class="comment">// channel write. returns result of nlWrite.</span>00274 <span class="keywordtype">int</span> chan_write(<span class="keywordtype">int</span> client_id, <span class="keywordtype">int</span> channel_id, <a class="code" href="classbuffer__c.html">buffer_c</a> &out, <span class="keywordtype">bool</span> timeout_is_failure = <span class="keyword">true</span>, <span class="keywordtype">double</span> timeout = 3.0);00275 <span class="keywordtype">int</span> chan_write(<span class="keywordtype">int</span> client_id, <span class="keywordtype">int</span> channel_id, NLubyte out, <span class="keywordtype">bool</span> timeout_is_failure = <span class="keyword">true</span>, <span class="keywordtype">double</span> timeout = 3.0);00276 <span class="keywordtype">int</span> chan_write(<span class="keywordtype">int</span> client_id, <span class="keywordtype">int</span> channel_id, NLbyte out, <span class="keywordtype">bool</span> timeout_is_failure = <span class="keyword">true</span>, <span class="keywordtype">double</span> timeout = 3.0);00277 <span class="keywordtype">int</span> chan_write(<span class="keywordtype">int</span> client_id, <span class="keywordtype">int</span> channel_id, NLushort out, <span class="keywordtype">bool</span> timeout_is_failure = <span class="keyword">true</span>, <span class="keywordtype">double</span> timeout = 3.0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -