From: Matze B. <mat...@us...> - 2002-06-18 20:28:04
|
Update of /cvsroot/planeshift/gnl/example/chat/client In directory usw-pr-cvs1:/tmp/cvs-serv14343/example/chat/client Modified Files: chatclient.cpp chatclient.h main.cpp Log Message: more work on chatclient, it should sendout the first packet now Index: chatclient.cpp =================================================================== RCS file: /cvsroot/planeshift/gnl/example/chat/client/chatclient.cpp,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** chatclient.cpp 17 Jun 2002 10:20:10 -0000 1.3 --- chatclient.cpp 18 Jun 2002 20:28:01 -0000 1.4 *************** *** 5,8 **** --- 5,9 ---- #include "chatclient.h" #include "util/gnltime.h" + #include "gnl/messagewriter.h" namespace chat { *************** *** 11,20 **** using gnl::Sleep; ! ChatClient::ChatClient () { } ChatClient::~ChatClient () { } --- 12,27 ---- using gnl::Sleep; ! ChatClient::ChatClient (const char* name) { + size_t nlen = strlen(name) + 1; + myname = new char [nlen]; + memcpy ((void*) myname, name, nlen); + + SetConnectionList (&connectionlist); } ChatClient::~ChatClient () { + delete[] myname; } *************** *** 35,41 **** } ! void ChatClient::Connect (const char* server) { } --- 42,130 ---- } ! void ChatClient::SendMessage (MessageWrapper* mw, bool reliable = true) ! { ! mw->SetClient (server); ! NetHandler::SendMessage (mw, reliable); ! } ! ! void ChatClient::Connect (const char* serveraddr) { + SocketAddr addr; + addr.Resolve (serveraddr); + + InitSocket (); + + Connection* newserver = new Connection(addr); + delete server; + server = newserver; + + MessageWriter msg; + + msg.Write ('C'); + msg.Write (myname); + + MessageWrapper* message = msg.GetMessage(); + SendMessage (message); + message->DecRef(); + } + + //-------------------------------------------------------------------------- + + ChatConnectionListIterator::ChatConnectionListIterator + (ChatConnectionList* parent) + : list (parent), start(true) + { + } + + ChatConnectionListIterator::~ChatConnectionListIterator () + { + } + void ChatConnectionListIterator::Reset () + { + start = true; + } + + Connection* ChatConnectionListIterator::Next () + { + if (start) + { + start = false; + return list->server; + } + + return NULL; + } + + ChatConnectionList::ChatConnectionList () + : server(NULL) + { + } + + ChatConnectionList::~ChatConnectionList () + { + } + + void ChatConnectionList::SetServer (Connection* newserver) + { + server = newserver; + } + + Connection* ChatConnectionList::Find (const SocketAddr& addr) + { + if (server->GetAddr() == addr) + return server; + + return NULL; + } + + Connection* ChatConnectionList::Create (const SocketAddr& addr) + { + return NULL; + } + + ConnectionListIterator* ChatConnectionList::GetIterator () + { + return new ChatConnectionListIterator(this); } Index: chatclient.h =================================================================== RCS file: /cvsroot/planeshift/gnl/example/chat/client/chatclient.h,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** chatclient.h 16 Jun 2002 16:20:37 -0000 1.2 --- chatclient.h 18 Jun 2002 20:28:01 -0000 1.3 *************** *** 8,19 **** using namespace gnl; class ChatConnectionList : public ConnectionList { public: ! ChatConnectionList (const SocketAddr& serveraddr); virtual ~ChatConnectionList (); virtual Connection* Find (const SocketAddr& addr); virtual Connection* Create (const SocketAddr& addr); }; --- 8,41 ---- using namespace gnl; + class ChatConnectionList; + + class ChatConnectionListIterator : public ConnectionListIterator + { + public: + ChatConnectionListIterator (ChatConnectionList* parent); + virtual ~ChatConnectionListIterator (); + + virtual void Reset(); + virtual Connection* Next(); + protected: + ChatConnectionList* list; + bool start; + }; + class ChatConnectionList : public ConnectionList { public: ! ChatConnectionList (); virtual ~ChatConnectionList (); + void SetServer (Connection* newserver); + virtual Connection* Find (const SocketAddr& addr); virtual Connection* Create (const SocketAddr& addr); + virtual ConnectionListIterator* GetIterator (); + + protected: + friend class ChatConnectionListIterator; + Connection* server; }; *************** *** 21,33 **** { public: ! ChatClient (); virtual ~ChatClient (); virtual bool HandleUnknownClient (Connection* connection, MessageWrapper* msg); void Run (); void Connect (const char* server); }; --- 43,61 ---- { public: ! ChatClient (const char* name); virtual ~ChatClient (); virtual bool HandleUnknownClient (Connection* connection, MessageWrapper* msg); + virtual void SendMessage (MessageWrapper* mw, bool reliable); void Run (); void Connect (const char* server); + + protected: + char* myname; + Connection* server; + ChatConnectionList connectionlist; }; Index: main.cpp =================================================================== RCS file: /cvsroot/planeshift/gnl/example/chat/client/main.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** main.cpp 16 Jun 2002 16:20:37 -0000 1.2 --- main.cpp 18 Jun 2002 20:28:01 -0000 1.3 *************** *** 11,17 **** int main(int argc, char* argv[]) { ! if (argc <= 1 || argc >= 3) { ! cerr << "chatclient Servername\n"; return 0; } --- 11,17 ---- int main(int argc, char* argv[]) { ! if (argc <= 2 || argc > 3) { ! cerr << "chatclient Username Servername\n"; return 0; } *************** *** 19,28 **** ChatClient* client; try { ! client = new ChatClient; ! ! cout << "Trying to connect to " << argv[1] << ".\n"; ! client->Connect (argv[1]); ! } catch (int) --- 19,26 ---- ChatClient* client; try { ! client = new ChatClient(argv[1]); + cout << "Trying to connect to " << argv[2] << ".\n"; + client->Connect (argv[2]); } catch (int) *************** *** 33,37 **** } ! client->Run (); return 0; --- 31,45 ---- } ! cout << "Main loop running...\n"; ! try { ! client->Run (); ! } ! catch (const char* msg) ! { ! cerr << "Aborting application due error:\n"; ! cerr << "\t" << msg << "\n"; ! delete client; ! return 1; ! } return 0; |