src/network/l2_demo.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 *************************************************************************
00004 
00005 ArmageTron -- Just another Tron Lightcycle Game in 3D.
00006 Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)
00007 
00008 **************************************************************************
00009 
00010 This program is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU General Public License
00012 as published by the Free Software Foundation; either version 2
00013 of the License, or (at your option) any later version.
00014 
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00023   
00024 ***************************************************************************
00025 
00026 */
00027 
00028 #include "config.h"
00029 #include "tConsole.h"
00030 #include "tLocale.h"
00031 #include "defs.h"
00032 #include <string>
00033 #if HAVE_UNISTD_H
00034 #include <unistd.h>
00035 #endif
00036 #include <iostream>
00037 #include "nNetwork.h"
00038 
00039 #define MAX 70000
00040 
00041 bool Transmitted[MAX];
00042 
00043 void LoginLogout()
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 }
00070 
00071 static nCallbackLoginLogout loginlogout(LoginLogout);
00072 
00073 // a sample message receiver: print all the shorts stored in m.
00074 void sample_message_handler(nMessage &m){
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 }
00088 
00089 
00090 
00091 // the descriptor
00092 nDescriptor message_test(31,&sample_message_handler,"message_test");
00093 
00094 void sample_message_handler2(nMessage &m){
00095     while(!m.End()){
00096         unsigned int out;
00097         m >> out;
00098         Transmitted[out] = true;
00099     }
00100 }
00101 
00102 
00103 
00104 // the descriptor
00105 nDescriptor message_test2(32,&sample_message_handler2,"message_test2");
00106 
00107 unsigned short client_gamestate[MAXCLIENTS+2];
00108 
00109 
00110 void server(){
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 }
00127 
00128 
00129 void client(const tString &serv){
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 }
00208 
00209 
00210 
00211 int main(int argnum,char **arglist){
00212     if (argnum<=1){
00213         server();
00214     }
00215     else{
00216         client(arglist[1]);
00217     }
00218 
00219     tLocale::Clear();
00220 }

Generated on Sat Mar 15 22:55:49 2008 for Armagetron Advanced by  doxygen 1.5.4