netpanzer-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[netPanzer-CVS] netpanzer ./ChangeLog ./BUGS src/NetPanzer/Clas...


From: Matthias Braun
Subject: [netPanzer-CVS] netpanzer ./ChangeLog ./BUGS src/NetPanzer/Clas...
Date: Thu, 06 Nov 2003 12:53:27 -0500

CVSROOT:        /cvsroot/netpanzer
Module name:    netpanzer
Branch:         
Changes by:     Matthias Braun <address@hidden> 03/11/06 12:53:26

Modified files:
        .              : ChangeLog BUGS 
        src/NetPanzer/Classes: SpawnList.cpp 
        src/NetPanzer/Classes/AI: Astar.cpp Astar.hpp 
        src/NetPanzer/Interfaces: ConfigVariable.cpp ConfigVariable.hpp 
        src/NetPanzer/Views/MainMenu/Multi: GameServer.hpp 
                                            GameServerList.hpp 
                                            IRCChatMessage.hpp 
                                            IRCLobby.cpp IRCLobby.hpp 
                                            IRCLobbyView.cpp 
                                            IRCLobbyView.hpp 

Log message:
        cleanups and improvements for the IRC Lobby code

Patches:
Index: netpanzer/BUGS
diff -u netpanzer/BUGS:1.5 netpanzer/BUGS:1.6
--- netpanzer/BUGS:1.5  Tue Nov  4 17:48:51 2003
+++ netpanzer/BUGS      Thu Nov  6 12:53:25 2003
@@ -3,7 +3,8 @@
  errors. - fixed
 -after playing some time we get an assert(totoalByteCount >= 0) in
  Surface::~Surface... - fixed
--another crash reported by kov (happens when someone joins the game):
+-another crash reported by kov (happens when someone joins the game): -should 
be
+fixe ...
 <kov> Connection lost for ID 0: accept() failed
 <kov>  
 <kov> Program received signal SIGSEGV, Segmentation fault.
@@ -32,3 +33,5 @@
 - objective game cannot finish - fixed
 - Astar priority_queue "open" are not clear anytime
 - in LinkListDoubleTemplate.addFront sometime is (front==0 && rear != 0)
+- HOM effect outside the progressview window
+
Index: netpanzer/ChangeLog
diff -u netpanzer/ChangeLog:1.21 netpanzer/ChangeLog:1.22
--- netpanzer/ChangeLog:1.21    Wed Nov  5 04:33:50 2003
+++ netpanzer/ChangeLog Thu Nov  6 12:53:25 2003
@@ -1,3 +1,6 @@
+06-Nov-2003 by Matthias Braun
+-cleanups and imporvements to the IRC Lobby code
+
 05-Nov-2003 by Matthias Braun
 -added manpage contributed by Bartosz Fesnky <address@hidden>
 
Index: netpanzer/src/NetPanzer/Classes/AI/Astar.cpp
diff -u netpanzer/src/NetPanzer/Classes/AI/Astar.cpp:1.8 
netpanzer/src/NetPanzer/Classes/AI/Astar.cpp:1.9
--- netpanzer/src/NetPanzer/Classes/AI/Astar.cpp:1.8    Tue Sep 16 16:16:11 2003
+++ netpanzer/src/NetPanzer/Classes/AI/Astar.cpp        Thu Nov  6 12:53:25 2003
@@ -49,36 +49,34 @@
 }
 
 
-void Astar::initializeAstar( void )
+void Astar::initializeAstar()
 {
     initializeAstar( 4000, 50, 30 );
 }
 
 
-AstarNode *  Astar::getNewNode( void )
+AstarNode*  Astar::getNewNode()
 {
     AstarNode *node_ptr;
 
     if ( dynamic_node_management_flag == true ) {
         if (free_list_ptr == 0) {
-            return( 0 );
+            return 0;
         } else {
             node_ptr = free_list_ptr;
             free_list_ptr = free_list_ptr->parent;
-            return( node_ptr );
+            return node_ptr;
         }
-
     } else {
         if ( node_index >= node_list_size ) {
-            return( 0 );
+            return 0;
         }
 
         node_ptr = &node_list[ node_index ];
         node_index++;
 
-        return( node_ptr );
+        return node_ptr;
     }
-
 }
 
 void Astar::releaseNode( AstarNode *node )
@@ -123,7 +121,6 @@
 
     node_list = (AstarNode *) malloc( sizeof( AstarNode) * node_list_size );
     assert( node_list != 0 );
-
 }
 
 
@@ -328,10 +325,9 @@
                                     done = true;
                                     goal_reachable = false;
                                 } else {
-                                    memcpy( node, &temp_node, sizeof( 
AstarNode ) );
+                                    *node = temp_node;
                                     node->parent = best_node;
 
-
                                     open_set.setBit( node->map_loc.x, 
node->map_loc.y );
 
                                     if ( start_sampling_flag == true)
@@ -355,7 +351,6 @@
                 astar_set_array.setBit( best_node->map_loc.x, 
best_node->map_loc.y );
 
             releaseNode( best_node );
-
         } // ** else
 
         if ( succ_swap_flag )
Index: netpanzer/src/NetPanzer/Classes/AI/Astar.hpp
diff -u netpanzer/src/NetPanzer/Classes/AI/Astar.hpp:1.6 
netpanzer/src/NetPanzer/Classes/AI/Astar.hpp:1.7
--- netpanzer/src/NetPanzer/Classes/AI/Astar.hpp:1.6    Tue Sep 16 16:16:11 2003
+++ netpanzer/src/NetPanzer/Classes/AI/Astar.hpp        Thu Nov  6 12:53:26 2003
@@ -58,14 +58,6 @@
         PathRequest::request_type = request_type;
         PathRequest::status = _slot_status_free;
     }
-
-    // XXX this looks strange to me
-#if 0
-    void operator=( const PathRequest &rhs )
-    {
-        memmove( this, &rhs, sizeof( PathRequest ) );
-    }
-#endif
 };
 
 class AstarNode
