00001 /****************************************************************************** 00002 * Wormux is a convivial mass murder game. 00003 * Copyright (C) 2001-2004 Lawrence Azzoug. 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License as published by 00007 * the Free Software Foundation; either version 2 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA 00018 ****************************************************************************** 00019 * Network layer for Wormux. 00020 *****************************************************************************/ 00021 00022 #ifndef NETWORK_H 00023 #define NETWORK_H 00024 //----------------------------------------------------------------------------- 00025 #include <SDL_net.h> 00026 #include <SDL_thread.h> 00027 #include <list> 00028 #include <string> 00029 #include "distant_cpu.h" 00030 #include "../include/action.h" 00031 #include "../include/base.h" 00032 #include "../menu/network_menu.h" 00033 //----------------------------------------------------------------------------- 00034 const std::string WORMUX_NETWORK_PORT = "9999"; 00035 const uint WORMUX_NETWORK_PORT_INT = 9999; 00036 00037 class Network 00038 { 00039 friend class DistantComputer; 00040 00041 bool inited; 00042 00043 #if defined(DEBUG) && not defined(WIN32) 00044 int fout; 00045 int fin; 00046 #endif 00047 00048 protected: 00049 bool m_is_connected; 00050 bool m_is_server; 00051 bool m_is_client; 00052 00053 TCPsocket server_socket; // Wait for incoming connections on this socket 00054 SDL_Thread* thread; // network thread, where we receive data from network 00055 SDLNet_SocketSet socket_set; 00056 IPaddress ip; // for server : store listening port 00057 // for client : store server address/port 00058 00059 public: 00060 NetworkMenu* network_menu; 00061 00062 typedef enum 00063 { 00064 NETWORK_NOT_CONNECTED, 00065 NETWORK_OPTION_SCREEN, 00066 NETWORK_INIT_GAME, 00067 NETWORK_READY_TO_PLAY, 00068 NETWORK_PLAYING 00069 } network_state_t; 00070 network_state_t state; 00071 00072 std::list<DistantComputer*> cpu; // list of the connected computer 00073 uint max_player_number; 00074 uint connected_player; 00075 uint client_inited; 00076 bool sync_lock; 00077 std::string nickname; //Clients: Send to Server at connect 00078 //Server: Send in chat messages 00079 00080 Network(); 00081 ~Network(); 00082 void Init(); 00083 00084 const bool IsConnected() const; 00085 const bool IsLocal() const; 00086 const bool IsServer() const; 00087 const bool IsClient() const; 00088 const uint GetPort() const; 00089 00090 // Network functions common to client and server 00091 void Disconnect(); 00092 00093 // Action handling 00094 void SendAction(Action* action); 00095 void SendPacket(char* packet, int size); 00096 void ReceiveActions(); 00097 // Client specific 00098 void ClientConnect(const std::string &host, const std::string &port); 00099 00100 // Serveur specific methods 00101 void ServerStart(const std::string &port); 00102 void AcceptIncoming(); 00103 void RejectIncoming(); 00104 std::list<DistantComputer*>::iterator CloseConnection(std::list<DistantComputer*>::iterator closed); 00105 00106 void SendChatMessage(std::string txt); 00107 }; 00108 00109 extern Network network; 00110 //----------------------------------------------------------------------------- 00111 #endif