#include <distant_cpu.h>
Public Member Functions | |
DistantComputer (TCPsocket new_sock) | |
~DistantComputer () | |
bool | SocketReady () |
int | ReceiveDatas (char *&buf) |
void | SendDatas (char *paket, int size) |
std::string | GetAdress () |
void | ManageTeam (Action *team) |
void | SendChatMessage (Action *a) |
Public Attributes | |
std::string | nickname |
Private Attributes | |
SDL_mutex * | sock_lock |
TCPsocket | sock |
std::list< std::string > | owned_teams |
Definition at line 33 of file distant_cpu.h.
DistantComputer::DistantComputer | ( | TCPsocket | new_sock | ) |
Definition at line 35 of file distant_cpu.cpp.
00036 : sock(new_sock) 00037 { 00038 sock_lock = SDL_CreateMutex(); 00039 00040 SDLNet_TCP_AddSocket(network.socket_set, sock); 00041 00042 // If we are the server, we have to tell this new computer 00043 // what teams / maps have already been selected 00044 if( network.IsServer() ) 00045 { 00046 Action a(Action::ACTION_SET_MAP, ActiveMap().ReadName()); 00047 int size; 00048 char* pack; 00049 a.WritePacket(pack, size); 00050 SendDatas(pack, size); 00051 free(pack); 00052 00053 // Teams infos of already connected computers 00054 for(TeamsList::iterator team = teams_list.playing_list.begin(); 00055 team != teams_list.playing_list.end(); 00056 ++team) 00057 { 00058 Action b(Action::ACTION_NEW_TEAM, (*team)->GetId()); 00059 b.Push((*team)->GetPlayerName()); 00060 b.Push((int)(*team)->GetNbCharacters()); 00061 b.WritePacket(pack, size); 00062 SendDatas(pack, size); 00063 free(pack); 00064 } 00065 } 00066 00067 if(network.network_menu != NULL) 00068 { 00069 // Display a message in the network menu 00070 network.network_menu->ReceiveMsgCallback(GetAdress() + _(" has joined the party")); 00071 } 00072 }
Here is the call graph for this function:
DistantComputer::~DistantComputer | ( | ) |
Definition at line 74 of file distant_cpu.cpp.
00075 { 00076 if(network.network_menu != NULL) 00077 { 00078 // Display a message in the network menu 00079 network.network_menu->ReceiveMsgCallback( GetAdress() + _(" has left the party")); 00080 } 00081 00082 SDLNet_TCP_DelSocket(network.socket_set, sock); 00083 SDLNet_TCP_Close(sock); 00084 00085 if(network.IsConnected()) 00086 for(std::list<std::string>::iterator team = owned_teams.begin(); 00087 team != owned_teams.end(); 00088 ++team) 00089 { 00090 ActionHandler::GetInstance()->NewAction(new Action(Action::ACTION_DEL_TEAM, *team)); 00091 } 00092 owned_teams.clear(); 00093 00094 SDL_DestroyMutex(sock_lock); 00095 }
Here is the call graph for this function:
std::string DistantComputer::GetAdress | ( | ) |
Definition at line 158 of file distant_cpu.cpp.
00159 { 00160 IPaddress* ip = SDLNet_TCP_GetPeerAddress(sock); 00161 std::string address; 00162 const char* resolved_ip = SDLNet_ResolveIP(ip); 00163 if(resolved_ip) 00164 address = resolved_ip; 00165 else 00166 return "Unresolved address"; 00167 return address; 00168 }
Here is the caller graph for this function:
void DistantComputer::ManageTeam | ( | Action * | team | ) |
Definition at line 170 of file distant_cpu.cpp.
00171 { 00172 std::string name = team->PopString(); 00173 if(team->GetType() == Action::ACTION_NEW_TEAM) 00174 { 00175 owned_teams.push_back(name); 00176 00177 int index = 0; 00178 Team * tmp = teams_list.FindById(name, index); 00179 tmp->SetRemote(); 00180 00181 Action* copy = new Action(Action::ACTION_NEW_TEAM, name); 00182 copy->Push( team->PopString() ); 00183 copy->Push( team->PopInt() ); 00184 ActionHandler::GetInstance()->NewAction(copy, false); 00185 } 00186 else 00187 if(team->GetType() == Action::ACTION_DEL_TEAM) 00188 { 00189 std::list<std::string>::iterator it; 00190 it = find(owned_teams.begin(), owned_teams.end(), name); 00191 std::cout << "ManageTeam : erase " << name << std::endl; 00192 assert(it != owned_teams.end()); 00193 owned_teams.erase(it); 00194 ActionHandler::GetInstance()->NewAction(new Action(Action::ACTION_DEL_TEAM, name), false); 00195 } 00196 else 00197 assert(false); 00198 }
Here is the call graph for this function:
int DistantComputer::ReceiveDatas | ( | char *& | buf | ) |
Definition at line 102 of file distant_cpu.cpp.
00103 { 00104 SDL_LockMutex(sock_lock); 00105 MSG_DEBUG("network","locked"); 00106 00107 // Firstly, we read the size of the incoming packet 00108 Uint32 net_size; 00109 if(SDLNet_TCP_Recv(sock, &net_size, 4) <= 0) 00110 { 00111 SDL_UnlockMutex(sock_lock); 00112 return -1; 00113 } 00114 00115 int size = (int)SDLNet_Read32(&net_size); 00116 assert(size > 0); 00117 00118 // Now we read the packet 00119 buf = (char*)malloc(size); 00120 00121 int total_received = 0; 00122 while(total_received != size) 00123 { 00124 int received = SDLNet_TCP_Recv(sock, buf + total_received, size - total_received); 00125 if(received > 0) 00126 { 00127 MSG_DEBUG("network", "%i received", received); 00128 total_received += received; 00129 } 00130 00131 if(received < 0) 00132 { 00133 assert(false); 00134 std::cerr << "Malformed packet" << std::endl; 00135 total_received = received; 00136 break; 00137 } 00138 } 00139 SDL_UnlockMutex(sock_lock); 00140 MSG_DEBUG("network","unlocked"); 00141 return total_received; 00142 }
void DistantComputer::SendChatMessage | ( | Action * | a | ) |
Definition at line 200 of file distant_cpu.cpp.
00201 { 00202 std::string txt = a->PopString(); 00203 if(network.IsServer()) 00204 { 00205 ActionHandler::GetInstance()->NewAction(new Action(Action::ACTION_CHAT_MESSAGE, nickname + "> "+txt)); 00206 } 00207 else 00208 { 00209 ActionHandler::GetInstance()->NewAction(new Action(Action::ACTION_CHAT_MESSAGE, txt), false); 00210 } 00211 }
Here is the call graph for this function:
void DistantComputer::SendDatas | ( | char * | paket, | |
int | size | |||
) |
Definition at line 144 of file distant_cpu.cpp.
00145 { 00146 SDL_LockMutex(sock_lock); 00147 MSG_DEBUG("network","locked"); 00148 Uint32 size; 00149 SDLNet_Write32(size_tmp, &size); 00150 SDLNet_TCP_Send(sock,&size,4); 00151 SDLNet_TCP_Send(sock,packet, size_tmp); 00152 MSG_DEBUG("network","%i sent", 4 + size_tmp); 00153 00154 SDL_UnlockMutex(sock_lock); 00155 MSG_DEBUG("network","unlocked"); 00156 }
Here is the caller graph for this function:
bool DistantComputer::SocketReady | ( | ) |
Definition at line 97 of file distant_cpu.cpp.
00098 { 00099 return SDLNet_SocketReady(sock); 00100 }
std::string DistantComputer::nickname |
Definition at line 48 of file distant_cpu.h.
std::list<std::string> DistantComputer::owned_teams [private] |
Definition at line 37 of file distant_cpu.h.
TCPsocket DistantComputer::sock [private] |
Definition at line 36 of file distant_cpu.h.
SDL_mutex* DistantComputer::sock_lock [private] |
Definition at line 35 of file distant_cpu.h.