Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | Related Pages | Examples

2. Connection (complex: use handshaking)

This test will send along data during the handshaking/connection phase, to validate the client. The following things will be tested:

Client Application

The following code shows a very simple client, the client just initiates its connection to a given host and sends a username along. If it receives a discontinue packet it prints out a reason sent by the server. If it receives a continue-packet it checks a version string sent by the server with its own version string and either sends a continue-packet or a discontinue packet.
/// @cond EXCLUDEDTESTSOURCES
/***************************************************************************
 * The contents of this file are subject to the Mozilla Public             *
 * License Version 1.1 (the "License"); you may not use this file          *
 * except in compliance with the License. You may obtain a copy of         *
 * the License at http://www.mozilla.org/MPL/                              *
 *                                                                         *
 * Software distributed under the License is distributed on an "AS         *
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or              *
 * implied. See the License for the specific language governing            *
 * rights and limitations under the License.                               *
 *                                                                         *
 * The Original Code is Game Network Framework (GaNeF).                    *
 *                                                                         *
 * The Initial Developers of the Original Code are                         *
 * Lars Langer and Emanuel Greisen                                         *
 * Copyright (C) 2005. Lars Langer & Emanuel Greisen                       *
 * All Rights Reserved.                                                    *
 *                                                                         *
 * Contributor(s):                                                         *
 *   none yet....                                                          *
 *                                                                         *
 ***************************************************************************/

#include <iostream>
#include <cstdlib>

#include "../Ganef/client/clientframework.h"

/**
 * This is just a test-client-framework.
 * @author Lars Langer and Emanuel Greisen
 */
class TestClientFramework : public ClientFramework
{
   private:
      bool is_running;
      std::string server_hostname;
      std::string username;
      std::string client_version;

   public:
      TestClientFramework(
            const std::string & server_hostname,
            const std::string & username,
            const std::string & client_version
      )
         : ClientFramework(int(0)),
            server_hostname(server_hostname),
            username(username),
            client_version(client_version)
      {
         std::cout << "TestClientFramework.Constructor(server:" << server_hostname
               << ",user: " << username
               << ",version: " << client_version << ")\n";
      }
      ~TestClientFramework()
      {
      };

   protected: // These must be implemented in a derived class
      virtual void writeHandshakeInitMessage( ClientPacket * fp )
      {
         std::cout << "Writing our username: " << username << " to the initial Handshake\n";
         // Let the user know our username
         fp->writeString(username);
      }

      virtual void onHandshakingDiscontinue( ClientPacket * fp )
      {
         std::cout << "The server did not want to let us continue..\n";
         std::cout << "[Reason]" << fp->readString() << std::endl;
      }

      virtual bool onHandshakeDescription( ClientPacket * fp, ClientPacket * reply )
      {
         std::cout << "The server accepted our username, now lets look at it's version" << std::endl;
         std::string server_version = fp->readString();
         if(server_version == client_version)
         {
            return true;
         }

         std::cout << "Version mismatch: ";
         std::cout << "(server: "<<server_version<<", client:"<<client_version<<")";
         std::cout << ", abort connection" << std::endl;
         return false;
      }

      virtual void onDisconnect( )
      {
         std::cout << "Disconnected (With state: "<<getState()<<")\n";
      }

      virtual void onConnect( )
      {
         std::cout << "Connected\n";
      }

   public:
      void mainLoop( )
      {
         std::cout << "Connecting to: " << server_hostname <<":"<< 8000<< std::endl;
         initServerConnection(server_hostname, 8000);
         is_running = true;
         while(is_running)
         {
            //std::cout << "keepAlive()\n";
            // Then we get Packets from the framework
            keepAlive();

            // Then we sleep to let the CPU live
            psleep(50);
         }
      }
};




int main(int argc, char *argv[])
{
   std::string server_host("localhost");
   std::string username("testuser");
   std::string client_version("V.1");

   if(argc > 1)
      server_host = argv[1];
   if(argc > 2)
      username = argv[2];
   if(argc > 3)
      client_version = argv[3];
   TestClientFramework app(server_host, username, client_version);

   // Start the Application
   app.mainLoop();
}

/// @endcond

Server Application

The following is the server for this test, it is rather simple. It checks the username sent by the server, anf if it matches it sends a continue packet with its version string. If it does not match it sends a discontinue-packet.
/// @cond EXCLUDEDTESTSOURCES
/***************************************************************************
 * The contents of this file are subject to the Mozilla Public             *
 * License Version 1.1 (the "License"); you may not use this file          *
 * except in compliance with the License. You may obtain a copy of         *
 * the License at http://www.mozilla.org/MPL/                              *
 *                                                                         *
 * Software distributed under the License is distributed on an "AS         *
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or              *
 * implied. See the License for the specific language governing            *
 * rights and limitations under the License.                               *
 *                                                                         *
 * The Original Code is Game Network Framework (GaNeF).                    *
 *                                                                         *
 * The Initial Developers of the Original Code are                         *
 * Lars Langer and Emanuel Greisen                                         *
 * Copyright (C) 2005. Lars Langer & Emanuel Greisen                       *
 * All Rights Reserved.                                                    *
 *                                                                         *
 * Contributor(s):                                                         *
 *   none yet....                                                          *
 *                                                                         *
 ***************************************************************************/

#include <iostream>
#include <cstdlib>
#include <vector>


#include "../Ganef/server/serverframework.h"


/**
 * This is just a test server-framework;
 * @author Lars Langer and Emanuel Greisen
 */
class TestServerFramework : public ServerFramework
{
   public:
      TestServerFramework() : ServerFramework(8000)
      {
      }

      ~TestServerFramework()
      {
      }

   protected: // These method should be implemented on the GameServer
      /// This method should write it's reply in the reply-packet,
      /// and return true or false depending on wether it want's us to continue the handshaking.
      virtual bool onInitialHandshake(ServerPacket * handshake, ServerPacket * reply)
      {
         std::string client_username = handshake->readString();
         if(client_username == "superclient")
         {
            // Accept the "super user", tell them our version
            reply->writeString("Version.42");
            return true;
         }
         else
         {
            // Wrong username
            std::cout << "Invalid username: " << client_username << std::endl;
            reply->writeString("Invalid username, sorry");
            return false;
         }
      }
      /// This method will be invoked when a new client has passed the handshaking-phase.
      virtual void onClientConnected(Client * client)
      {
         // Write what ever should happen to a new client
         std::cout << "Client: " << client->getId() << " connected\n";
      }
      /// This method will be invoked when a client has timed out.
      virtual void onClientDisconnected(Client * client)
      {
         std::cout << "Client: " << client->getId() << " disconnected\n";
      }
      /// This method should return some dirivative of the Client-class.
      virtual Client * createClient(const ipaddress &addr, int port)
      {
         return new Client(this, addr, port);
      }

   public:
      void mainLoop()
      {
         std::cout << "TestServerFramework: Running\n";
         while(true)
         {
            // Get all incomming messages
            keepAlive();

            // Sleep a little
            psleep(100);
         }
      }
};




int main(int argc, char *argv[])
{
   try
   {
      // Create a framework
      TestServerFramework framework;

      // Start the framework
      framework.start();

      // GameLoop
      framework.mainLoop();
   }
   catch(FrameworkError *err)
   {
      std::cout << "FrameworkError:" << err->msg << std::endl;
   }
   catch(PacketError *err)
   {
      std::cout << "PacketError:" << err->msg << std::endl;
   }

   return EXIT_SUCCESS;
}

/// @endcond

Generated on Mon Feb 6 12:24:53 2006 for Ganef by  doxygen 1.4.4