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 "data_store.hh"
#include "gui/gui.hh"
#include "gui/network_user_menu.hh"
#if defined(GEKKO)
# include <wiiuse/wpad.h>
@ -88,6 +90,7 @@ Network::Network(const char *remote_host, int port)
memset(this->screenshot, 0, sizeof(this->screenshot));
Network::networking_started = true;
this->peer_selected = false;
/* Peer addresses, if it fails we are out of luck */
if (this->InitSocket(remote_host, port) == false)
{
@ -1012,6 +1015,9 @@ bool Network::ConnectToBroker()
NetworkUpdatePeerInfo *pi = (NetworkUpdatePeerInfo *)ud->data;
bool out;
/* Reset peer selection */
this->peer_selected = false;
pi->is_master = 0; /* Will be set later */
pi->key = ThePrefs.NetworkKey;
pi->version = FRODO_NETWORK_PROTOCOL_VERSION;
@ -1028,26 +1034,6 @@ bool Network::ConnectToBroker()
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)
{
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)
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;
}
@ -1141,37 +1115,33 @@ network_connection_error_t Network::WaitForPeerList()
return SERVER_GARBAGE_ERROR;
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);
return VERSION_ERROR;
}
#endif
int sel = 0; // FIXME! menu_select_peer(pi->peers, pi->n_peers);
/* FIXME! What to do here??? */
if (sel < 0)
return SERVER_GARBAGE_ERROR;
if (sel == 0) {
/* We want to wait for a connection, and are therefore
* 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 */
this->IpToStr(buf, pi->peers[sel].public_ip);
Gui::gui->nuv->setPeers(pi);
Gui::gui->activate();
Gui::gui->pushView(Gui::gui->nuv);
return OK;
}
bool Network::SelectPeer(const char *hostname, uint16_t port, uint32_t server_id)
{
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;
}
@ -1305,7 +1275,10 @@ network_connection_error_t Network::ConnectFSM()
break;
case CONN_WAIT_FOR_PEER_LIST:
/* 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) {
this->network_connection_state = CONN_CONNECT_TO_PEER;
this->is_master = false;

View File

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

View File

@ -3,20 +3,49 @@
#include "help_box.hh"
#include "status_bar.hh"
#include "data_store.hh"
#include "network_user_menu.hh"
#include <Network.h>
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
{
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->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()
@ -58,6 +87,9 @@ public:
SDL_Surface *scr;
const char *name;
const char *hostname;
uint16_t public_port, private_port;
uint32_t server_id;
int region;
int scr_key;
};
@ -165,7 +197,7 @@ public:
for (unsigned i = 0; i < peerList->n_peers; i++)
{
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);
free((void*)messages);
@ -204,46 +236,43 @@ private:
};
class NetworkUserView : public GuiView
NetworkUserView::NetworkUserView() : GuiView()
{
this->peerInfo = new PeerInfoBox(Gui::gui->default_font);
this->menu = new NetworkUserMenu(Gui::gui->default_font, this->peerInfo);
}
NetworkUserView::~NetworkUserView()
{
public:
NetworkUserView() : GuiView()
{
this->peerInfo = new PeerInfoBox(Gui::gui->default_font);
this->menu = new NetworkUserMenu(Gui::gui->default_font, this->peerInfo);
}
delete this->menu;
}
~NetworkUserView()
{
delete this->menu;
}
void NetworkUserView::runLogic()
{
this->menu->runLogic();
}
void runLogic()
{
this->menu->runLogic();
}
void NetworkUserView::pushEvent(SDL_Event *ev)
{
this->menu->pushEvent(ev);
}
void pushEvent(SDL_Event *ev)
{
this->menu->pushEvent(ev);
}
void NetworkUserView::setPeers(NetworkUpdateListPeers *peerList)
{
this->menu->setPeers(peerList);
}
void draw(SDL_Surface *where)
{
SDL_Rect dst;
void NetworkUserView::draw(SDL_Surface *where)
{
SDL_Rect dst;
/* Blit the backgrounds */
dst = (SDL_Rect){20,45,300,400};
SDL_BlitSurface(Gui::gui->main_menu_bg, NULL, where, &dst);
/* Blit the backgrounds */
dst = (SDL_Rect){20,45,300,400};
SDL_BlitSurface(Gui::gui->main_menu_bg, NULL, where, &dst);
dst = (SDL_Rect){350,13,0,0};
SDL_BlitSurface(Gui::gui->disc_info, NULL, where, &dst);
dst = (SDL_Rect){350,13,0,0};
SDL_BlitSurface(Gui::gui->disc_info, NULL, where, &dst);
this->menu->draw(where, 50, 70, 280, 375);
this->peerInfo->draw(where, 360, 55, 262, 447);
}
protected:
NetworkUserMenu *menu;
PeerInfoBox *peerInfo;
};
this->menu->draw(where, 50, 70, 280, 375);
this->peerInfo->draw(where, 360, 55, 262, 447);
}