@@ -119,9 +111,8 @@
     AstarNode goal_node;
     AstarNode *best_node;
 
-    std::priority_queue<AstarNode*,
-    std::vector<AstarNode*>,
-    AstarNodePtrCompare> open;
+    std::priority_queue<AstarNode*, std::vector<AstarNode*>,
+                        AstarNodePtrCompare> open;
 
     BitArray open_set;
     BitArray closed_set;
@@ -155,6 +146,7 @@
                                 AstarNode *succ );
 
     bool process_succ( PathList *path, int *result_code );
+    
 public:
     Astar();
 
Index: netpanzer/src/NetPanzer/Classes/SpawnList.cpp
diff -u netpanzer/src/NetPanzer/Classes/SpawnList.cpp:1.8 
netpanzer/src/NetPanzer/Classes/SpawnList.cpp:1.9
--- netpanzer/src/NetPanzer/Classes/SpawnList.cpp:1.8   Thu Oct 23 15:26:14 2003
+++ netpanzer/src/NetPanzer/Classes/SpawnList.cpp       Thu Nov  6 12:53:25 2003
@@ -86,7 +86,7 @@
             spawn_index = rand() % size;
             break;
         default:
-            assert("unknown respawn type" == 0);
+            throw Exception("unknown respawn type");
     }
 
     do {
Index: netpanzer/src/NetPanzer/Interfaces/ConfigVariable.cpp
diff -u netpanzer/src/NetPanzer/Interfaces/ConfigVariable.cpp:1.1 
netpanzer/src/NetPanzer/Interfaces/ConfigVariable.cpp:1.2
--- netpanzer/src/NetPanzer/Interfaces/ConfigVariable.cpp:1.1   Sat Oct  4 
10:46:28 2003
+++ netpanzer/src/NetPanzer/Interfaces/ConfigVariable.cpp       Thu Nov  6 
12:53:26 2003
@@ -70,3 +70,7 @@
     return value;
 }
 
+std::ostream& operator<< (std::ostream& o, const ConfigString& string)
+{
+    return o << ( (const std::string&) string);
+}
Index: netpanzer/src/NetPanzer/Interfaces/ConfigVariable.hpp
diff -u netpanzer/src/NetPanzer/Interfaces/ConfigVariable.hpp:1.2 
netpanzer/src/NetPanzer/Interfaces/ConfigVariable.hpp:1.3
--- netpanzer/src/NetPanzer/Interfaces/ConfigVariable.hpp:1.2   Wed Oct 22 
13:50:46 2003
+++ netpanzer/src/NetPanzer/Interfaces/ConfigVariable.hpp       Thu Nov  6 
12:53:26 2003
@@ -18,6 +18,7 @@
 #ifndef __CONFIGVARIABLE_HPP__
 #define __CONFIGVARIABLE_HPP__
 
+#include <iostream>
 #include <string>
 
 class ConfigVariable
@@ -91,6 +92,8 @@
 private:
     std::string value;
 };
+
+std::ostream& operator<< (std::ostream& o, const ConfigString& string);
 
 #endif
 
Index: netpanzer/src/NetPanzer/Views/MainMenu/Multi/GameServer.hpp
diff -u netpanzer/src/NetPanzer/Views/MainMenu/Multi/GameServer.hpp:1.1 
netpanzer/src/NetPanzer/Views/MainMenu/Multi/GameServer.hpp:1.2
--- netpanzer/src/NetPanzer/Views/MainMenu/Multi/GameServer.hpp:1.1     Mon Nov 
 3 08:26:35 2003
+++ netpanzer/src/NetPanzer/Views/MainMenu/Multi/GameServer.hpp Thu Nov  6 
12:53:26 2003
@@ -18,35 +18,35 @@
 #ifndef __GameServer_h__
 #define __GameServer_h__
 
-#include <SDL_net.h>
-
-#include "String.hpp"
+#include <ctime>
+#include <string>
 
 //---------------------------------------------------------------------------
 class GameServer
 {
 public:
-    String host;
-    String user;
-    String map;
-    time_t last_update;
-    int players;
-    int max_players;
-    GameServer *next,*prev;
-    GameServer() {
-        next=NULL;
-        prev=NULL;
+    GameServer(const std::string& newhost, int newport,
+            const std::string& newuser, const std::string& newmap,
+            int newplayercount, int newmaxplayers)
+        : host(newhost), port(newport), user(newuser), map(newmap),
+            playercount(newplayercount), max_players(newmaxplayers)
+    {
+        std::time(&last_update);
     }
-    void set(const char *h,const char *u,int p,int mp,const char *m) {
-        host=h;
-        user=u;
-        map=m;
-        time(&last_update);
-        players=p;
-        max_players=mp;
-    }
-};
-
 
+    GameServer::GameServer(const GameServer& other)
+        : host(other.host), port(other.port), user(other.user), map(other.map),
+          last_update(other.last_update), playercount(other.playercount),
+          max_players(other.max_players)
+    { }
+    
+    std::string host;
+    int port;
+    std::string user;
+    std::string map;
+    std::time_t last_update;
+    int playercount;
+    int max_players;
+};
 
 #endif
Index: netpanzer/src/NetPanzer/Views/MainMenu/Multi/GameServerList.hpp
diff -u netpanzer/src/NetPanzer/Views/MainMenu/Multi/GameServerList.hpp:1.1 
netpanzer/src/NetPanzer/Views/MainMenu/Multi/GameServerList.hpp:1.2
--- netpanzer/src/NetPanzer/Views/MainMenu/Multi/GameServerList.hpp:1.1 Mon Nov 
 3 08:26:35 2003
+++ netpanzer/src/NetPanzer/Views/MainMenu/Multi/GameServerList.hpp     Thu Nov 
 6 12:53:26 2003
@@ -18,60 +18,32 @@
 #ifndef __GameServerList_h__
 #define __GameServerList_h__
 
