#include "config.h"
#include "tConsole.h"
#include "tLocale.h"
#include "defs.h"
#include <string>
#include <iostream>
#include "nNetwork.h"
Go to the source code of this file.
Defines | |
#define | MAX 70000 |
Functions | |
void | LoginLogout () |
void | sample_message_handler (nMessage &m) |
void | sample_message_handler2 (nMessage &m) |
void | server () |
void | client (const tString &serv) |
int | main (int argnum, char **arglist) |
Variables | |
bool | Transmitted [MAX] |
static nCallbackLoginLogout | loginlogout (LoginLogout) |
nDescriptor | message_test (31,&sample_message_handler,"message_test") |
nDescriptor | message_test2 (32,&sample_message_handler2,"message_test2") |
unsigned short | client_gamestate [MAXCLIENTS+2] |
#define MAX 70000 |
Definition at line 39 of file l2_demo.cpp.
Referenced by nQueryMessageStats::Block(), client(), nQueryMessageStats::FloodProtection(), LoginLogout(), main(), nQueryMessageStats::nQueryMessageStats(), tPolynomial< T >::operator+(), tPolynomial< T >::operator+=(), and operator==().
void client | ( | const tString & | serv | ) |
Definition at line 129 of file l2_demo.cpp.
References nMessage::BroadCast(), con, MAX, message_test2, nCLIENT, nSTANDALONE, sn_Connect(), sn_GetNetState(), sn_MessagesPending(), sn_Receive(), sn_SendPlanned(), and sn_SetNetState().
Referenced by main().
00129 { 00130 sn_Connect(serv); // initialise client state and connect to the given server 00131 00132 if (sn_GetNetState()!=nCLIENT){ 00133 con << "Login failed.\n"; 00134 return; 00135 } 00136 00137 nMessage *m; 00138 int i; 00139 00140 /* 00141 con << "\n\nFirst, we send a single short (17) to the server:\n"; 00142 00143 m=new nMessage(message_test); 00144 (*m) << (short)17; 00145 m->BroadCast(); 00146 sn_Receive(); 00147 00148 usleep(3000000); 00149 00150 con << "\n\nThen, we try three shorts(10,1,7):\n"; 00151 00152 m=new nMessage(message_test); 00153 (*m) << (short)10; 00154 (*m) << (short)1; 00155 (*m) << (short)7; 00156 m->BroadCast(); 00157 sn_Receive(); 00158 00159 usleep(3000000); 00160 00161 con << "\n\nLet's see what happens if we forget the type cast\n" 00162 << "and send our number (17) as int:\n"; 00163 00164 m=new nMessage(message_test); 00165 (*m) << 17; 00166 m->BroadCast(); 00167 sn_Receive(); 00168 00169 // wait before we exit, getting messages and repeating lost ones 00170 for(i=300;i>=0;i--){ 00171 sn_Receive(); 00172 usleep(1000); 00173 } 00174 00175 con << "\nAha. Thought so :-> It arrives as two shorts.\n\n\n"; 00176 */ 00177 con << "\nSending all numbers from zero to " << MAX-1 << ":\n\n\n"; 00178 00179 for ( unsigned int j = 0; j < MAX; ++j ) 00180 { 00181 // don't let too many messages be pending 00182 while( sn_MessagesPending(0) > 10){ 00183 sn_Receive(); 00184 sn_SendPlanned(); 00185 usleep(1000); 00186 } 00187 00188 m=new nMessage(message_test2); 00189 (*m) << j; 00190 m->BroadCast(); 00191 sn_Receive(); 00192 sn_SendPlanned(); 00193 } 00194 00195 // wait before we exit, getting messages and repeating lost ones 00196 for(i=300;i>=0 || sn_MessagesPending(0) > 0; i--){ 00197 con << "Pending messages: " << sn_MessagesPending(0) << "\n"; 00198 sn_Receive(); 00199 sn_SendPlanned(); 00200 usleep(1000); 00201 } 00202 00203 sn_SetNetState(nSTANDALONE); 00204 usleep(1000); 00205 sn_Receive(); 00206 sn_SendPlanned(); 00207 }
void LoginLogout | ( | ) |
Definition at line 43 of file l2_demo.cpp.
References con, nCallbackLoginLogout::Login(), MAX, nSERVER, sn_GetNetState(), and Transmitted.
00044 { 00045 con << "LoginLogout()\n"; 00046 00047 if ( nSERVER != sn_GetNetState() ) 00048 return; 00049 00050 bool failed = false; 00051 00052 if ( !nCallbackLoginLogout::Login() ) 00053 { 00054 con << "Client quit; checking messages.\n"; 00055 for ( int i = MAX-1; i>=0; --i ) 00056 if ( !Transmitted[i] ) 00057 { 00058 con << "Message " << i << " has not been transmitted.\n"; 00059 failed = true; 00060 i-=MAX/10; 00061 } 00062 } 00063 00064 if ( failed ) 00065 exit(-1); 00066 00067 for ( int i = MAX-1; i>=0; --i ) 00068 Transmitted[i] = false; 00069 }
int main | ( | int | argnum, | |
char ** | arglist | |||
) |
Definition at line 211 of file l2_demo.cpp.
References tLocale::Clear(), client(), and server().
00211 { 00212 if (argnum<=1){ 00213 server(); 00214 } 00215 else{ 00216 client(arglist[1]); 00217 } 00218 00219 tLocale::Clear(); 00220 }
void sample_message_handler | ( | nMessage & | m | ) |
Definition at line 74 of file l2_demo.cpp.
References con, nMessage::End(), and Transmitted.
00074 { 00075 int i=1; 00076 while(!m.End()){ 00077 short out; 00078 m >> out; 00079 if ( out < 20 ) 00080 { 00081 con << i << " :\t" << out << '\n'; 00082 i++; 00083 } 00084 00085 Transmitted[out] = true; 00086 } 00087 }
void sample_message_handler2 | ( | nMessage & | m | ) |
Definition at line 94 of file l2_demo.cpp.
References nMessage::End(), and Transmitted.
00094 { 00095 while(!m.End()){ 00096 unsigned int out; 00097 m >> out; 00098 Transmitted[out] = true; 00099 } 00100 }
void server | ( | ) |
Definition at line 110 of file l2_demo.cpp.
References nSERVER, nSTANDALONE, sn_GetNetState(), sn_Receive(), sn_SendPlanned(), and sn_SetNetState().
Referenced by ANET_GetHostList(), gCommandLineJumpStartAnalyzer::Connect(), AAURLHandler::connectToServer:, FloodProtection(), nServerInfo::GetBigServerInfo(), nServerInfo::GetBigServerInfoCommon(), nServerInfo::GetBigServerInfoMaster(), nServerInfo::GetMasters(), nServerInfo::GiveBigServerInfoMaster(), nServerInfo::Load(), main(), and sg_TransferCustomServer().
00110 { 00111 int loop=100000; // a long delay loop 00112 00113 sn_SetNetState(nSERVER); // initialise server mode 00114 00115 while(loop>0 && sn_GetNetState()!=nSTANDALONE){ // and loop a while 00116 sn_Receive(); 00117 sn_SendPlanned(); 00118 loop--; 00119 usleep(10000); 00120 } 00121 00122 sn_SetNetState(nSTANDALONE); // exit. 00123 usleep(10000); 00124 sn_Receive(); 00125 sn_SendPlanned(); 00126 }
unsigned short client_gamestate[MAXCLIENTS+2] |
Definition at line 107 of file l2_demo.cpp.
nCallbackLoginLogout loginlogout(LoginLogout) [static] |
nDescriptor message_test(31,&sample_message_handler,"message_test") |
nDescriptor message_test2(32,&sample_message_handler2,"message_test2") |
Referenced by client().
bool Transmitted[MAX] |
Definition at line 41 of file l2_demo.cpp.
Referenced by LoginLogout(), sample_message_handler(), and sample_message_handler2().