Redesign the network setup. Untested of course.

This commit is contained in:
simon.kagstrom 2010-02-12 06:35:24 +00:00
parent 530f10f842
commit a5618a93d7
3 changed files with 106 additions and 98 deletions

View File

@ -27,6 +27,8 @@
#include "utils.hh" #include "utils.hh"
#include "data_store.hh" #include "data_store.hh"
#include "gui/gui.hh"
#include "gui/network_user_menu.hh"
#if defined(GEKKO) #if defined(GEKKO)
# include <wiiuse/wpad.h> # include <wiiuse/wpad.h>
@ -88,6 +90,7 @@ Network::Network(const char *remote_host, int port)
memset(this->screenshot, 0, sizeof(this->screenshot)); memset(this->screenshot, 0, sizeof(this->screenshot));
Network::networking_started = true; Network::networking_started = true;
this->peer_selected = false;
/* Peer addresses, if it fails we are out of luck */ /* Peer addresses, if it fails we are out of luck */
if (this->InitSocket(remote_host, port) == false) if (this->InitSocket(remote_host, port) == false)
{ {
@ -1012,6 +1015,9 @@ bool Network::ConnectToBroker()
NetworkUpdatePeerInfo *pi = (NetworkUpdatePeerInfo *)ud->data; NetworkUpdatePeerInfo *pi = (NetworkUpdatePeerInfo *)ud->data;
bool out; bool out;
/* Reset peer selection */
this->peer_selected = false;
pi->is_master = 0; /* Will be set later */ pi->is_master = 0; /* Will be set later */
pi->key = ThePrefs.NetworkKey; pi->key = ThePrefs.NetworkKey;
pi->version = FRODO_NETWORK_PROTOCOL_VERSION; pi->version = FRODO_NETWORK_PROTOCOL_VERSION;
@ -1028,26 +1034,6 @@ bool Network::ConnectToBroker()
return out; return out;
} }
bool Network::IpToStr(char *dst, uint8 *ip_in)
{
int ip[4];
for (int i = 0; i < 4; i++)
{
char tmp[3];
char *endp;
tmp[0] = ip_in[i * 2];
tmp[1] = ip_in[i * 2 + 1];
tmp[2] = '\0';
ip[i] = strtoul(tmp, &endp, 16);
if (endp == (const char*)tmp)
return false;
}
sprintf(dst, "%d.%d.%d.%d", ip[3], ip[2], ip[1], ip[0]);
return true;
}
void Network::SendPingAck(int seq, uint16 type, size_t size_to_send) void Network::SendPingAck(int seq, uint16 type, size_t size_to_send)
{ {
NetworkUpdate *ud = InitNetworkUpdate(this->ud, type, size_to_send); NetworkUpdate *ud = InitNetworkUpdate(this->ud, type, size_to_send);
@ -1087,18 +1073,6 @@ network_connection_error_t Network::WaitForPeerAddress()
if (pi->peers[0].version != FRODO_NETWORK_PROTOCOL_VERSION) if (pi->peers[0].version != FRODO_NETWORK_PROTOCOL_VERSION)
return VERSION_ERROR; return VERSION_ERROR;
/* Setup the peer info */
char buf[128];
/* Not sure what to do if this fails */
this->IpToStr(buf, pi->peers[0].public_ip);
printf("Converted ip to %s:%d\n", buf, pi->peers[0].public_port);
if (this->InitSockaddr(&this->connection_addr, buf,
pi->peers[0].public_port) == false)
{
printf("Init sockaddr error\n");
return SERVER_GARBAGE_ERROR;
}
return OK; return OK;
} }
@ -1141,37 +1115,33 @@ network_connection_error_t Network::WaitForPeerList()
return SERVER_GARBAGE_ERROR; return SERVER_GARBAGE_ERROR;
pi = (NetworkUpdateListPeers *)this->ud->data; pi = (NetworkUpdateListPeers *)this->ud->data;
#if 0 for (unsigned i = 0; i < pi->n_peers; i++)
if (pi->peers[i].version != FRODO_NETWORK_PROTOCOL_VERSION)
{ {
free(msgs); if (pi->peers[i].version != FRODO_NETWORK_PROTOCOL_VERSION)
return VERSION_ERROR; return VERSION_ERROR;
} }
#endif
int sel = 0; // FIXME! menu_select_peer(pi->peers, pi->n_peers);
/* FIXME! What to do here??? */ Gui::gui->nuv->setPeers(pi);
if (sel < 0) Gui::gui->activate();
return SERVER_GARBAGE_ERROR; Gui::gui->pushView(Gui::gui->nuv);
if (sel == 0) {
/* We want to wait for a connection, and are therefore return OK;
* implicitly a master */
return NO_PEERS_ERROR;
} }
/* Correct the index */
sel--;
/* Setup the peer info */
char buf[128];
uint16 port = pi->peers[sel].public_port;
/* Not sure what to do if this fails */ bool Network::SelectPeer(const char *hostname, uint16_t port, uint32_t server_id)
this->IpToStr(buf, pi->peers[sel].public_ip); {
this->SelectPeer(server_id);
this->InitSockaddr(&this->connection_addr, hostname, port);
this->peer_selected = true;
return true;
}
network_connection_error_t Network::WaitForPeerSelection()
{
if (!this->peer_selected)
return AGAIN_ERROR;
/* Finally tell the broker who we selected */
this->SelectPeer(pi->peers[sel].server_id);
if (this->InitSockaddr(&this->connection_addr, buf,
port) == false)
return SERVER_GARBAGE_ERROR;
return OK; return OK;
} }
@ -1305,7 +1275,10 @@ network_connection_error_t Network::ConnectFSM()
break; break;
case CONN_WAIT_FOR_PEER_LIST: case CONN_WAIT_FOR_PEER_LIST:
/* Also tells the broker that we want to connect */ /* Also tells the broker that we want to connect */
err = this->WaitForPeerList(); return this->WaitForPeerList();
break;
case CONN_WAIT_FOR_PEER_SELECT:
err = this->WaitForPeerSelection();
if (err == OK) { if (err == OK) {
this->network_connection_state = CONN_CONNECT_TO_PEER; this->network_connection_state = CONN_CONNECT_TO_PEER;
this->is_master = false; this->is_master = false;

View File

@ -60,6 +60,7 @@ typedef enum
CONN_WAIT_FOR_PEER_REPLY, CONN_WAIT_FOR_PEER_REPLY,
CONN_WAIT_FOR_PEER_LIST, CONN_WAIT_FOR_PEER_LIST,
CONN_WAIT_FOR_PEER_SELECT,
CONN_BANDWIDTH_PING, CONN_BANDWIDTH_PING,
CONN_BANDWIDTH_REPLY, CONN_BANDWIDTH_REPLY,
@ -237,6 +238,8 @@ public:
return this->screen; return this->screen;
} }
bool SelectPeer(const char *hostname, uint16_t port, uint32_t server_id);
network_connection_error_t ConnectFSM(); network_connection_error_t ConnectFSM();
/** /**
@ -345,6 +348,8 @@ protected:
network_connection_error_t WaitForPeerAddress(); network_connection_error_t WaitForPeerAddress();
network_connection_error_t WaitForPeerSelection();
bool SelectPeer(uint32 id); bool SelectPeer(uint32 id);
size_t FillNetworkBuffer(NetworkUpdate *p); size_t FillNetworkBuffer(NetworkUpdate *p);
@ -376,6 +381,7 @@ protected:
Uint8 cur_joystick_data; Uint8 cur_joystick_data;
/* Connection to the peer */ /* Connection to the peer */
bool peer_selected;
int sock; int sock;
struct sockaddr_in connection_addr; struct sockaddr_in connection_addr;