-#include <SDL.h>
-#include <SDL_thread.h>
-#include <SDL_net.h>
+#include <vector>
+#include <string>
 
 #include "View.hpp"
 #include "Surface.hpp"
-#include "BucketArrayTemplate.hpp"
 #include "GameServer.hpp"
 
 
 //---------------------------------------------------------------------------
-class GameServerList
+class GameServerList : public std::vector<GameServer>
 {
-    BucketArrayTemplate<GameServer> servers;
-    static int get_str_hash(const char *h) {
-        int n=0xaa;
-        while(*h) { n&=*h; h++; }
-        return n&0xf;
-    }
 public:
-    static const int max_buckets=16;
-    GameServerList() {
-        servers.initialize(max_buckets);
-    }
-    ~GameServerList() {
-    }
-
-    LinkListDoubleTemplate<GameServer> *getBucket(int h) {
-        assert(this!=NULL);
-        assert(h<max_buckets);
-        LinkListDoubleTemplate<GameServer> *i= servers.getBucket(h);
-        return i;
-    }
-    GameServer *find(const char *addr) {
-        assert(this!=NULL);
-        assert(addr!=NULL);
-
-        int h=get_str_hash(addr);
-        LinkListDoubleTemplate<GameServer> *bucket=servers.getBucket(h);
-        GameServer *upto=bucket->getFront();
-        while(upto!=NULL) {
-            if(strcmp((const char *)upto->host,addr)==0) {
-                return upto;
-            }
-            upto=upto->next;
+    GameServerList()
+    { }
+    ~GameServerList()
+    { }
+
+    GameServer *find(const std::string& host, int port)
+    {
+        std::vector<GameServer>::iterator i;
+        for(i=begin(); i!=end(); i++) {
+            if(i->host == host && i->port == port)
+                return &(*i);
         }
-        return NULL;
-    }
-    void add(GameServer *server) {
-        assert(this!=NULL);
-        assert(server!=NULL);
 
-        int h=get_str_hash(server->host);
-        servers.addObject(h,server);
+        return 0;
     }
 };
 
Index: netpanzer/src/NetPanzer/Views/MainMenu/Multi/IRCChatMessage.hpp
diff -u netpanzer/src/NetPanzer/Views/MainMenu/Multi/IRCChatMessage.hpp:1.1 
netpanzer/src/NetPanzer/Views/MainMenu/Multi/IRCChatMessage.hpp:1.2
--- netpanzer/src/NetPanzer/Views/MainMenu/Multi/IRCChatMessage.hpp:1.1 Mon Nov 
 3 08:26:36 2003
+++ netpanzer/src/NetPanzer/Views/MainMenu/Multi/IRCChatMessage.hpp     Thu Nov 
 6 12:53:26 2003
@@ -18,25 +18,31 @@
 #ifndef __IRCChatMessage_h__
 #define __IRCChatMessage_h__
 
-#include <SDL.h>
-#include <SDL_thread.h>
-#include <SDL_net.h>
-
-#include "View.hpp"
-#include "Surface.hpp"
-#include "String.hpp" 
+#include <string>
 
 //---------------------------------------------------------------------------
 class IRCChatMessage
 {
 public:
-    String mess;
-    String user;
-    IRCChatMessage *prev,*next;
-    IRCChatMessage(const char *m,const char *u) : mess(m),user(u) {
-        prev=NULL;
-        next=NULL;
+    IRCChatMessage(const std::string& newuser, const std::string& newmessage) 
+        : user(newuser), message(newmessage)
+    {
+    }
+
+    IRCChatMessage(const IRCChatMessage& other)
+        : user(other.user), message(other.message)
+    {
     }
+
+    const std::string getUser() const
+    { return user; }
+
+    const std::string getMessage() const
+    { return message; }
+
+private:
+    std::string user;
+    std::string message;
 };
 
 #endif
Index: netpanzer/src/NetPanzer/Views/MainMenu/Multi/IRCLobby.cpp
diff -u netpanzer/src/NetPanzer/Views/MainMenu/Multi/IRCLobby.cpp:1.1 
netpanzer/src/NetPanzer/Views/MainMenu/Multi/IRCLobby.cpp:1.2
--- netpanzer/src/NetPanzer/Views/MainMenu/Multi/IRCLobby.cpp:1.1       Mon Nov 
 3 08:26:36 2003
+++ netpanzer/src/NetPanzer/Views/MainMenu/Multi/IRCLobby.cpp   Thu Nov  6 
12:53:26 2003
@@ -15,109 +15,119 @@
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
+#include <config.h>
+
+#include <sstream>
 
 #include "Log.hpp"
 #include "IRCLobby.hpp"
 #include "PlayerInterface.hpp"
 #include "GameConfig.hpp"
 #include "NetworkGlobals.hpp" 
+#include "Exception.hpp"
+
+static const char* ask_server_running_mess = "Who's running a server?";
+static const char* server_running_mess = "I'm running";
 
-IRCLobby::IRCLobby(IPaddress *addr)
+IRCLobby::IRCLobby(const std::string& servername, int serverport,
+        const std::string& nickname,
+        const std::string& newchannelname)
+    : irc_server_socket(0), channel_name(newchannelname), game_servers(0),
+      running_thread(0)
 {
-    irc_server_sock=0;
-    channel_name="#netpanzerlob";
     game_servers=new GameServerList();
-    memcpy(&irc_server,addr,sizeof(irc_server));
-    running_thread=NULL;
-    quit_thread=0;
     game_servers_mutex=SDL_CreateMutex();
+    connectToServer(servername, serverport, nickname, channel_name);
+    startMessagesThread();
 }
 
 IRCLobby::~IRCLobby()
 {
-    SDL_mutexP(game_servers_mutex);
-    delete game_servers;
-    game_servers=NULL;
-    SDL_mutexV(game_servers_mutex);
-    SDL_DestroyMutex(game_servers_mutex);
+    std::cout << "Stop Thread: " << std::endl;
     stopThread();
+    
+    std::cout << "More... " << std::endl;
+    SDL_DestroyMutex(game_servers_mutex);
+    delete game_servers;
+    game_servers=0;
+    std::cout << "ok" << std::endl;
 }
 
-void IRCLobby::stopThread() {
-    int r;
-    quit_thread=1;
-    if(running_thread==NULL) { return; }
-    SDL_WaitThread(running_thread,&r);
-    running_thread=NULL;
-    if(irc_server_sock) {
-        char *quit="QUIT\n";
-        SDLNet_TCP_Send(irc_server_sock,quit,5);
-        SDLNet_TCP_Close(irc_server_sock);
-        irc_server_sock=0;
+void IRCLobby::stopThread()
+{
+    if(!running_thread)
+        return;
+    
+    SDL_KillThread(running_thread);
+    running_thread = 0;
+    
+    if(irc_server_socket) {
+        char* quit="QUIT\n";
+        SDLNet_TCP_Send(irc_server_socket,quit,5);
+        SDLNet_TCP_Close(irc_server_socket);
+        irc_server_socket=0;
     }
 }
 
-const char IRCLobby::server_running_mess[]="I'm running";
-const char IRCLobby::ask_server_running_mess[]="Who's running a server?";
-
 // send server info to someone
-int IRCLobby::sendServerInfo(const char *dest)
+void IRCLobby::sendServerInfo(const std::string& dest)
 {
-    char line[1024];
-    assert(this!=NULL);
-    char *l=line;
-
-    const char *map= ((const std::string&)(gameconfig->map)).c_str();
-    int running_players=PlayerInterface::countPlayers();
-    l+=snprintf(l,(line+sizeof(line))-l,"-%s %i/%i map:%s",
-        server_running_mess,
-        running_players,PlayerInterface::getMaxPlayers(),
-        map);
-
-#if 0
-    int max_players=PlayerInterface::getMaxPlayers();
-    for(int p=0; p<max_players; p++) {
-            PlayerState *st=getPlayerState(p);
-            l+=snprintf(l,(line+sizeof(line))-l,"%s:",
-                    st->getName());
-    }
-    *l++=',';
-#endif
-    return sendIRCMessageLine(line,dest);
-}
+    std::stringstream buffer;
 
-
-int IRCLobby::connectToServer()
-{
-    char line[1024];
-    char irc_playername[1024];
-    char *pl;
-    const char *pl_from;
-
-    assert(this!=NULL);
-    if(!(irc_server_sock=SDLNet_TCP_Open(&irc_server))) {
-        return -1;
-    }
+    buffer << "-" << server_running_mess
+           << PlayerInterface::countPlayers()
+           << "/" << PlayerInterface::getMaxPlayers()
+           << " map:" << gameconfig->map;
+
+    return sendIRCMessageLine(buffer.str(), dest);
+}
+
+void IRCLobby::connectToServer(const std::string& serveraddress, int port,
+        const std::string& nickname, const std::string& channel)
+{
+    IPaddress addr;
+    // some old versions of SDL_net take a char* instead of const char*
+    if(SDLNet_ResolveHost(&addr, const_cast<char*>(serveraddress.c_str()), 
6667)
+            < 0)
+        throw Exception("Couldn't resolve server address for '%s'",
+                serveraddress.c_str());
+        
+    irc_server_socket = SDLNet_TCP_Open(&addr);
+    if(!irc_server_socket)
+        throw Exception("Couldn't connect to irc server: %s",
+                SDLNet_GetError());
 
     // login
-    const char *playername=((const 
std::string&)(gameconfig->playername)).c_str();
+    const char *playername = nickname.c_str();
 
-    pl=irc_playername;
-    pl_from=playername;
-    while(*pl_from) {
-        if(!isalnum(*pl_from) && *pl_from!='_') { pl_from++; continue; }
-        *pl++=*pl_from++;
+    // only some names are allowed in irc names
+    char ircname[1024];
+    int i;
+    for(i=0; i<1023; i++) {
+        char c = playername[i];
+        if(c==0)
+            break;
+        // don't use isalpha here since it only behaves correctly (for irc
+        // nicknames) in the C locale setting
+        if( (c>='a' && c<='z') || (c>='A' && c<='Z') || (c>='0' && c<='9')
+          || c == ':') {
+            ircname[i] = c;
+        }
     }
-    *pl=0;
+    ircname[i] = 0;
+   
+    std::stringstream buffer;
+    buffer << "NICK " << ircname;
+    sendIRCLine(buffer.str());
+
+    buffer.str("");
+    buffer << "USER " << ircname << " 0 * :" << ircname;
+    sendIRCLine(buffer.str());
 
-    snprintf(line,sizeof(line),"NICK %s",irc_playername);
-    sendIRCLine(line);
-    snprintf(line,sizeof(line),"USER %s 0 * :%s",
-            irc_playername,irc_playername);
-    sendIRCLine(line);
     // join channel
-    snprintf(line,sizeof(line),"JOIN %s",channel_name);
-    sendIRCLine(line);
+    buffer.str("");
+    buffer << "JOIN " << channel_name;
+    sendIRCLine(buffer.str());
 
     if(gameconfig->hostorjoin== _game_session_host) {
         // tell everyone the server just started
@@ -126,74 +136,76 @@
     else if(gameconfig->hostorjoin== _game_session_join) {
         refreshServerList();
     }
-    return 0;
 }
 
-int IRCLobby::refreshServerList()
+void IRCLobby::refreshServerList()
 {
-    char line[256];
+    std::stringstream buffer;
 
-    assert(this!=NULL);
-    // ask for known servers
-    snprintf(line,sizeof(line),"-%s",ask_server_running_mess);
-    sendIRCMessageLine(line,channel_name);
-    return 0;
+    buffer << "-" << ask_server_running_mess;
+    
+    sendIRCMessageLine(buffer.str());
 }
 
-static int messagesThreadEntry(IRCLobby *t)
+int IRCLobby::messagesThreadEntry(void* data)
 {
+    IRCLobby* t = (IRCLobby*) data;
     t->processMessages();
-    LOG(("irc chat ended"));
     return 0;
 }
 
-int IRCLobby::startMessagesThread()
+void IRCLobby::startMessagesThread()
 {
-    assert(this!=NULL);
-    assert(running_thread==NULL);
+    assert(running_thread==0);
 
-    quit_thread=0;
-    if(running_thread!=NULL) { return -1; }
-    running_thread=SDL_CreateThread( (int (*)(void *)) 
messagesThreadEntry,this);
-    return 0;
-}
-
-int IRCLobby::processMessages()
-{
-    assert(this!=NULL);
-    if(connectToServer()<0) { return -1; }
-    while(!quit_thread) {
-        if(processMessage()<0) { return -1; }
+    running_thread 
+        = SDL_CreateThread( (int (*)(void *)) messagesThreadEntry,this);
+    if(!running_thread)
+        throw Exception("Couldn't start IRC thread.");
+}
+
+void IRCLobby::processMessages()
+{
+    while(1) {
+        try {
+            processMessage();
+        } catch(std::exception& e) {
+            LOG(("Exception in IRC Thread: %s", e.what()));
+            break;
+        } catch(...) {
+            break;
+        }
     }
-    return 0;
 }
 
-void IRCLobby::addChatMessage(const char *u,const char *m)
+void IRCLobby::addChatMessage(const std::string& user,
+                              const std::string& message)
 {
-    IRCChatMessage *chat_message=new IRCChatMessage(m,u);
-    int dels=chat_messages.getSize();
-    while(dels-->4) {
-        chat_messages.deleteFront();
-    }
-    chat_messages.addRear(chat_message);
+    // only save latest 20 messages
+    while(chat_messages.size() > 20)
+        chat_messages.pop_front();
+
+    chat_messages.push_back(IRCChatMessage(user, message));
 }
 
 // read a line of irc and process it.
-int IRCLobby::processMessage()
+void IRCLobby::processMessage()
 {
+    assert(irc_server_socket != 0);
+    
     char buf[1024];
-    char *host,*mess,*host_end,*user_end,*code;
+    char *host, *mess, *host_end, *user_end, *code;
 
-    assert(this!=NULL);
-    if(!irc_server_sock)  { return 0; }
+    readIRCLine(buf, sizeof(buf));
+    
+    if(buf[0]!=':')
+        return;
 
-    if(readIRCLine(buf,sizeof(buf))<0) {
-        return 0;
-    }
-    if(buf[0]!=':') { return 0; }
     code=buf+1;
-    while(*code && !isspace(*code)) {  code++; }
-    while(isspace(*code)) {  code++; }
+    // skip 1 word and spaces behind it
+    while(*code && !isspace(*code)) { code++; }
+    while(*code && isspace(*code)) { code++; }
+
     int code_i=atoi(code);
     if(code_i>=400 && code_i<500) {
         LOG(("IRC:%s",buf));
@@ -202,169 +214,153 @@
     // get remote user/host
     // address@hidden PRIVMSG #netpanzerlob :asfd
     if(
-        (host=strchr(buf,'@'))==NULL
-        || (mess=strchr(buf+1,':'))==NULL
-        || (user_end=strchr(buf,'!'))==NULL
+        (host=strchr(buf,'@'))==0
+        || (mess=strchr(buf+1,':'))==0
+        || (user_end=strchr(buf,'!'))==0
     ) {
-        return 0;
+        return;
     }
     *host++=0;
     *user_end++=0;
     mess++;
-    if((host_end=strchr(host,' '))==NULL) {
-        return 0;
+    if((host_end=strchr(host,' '))==0) {
+        return;
     }
     *host_end++=0;
     while(isspace(*host_end)) host_end++;
     if(strncmp(host_end,"PRIVMSG",7)!=0) {
-        return 0;
+        return;
     }
 
     if(mess[0]=='#') {
         // this is a chat message
-        addChatMessage(mess+1,buf+1);
+        addChatMessage(buf+1, mess+1);
         if(gameconfig->hostorjoin== _game_session_host) {
             LOG(("IRC message:%s:%s",buf+1,mess+1));
         }
 
-        return 0;
+        return;
     }
-    if(mess[0]!='-') {
-        // this is not an internal message
-        return 0;
-    }
-
-    if(strcmp(mess+1,ask_server_running_mess)==0) {
-        if(gameconfig->hostorjoin== _game_session_host) {
-            // reply with server details
-            sendServerInfo(buf+1);
-        }
-    }
-    else 
if(strncmp(mess+1,server_running_mess,sizeof(server_running_mess)-1)==0) {
-        // add a server to the list
-        if(gameconfig->hostorjoin== _game_session_join) {
-            const char *p=mess+1+sizeof(server_running_mess);
-            const char *map;
-            int players=atoi(p);
-            if((p=strchr(p,'/'))==NULL) {
-                LOG(("bad server description: %s\n",mess));
-                return 0;
-            }
-            int max_players=atoi(++p);
-            if((map=strstr(p,"map:"))==NULL) {
-                LOG(("no map name: %s\n",mess));
-                return 0;
-            }
-            map+=4;
+    if(mess[0]=='-') {
+        // this is an internal message
 
-//            IPaddress addr;
-//            
if(SDLNet_ResolveHost(&addr,host,_NETPANZER_DEFAULT_PORT_TCP)==0) {
-
-            GameServer *server=game_servers->find(host);
-            if(server==NULL) {
-                server=new GameServer(); 
-                server->set(host,buf+1,players,max_players,map);
-                SDL_mutexP(game_servers_mutex);
-                game_servers->add(server);
-                SDL_mutexV(game_servers_mutex);
+        if(strcmp(mess+1, ask_server_running_mess)==0) {
+            if(gameconfig->hostorjoin== _game_session_host) {
+                // reply with server details
+                sendServerInfo(buf+1);
             }
-            else {
-                server->set(host,buf+1,players,max_players,map);
+        }
+        else 
if(strncmp(mess+1,server_running_mess,sizeof(server_running_mess)-1)==0) {
+            // add a server to the list
+            if(gameconfig->hostorjoin== _game_session_join) {
+                const char *p=mess+1+sizeof(server_running_mess);
+                const char *map;
+                int players=atoi(p);
+                if((p=strchr(p,'/'))==0) {
+                    LOG(("bad server description: %s\n",mess));
+                    return;
+                }
+                int max_players=atoi(++p);
+                if((map=strstr(p,"map:"))==0) {
+                    LOG(("no map name: %s\n",mess));
+                    return;
+                }
+                map+=4;
+
+                GameServer *server
+                    = game_servers->find(host, _NETPANZER_DEFAULT_PORT_TCP);
+                if(server==0) {
+                    SDL_mutexP(game_servers_mutex);
+                    game_servers->push_back(
+                            GameServer(host, _NETPANZER_DEFAULT_PORT_TCP,
+                                buf+1, map, players, max_players));
+                    SDL_mutexV(game_servers_mutex);
+                }
+                else {
+                    server->user = buf+1;
+                    server->map = map;
+                    server->playercount = players;
+                    server->max_players = max_players;
+                }
             }
-
-//            }
-//            else { LOG(("cannot lookup: %s\n",host)); }
         }
     }
-    else {
-        LOG(("unknown chat cmd: %s\n",mess));
-    }
-    return 0;
 }
 
-int IRCLobby::sendChatMessage(const char *line)
+void IRCLobby::sendChatMessage(const std::string& user,
+                               const std::string& line)
 {
-    assert(this!=NULL);
-    assert(line!=NULL);
-
-    char chat_line[1024];
-    snprintf(chat_line,sizeof(chat_line),"#%s",line);
-    addChatMessage("",line);
-    return sendIRCMessageLine(chat_line);
+    std::string chatline = "#";
+    chatline += line;
+    addChatMessage(user, line);
+    return sendIRCMessageLine(chatline);
 }
 
-
-int IRCLobby::sendIRCMessageLine(char *line)
+void IRCLobby::sendIRCMessageLine(const std::string& line)
 {
-    assert(this!=NULL);
-    assert(line!=NULL);
-    return sendIRCMessageLine(line,channel_name);
+    return sendIRCMessageLine(line, channel_name);
 }
 
-int IRCLobby::sendIRCMessageLine(char *line,const char *to)
+void IRCLobby::sendIRCMessageLine(const std::string& line, const std::string& 
to)
 {
-    assert(this!=NULL);
-    assert(line!=NULL);
-    assert(to!=NULL);
-    char channel_msg[256];
-    int channel_msg_len;
+    std::stringstream channelmsg;
+    channelmsg << "PRIVMSG " << to << " :";
 
-    channel_msg_len=snprintf(channel_msg,sizeof(channel_msg),"PRIVMSG %s 
:",to);
-
-    
if(SDLNet_TCP_Send(irc_server_sock,channel_msg,channel_msg_len)!=channel_msg_len)
 {
-        return -1;
+    if(SDLNet_TCP_Send(irc_server_socket,
+                const_cast<char*> (channelmsg.str().c_str()),
+                channelmsg.str().size()) != (int) channelmsg.str().size()) {
+        throw Exception("Error when sending channelmessage: %s",
+                SDLNet_GetError());
     }
-    return sendIRCLine(line);
-}
 
+    sendIRCLine(line);
+}
 
-int IRCLobby::sendIRCLine(char *line)
+void IRCLobby::sendIRCLine(const std::string& line)
 {
-    assert(this!=NULL);
-    int len=strlen(line);
-    if(SDLNet_TCP_Send(irc_server_sock,line,len)!=len) {
-        return -1;
-    }
+    if(SDLNet_TCP_Send(irc_server_socket,
+                const_cast<char*> (line.c_str()),
+                line.size()) != (int) line.size())
+        throw Exception("Error when sending irc message '%s': %s",
+                line.c_str(), SDLNet_GetError());
+    
     static char lf[]="\n";
-    SDLNet_TCP_Send(irc_server_sock,lf,1);
-    return 0;
+    if(SDLNet_TCP_Send(irc_server_socket,lf,1) != 1)
+        throw Exception("Error when senging irc lf: %s",
+                SDLNet_GetError());
 }
 
-int IRCLobby::readIRCLine(char *buf,int buf_len)
+void IRCLobby::readIRCLine(char *buf, size_t buf_len)
 {
-    assert(this!=NULL);
     char *buf_end=buf+buf_len-1;
     char ch;
     char *buf_upto=buf;
+    
     SDLNet_SocketSet sock_set=SDLNet_AllocSocketSet(1);
-    SDLNet_TCP_AddSocket(sock_set,irc_server_sock);
+    SDLNet_TCP_AddSocket(sock_set,irc_server_socket);
 
-    while( buf_upto<buf_end) {
-        if(quit_thread) {
-err_ex:;
-            SDLNet_FreeSocketSet(sock_set);
-            return -1;
-        }
-        if(SDLNet_CheckSockets(sock_set,1000)<=0) {
-            const char *err=SDLNet_GetError();
-            if(err[0]!=0) {
-                LOG(("irc socket err:%s\n",err));
-                goto err_ex;
-            }
-            continue;
-        }
-        if(SDLNet_TCP_Recv(irc_server_sock,&ch,1)<=0) {
-            goto err_ex;
-        }
-        if(ch=='\r') { continue; }
-        if(ch=='\n') {
+    try {
+        while(buf_upto < buf_end) {
+            SDLNet_CheckSockets(sock_set, 1000);
+            if(!SDLNet_SocketReady(irc_server_socket))
+                continue;
+
+            if(SDLNet_TCP_Recv(irc_server_socket,&ch,1)<0)
+                throw Exception("Couldn't read TCP: %s",
+                        SDLNet_GetError());
+            
+            if(ch=='\r') { continue; }
+            if(ch=='\n') {
                 break;
+            }
+            *buf_upto++=ch;
         }
-        *buf_upto++=ch;
+    } catch(std::exception& e) {
+        SDLNet_FreeSocketSet(sock_set);
+        throw;
     }
+
     SDLNet_FreeSocketSet(sock_set);
     *buf_upto=0;
-    return buf_upto-buf;
 }
-
 
Index: netpanzer/src/NetPanzer/Views/MainMenu/Multi/IRCLobby.hpp
diff -u netpanzer/src/NetPanzer/Views/MainMenu/Multi/IRCLobby.hpp:1.1 
netpanzer/src/NetPanzer/Views/MainMenu/Multi/IRCLobby.hpp:1.2
--- netpanzer/src/NetPanzer/Views/MainMenu/Multi/IRCLobby.hpp:1.1       Mon Nov 
 3 08:26:36 2003
+++ netpanzer/src/NetPanzer/Views/MainMenu/Multi/IRCLobby.hpp   Thu Nov  6 
12:53:26 2003
@@ -18,6 +18,8 @@
 #ifndef __IRCLobby_h__
 #define __IRCLobby_h__
 
+#include <list>
+
 #include <SDL.h>
 #include <SDL_thread.h>
 #include <SDL_net.h>
@@ -33,40 +35,43 @@
 //---------------------------------------------------------------------------
 class IRCLobby
 {
-    friend class IRCLobbyView;
+public:
+    IRCLobby(const std::string& servername, int serverport,
+             const std::string& nickname,
+             const std::string& channel);
+    ~IRCLobby();
 
-    static const char server_running_mess[];
-    static const char ask_server_running_mess[];
-    IPaddress irc_server;
-    TCPsocket irc_server_sock;
-    char *channel_name;
-    SDL_Thread *running_thread;
-    int quit_thread;
-    LinkListDoubleTemplate<IRCChatMessage> chat_messages;
-    GameServerList *game_servers;
+    void sendChatMessage(const std::string& user, const std::string& line);
+    void sendIRCMessageLine(const std::string& line);
+    void refreshServerList();
 
-    int sendServerInfo(const char *dest);
-    // read any messages that need to be processed
-    int processMessage();
-    int sendIRCLine(char *line);
-    int sendIRCMessageLine(char *line,const char *to);
-    int readIRCLine(char *buf,int buf_len);
-    int connectToServer();
-    void addChatMessage(const char *m,const char *u);
+private:
+    void startMessagesThread();
+    void stopThread();
 
+    void processMessages();
+    static int messagesThreadEntry(void* t);
 
-public:
-    SDL_mutex *game_servers_mutex;
+    void sendServerInfo(const std::string& dest);
+    // read any messages that need to be processed
+    void processMessage();
+    void sendIRCLine(const std::string& line);
+    void sendIRCMessageLine(const std::string& line, const std::string& to);
+    void readIRCLine(char *buf, size_t buf_len);
+    void connectToServer(const std::string& serveraddr, int port, 
+            const std::string& nickname, const std::string& channel_name);
+    void addChatMessage(const std::string& user, const std::string& message);
 
-    IRCLobby(IPaddress *addr);
-    ~IRCLobby();
+    SDL_mutex *game_servers_mutex;   
 
-    int sendChatMessage(const char *line);
-    int sendIRCMessageLine(char *line);
-    void stopThread();
-    int refreshServerList();
-    int startMessagesThread();
-    int processMessages();
+    friend class IRCLobbyView;
+    TCPsocket irc_server_socket;
+
+    std::string channel_name;
+    std::list<IRCChatMessage> chat_messages;
+    GameServerList* game_servers;
+    
+    SDL_Thread *running_thread;
 };
 
 #endif
Index: netpanzer/src/NetPanzer/Views/MainMenu/Multi/IRCLobbyView.cpp
diff -u netpanzer/src/NetPanzer/Views/MainMenu/Multi/IRCLobbyView.cpp:1.1 
netpanzer/src/NetPanzer/Views/MainMenu/Multi/IRCLobbyView.cpp:1.2
--- netpanzer/src/NetPanzer/Views/MainMenu/Multi/IRCLobbyView.cpp:1.1   Mon Nov 
 3 08:26:36 2003
+++ netpanzer/src/NetPanzer/Views/MainMenu/Multi/IRCLobbyView.cpp       Thu Nov 
 6 12:53:26 2003
@@ -17,7 +17,8 @@
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
 #include <config.h>
-#include <SDL_net.h>
+
+#include <sstream>
 
 #include "Log.hpp"
 #include "IRCLobbyView.hpp"
@@ -28,9 +29,7 @@
 
 #include "Client.hpp"
 
-
-
-IRCLobby *IRCLobbyView::lobby_connection=NULL;
+IRCLobby *IRCLobbyView::lobby_connection=0;
 
 cInputFieldString IRCLobbyView::szChat;
 
@@ -43,6 +42,7 @@
 static void buttonChat(void)
 {
     IRCLobbyView::lobby_connection->sendChatMessage(
+        gameconfig->playername,
         IRCLobbyView::szChat.getString());
     IRCLobbyView::szChat.setString("");
 }
@@ -53,7 +53,7 @@
 IRCLobbyView::IRCLobbyView() : View()
 {
     lobby_view_height=160;
-    mouse_down_server=NULL;
+    mouse_down_server=0;
     total_displayed_servers=0;
     setSearchName("IRCLobbyView");
     setTitle("Lobby");
@@ -80,66 +80,68 @@
 //---------------------------------------------------------------------------
 void IRCLobbyView::doDraw(Surface &viewArea, Surface &clientArea)
 {
-    assert(this!=NULL);
+    assert(this!=0);
     clientArea.fill(Color::black);
+    
     int y=0;
-    int bucket_upto=0;
     int disp_server_upto=0;
+    int server_list_end_y=lobby_view_height-(Surface::getFontHeight()*6);
+    int chat_list_end_y=server_list_end_y+(Surface::getFontHeight()*4);      
 
-    if(lobby_connection==NULL) { return; }
-
-    LinkListDoubleTemplate<GameServer> *bucket=NULL;
-    GameServer *server=NULL;
+    if(lobby_connection==0) { return; }
 
 //~~~ todo: scrollbar for large list of servers
     SDL_mutexP(lobby_connection->game_servers_mutex);
-    int server_list_end_y=lobby_view_height-(Surface::getFontHeight()*6);
-    int chat_list_end_y=server_list_end_y+(Surface::getFontHeight()*4);
-    for(; y<server_list_end_y; y+=Surface::getFontHeight()) {
-        while(server==NULL && bucket_upto<GameServerList::max_buckets) {
-            bucket=lobby_connection->game_servers->getBucket(bucket_upto++);
-            server=bucket->getFront();
-        }
-        if(server==NULL) { break; }
-
-        char players_str[256];
-        sprintf(players_str,"%i/%i",server->players,server->max_players);
-        clientArea.bltString(iXY(0,y),server->user, Color::white);
-        clientArea.bltString(iXY(140,y),players_str, Color::white);
-        clientArea.bltString(iXY(200,y),server->map, Color::white);
+    
+    GameServerList::iterator i;
+    GameServerList* serverlist = lobby_connection->game_servers;
+    disp_server_upto=0;
+    for(i=serverlist->begin(); i!=serverlist->end(); i++) {
+        const GameServer* server = &(*i);
+
+        std::stringstream playerstr;
+        playerstr << server->playercount << "/" << server->max_players;
+        
+        clientArea.bltString(iXY(0,y),server->user.c_str(), Color::white);
+        clientArea.bltString(iXY(140,y),playerstr.str().c_str(), Color::white);
+        clientArea.bltString(iXY(200,y),server->map.c_str(), Color::white);
+        
         displayed_servers[disp_server_upto++]=server;
-        server=server->next;
+        y += Surface::getFontHeight();
+        if(y >= server_list_end_y)
+            break;
     }
     SDL_mutexV(lobby_connection->game_servers_mutex);
     total_displayed_servers=disp_server_upto;
 
-    IRCChatMessage *cm=lobby_connection->chat_messages.getFront();
-    y=server_list_end_y;
-    for(; cm!=NULL && y<chat_list_end_y; y+=Surface::getFontHeight()) {
-        char mess_str[256];
-        snprintf(mess_str,sizeof(mess_str),"%s:%s",
-            (const char *)cm->user,(const char *)cm->mess);
-        clientArea.bltString(iXY(4,y),mess_str, Color::white);
-        cm=cm->next;
+    std::list<IRCChatMessage>::reverse_iterator m;
+    y = chat_list_end_y - Surface::getFontHeight();
+    for(m = lobby_connection->chat_messages.rbegin();
+        m != lobby_connection->chat_messages.rend(); m++) {
+
+        std::stringstream temp;
+        temp << m->getUser() << ": " << m->getMessage();
+        clientArea.bltString(iXY(4,y), temp.str().c_str(), Color::white);
+        
+        y-=Surface::getFontHeight();
+        if(y < server_list_end_y)
+            break;
     }
     
     View::doDraw(viewArea, clientArea);
 }
 
-
-
-
 int IRCLobbyView::lMouseUp(const iXY &down_pos,const iXY &up_pos)
 {
-    assert(this!=NULL);
+    assert(this!=0);
     int idx_down=down_pos.y/Surface::getFontHeight();
     int idx=up_pos.y/Surface::getFontHeight();
     if(idx>=0 && idx_down==idx && idx<total_displayed_servers) {
-        GameServer *server=displayed_servers[idx];
-        assert(server!=NULL);
+        const GameServer *server=displayed_servers[idx];
+        assert(server!=0);
 
         // connect to this game
-        IPAddressView::szServer.setString(server->host);
+        IPAddressView::szServer.setString(server->host.c_str());
     }
     return View::lMouseUp(down_pos,up_pos);
 }
@@ -150,22 +152,18 @@
 //---------------------------------------------------------------------------
 void IRCLobbyView::startIRC()
 {
-    IPaddress addr;
-    stopIRC();
-    if(SDLNet_ResolveHost(&addr,"irc.freenode.net",6667)==0) {
-        lobby_connection=new IRCLobby(&addr);
-        lobby_connection->startMessagesThread();
+    try {
+        stopIRC();
+        lobby_connection = new IRCLobby("irc.freenode.net", 6667,
+                gameconfig->playername, "#netpanzerlob");
+    } catch(std::exception& e) {
+        LOG(("Couldn't connect to irc lobby: %s", e.what()));
     }
-    else { LOG(("cannot find irc host\n")); }
 }
 
 void IRCLobbyView::stopIRC()
 {
-    if(lobby_connection!=NULL) {
-        lobby_connection->stopThread();
-        delete lobby_connection;
-        lobby_connection=NULL;
-        LOG(("stopped irc"));
-    }
+    delete lobby_connection;
+    lobby_connection=0;
 }
 
Index: netpanzer/src/NetPanzer/Views/MainMenu/Multi/IRCLobbyView.hpp
diff -u netpanzer/src/NetPanzer/Views/MainMenu/Multi/IRCLobbyView.hpp:1.1 
netpanzer/src/NetPanzer/Views/MainMenu/Multi/IRCLobbyView.hpp:1.2
--- netpanzer/src/NetPanzer/Views/MainMenu/Multi/IRCLobbyView.hpp:1.1   Mon Nov 
 3 08:26:36 2003
+++ netpanzer/src/NetPanzer/Views/MainMenu/Multi/IRCLobbyView.hpp       Thu Nov 
 6 12:53:26 2003
@@ -27,8 +27,8 @@
 class IRCLobbyView : public View
 {
  int lobby_view_height;
- GameServer *mouse_down_server;
- GameServer *displayed_servers[64];
+ const GameServer *mouse_down_server;
+ const GameServer *displayed_servers[64];
  int total_displayed_servers;
 public:
  static cInputFieldString szChat;




reply via email to

[Prev in Thread] Current Thread [Next in Thread]