View File

@ -3,20 +3,49 @@
#include "help_box.hh" #include "help_box.hh"
#include "status_bar.hh" #include "status_bar.hh"
#include "data_store.hh" #include "data_store.hh"
#include "network_user_menu.hh"
#include <Network.h> #include <Network.h>
class NetworkUserView; class NetworkUserView;
const char *ip_to_str(uint8 *ip_in)
{
char *out = (char *)xmalloc(24);
int ip[4];
for (int i = 0; i < 4; i++)
{
char tmp[3];
char *endp;
tmp[0] = ip_in[i * 2];
tmp[1] = ip_in[i * 2 + 1];
tmp[2] = '\0';
ip[i] = strtoul(tmp, &endp, 16);
panic_if (endp == (const char*)tmp,
"Could not convert ip to str.\n");
}
sprintf(out, "%d.%d.%d.%d", ip[3], ip[2], ip[1], ip[0]);
return out;
}
class PeerInfo class PeerInfo
{ {
public: public:
PeerInfo(const char *name, int scr_key) PeerInfo(NetworkUpdatePeerInfo *pi)
{ {
this->name = (const char*)xstrdup(name); this->name = (const char*)xstrdup((char*)pi->name);
this->scr = NULL; this->scr = NULL;
this->region = 0; this->region = 0;
this->scr_key = scr_key; this->scr_key = pi->screenshot_key;
this->public_port = pi->public_port;
this->private_port = pi->private_port;
this->server_id = pi->server_id;
this->hostname = ip_to_str(pi->public_ip);
} }
~PeerInfo() ~PeerInfo()
@ -58,6 +87,9 @@ public:
SDL_Surface *scr; SDL_Surface *scr;
const char *name; const char *name;
const char *hostname;
uint16_t public_port, private_port;
uint32_t server_id;
int region; int region;
int scr_key; int scr_key;
}; };
@ -165,7 +197,7 @@ public:
for (unsigned i = 0; i < peerList->n_peers; i++) for (unsigned i = 0; i < peerList->n_peers; i++)
{ {
messages[i] = (const char*)xstrdup((char*)ps->name); messages[i] = (const char*)xstrdup((char*)ps->name);
this->peers[i] = new PeerInfo(messages[i], ps->screenshot_key); this->peers[i] = new PeerInfo(&peerList->peers[i]);
} }
this->setText(messages); this->setText(messages);
free((void*)messages); free((void*)messages);
@ -204,31 +236,33 @@ private:
}; };
class NetworkUserView : public GuiView NetworkUserView::NetworkUserView() : GuiView()
{
public:
NetworkUserView() : GuiView()
{ {
this->peerInfo = new PeerInfoBox(Gui::gui->default_font); this->peerInfo = new PeerInfoBox(Gui::gui->default_font);
this->menu = new NetworkUserMenu(Gui::gui->default_font, this->peerInfo); this->menu = new NetworkUserMenu(Gui::gui->default_font, this->peerInfo);
} }
~NetworkUserView() NetworkUserView::~NetworkUserView()
{ {
delete this->menu; delete this->menu;
} }
void runLogic() void NetworkUserView::runLogic()
{ {
this->menu->runLogic(); this->menu->runLogic();
} }
void pushEvent(SDL_Event *ev) void NetworkUserView::pushEvent(SDL_Event *ev)
{ {
this->menu->pushEvent(ev); this->menu->pushEvent(ev);
} }
void draw(SDL_Surface *where) void NetworkUserView::setPeers(NetworkUpdateListPeers *peerList)
{
this->menu->setPeers(peerList);
}
void NetworkUserView::draw(SDL_Surface *where)
{ {
SDL_Rect dst; SDL_Rect dst;
@ -242,8 +276,3 @@ public:
this->menu->draw(where, 50, 70, 280, 375); this->menu->draw(where, 50, 70, 280, 375);
this->peerInfo->draw(where, 360, 55, 262, 447); this->peerInfo->draw(where, 360, 55, 262, 447);
} }
protected:
NetworkUserMenu *menu;
PeerInfoBox *peerInfo;
};