The network stuff should be finsihed now. But it's untested still

This commit is contained in:
simon.kagstrom 2009-02-16 19:20:00 +00:00
parent 18b0f333f5
commit 4e90dad903
6 changed files with 46 additions and 42 deletions

View File

@ -13,12 +13,10 @@
#if defined(GEKKO)
#include <wiiuse/wpad.h>
#include <ogc/lwp_watchdog.h>
#define FONT_PATH "/apps/frodo/FreeMono.ttf"
#define SAVES_PATH "/apps/frodo/saves"
#define IMAGE_PATH "/apps/frodo/images"
#define TMP_PATH "/apps/frodo/tmp"
#else
#define FONT_PATH "FreeMono.ttf"
#define SAVES_PATH "saves"
#define IMAGE_PATH "images"
#define TMP_PATH "tmp"

View File

@ -76,7 +76,7 @@ Network::Network(const char *remote_host, int port, bool is_master)
fprintf(stderr, "Could not init the socket\n");
exit(1);
}
this->network_connection_state = CONNECT_TO_BROKER;
this->network_connection_state = CONN_CONNECT_TO_BROKER;
}
Network::~Network()
@ -578,7 +578,7 @@ bool Network::MarshalData(NetworkUpdate *p)
case SOUND_UPDATE_RLE:
case JOYSTICK_UPDATE:
case DISCONNECT:
case PEER_CONNECT:
case CONNECT_TO_PEER:
case STOP:
break;
case LIST_PEERS:
@ -645,7 +645,7 @@ bool Network::DeMarshalData(NetworkUpdate *p)
case SOUND_UPDATE_RLE:
case JOYSTICK_UPDATE:
case DISCONNECT:
case PEER_CONNECT:
case CONNECT_TO_PEER:
case STOP:
/* Nothing to do, just bytes */
break;
@ -757,13 +757,13 @@ bool Network::IpToStr(char *dst, uint8 *ip_in)
for (int i = 0; i < 4; i++)
{
char tmp[3];
const char *endp;
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 == tmp)
if (endp == (const char*)tmp)
return false;
}
sprintf(dst, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
@ -782,7 +782,7 @@ bool Network::WaitForPeerAddress()
this->ResetNetworkUpdate();
if (this->ReceiveUpdate(&tv) == false)
return false;
if (ud->type != PEER_LIST)
if (ud->type != LIST_PEERS)
return false;
pi = (NetworkUpdateListPeers *)this->ud->data;
@ -799,7 +799,7 @@ bool Network::WaitForPeerAddress()
/* Not sure what to do if this fails */
this->IpToStr(buf, pi->peers[0].public_ip);
return this->InitSockaddr(this->connection_addr, buf,
return this->InitSockaddr(&this->connection_addr, buf,
pi->peers[0].public_port);
}
@ -816,14 +816,14 @@ bool Network::WaitForPeerList()
this->ResetNetworkUpdate();
if (this->ReceiveUpdate(&tv) == false)
return false;
if (ud->type != PEER_LIST)
if (ud->type != LIST_PEERS)
return false;
pi = (NetworkUpdateListPeers *)this->ud->data;
msgs = (const char**)calloc(pi->n_peers + 1, sizeof(const char*));
for (int i = 0; pi->n_peers; i++) {
msgs[i] = pi->peers[i].name;
msgs[i] = (const char*)pi->peers[i].name;
}
int sel = menu_select(msgs, NULL);
free(msgs);
@ -836,7 +836,7 @@ bool Network::WaitForPeerList()
/* Not sure what to do if this fails */
this->IpToStr(buf, pi->peers[sel].public_ip);
return this->InitSockaddr(this->connection_addr, buf,
return this->InitSockaddr(&this->connection_addr, buf,
pi->peers[sel].public_port);
}
@ -862,7 +862,8 @@ bool Network::WaitForPeerReply()
bool Network::ConnectToPeer()
{
NetworkUpdate *ud = InitNetworkUpdate(this->ud, CONNECT_TO_PEER,
sizeof(NetworkUpdate));
sizeof(NetworkUpdate));
bool out;
this->AddNetworkUpdate(ud);
out = this->SendUpdate();
@ -895,42 +896,42 @@ bool Network::ConnectFSM()
*/
switch(this->network_connection_state)
{
case CONNECT_TO_BROKER:
case CONN_CONNECT_TO_BROKER:
if (this->ConnectToBroker() == true)
{
if (this->is_master)
this->network_connection_state = WAIT_FOR_PEER_ADDRESS;
this->network_connection_state = CONN_WAIT_FOR_PEER_ADDRESS;
else
this->network_connection_state = WAIT_FOR_PEER_LIST;
this->network_connection_state = CONN_WAIT_FOR_PEER_LIST;
}
break;
case WAIT_FOR_PEER_ADDRESS:
case CONN_WAIT_FOR_PEER_ADDRESS:
if (this->WaitForPeerAddress() == false)
return false;
this->network_connection_state = CONNECT_TO_PEER;
this->network_connection_state = CONN_CONNECT_TO_PEER;
break;
case WAIT_FOR_PEER_LIST:
case CONN_WAIT_FOR_PEER_LIST:
if (this->WaitForPeerList() == false)
return false;
this->network_connection_state = CONNECT_TO_PEER;
this->network_connection_state = CONN_CONNECT_TO_PEER;
break;
case CONNECT_TO_PEER:
case CONN_CONNECT_TO_PEER:
if (this->ConnectToPeer() == false)
return false;
/* Allow some transit time */
sleep(1);
this->network_connection_state = WAIT_FOR_PEER_REPLY;
this->network_connection_state = CONN_WAIT_FOR_PEER_REPLY;
break;
case WAIT_FOR_PEER_REPLY:
case CONN_WAIT_FOR_PEER_REPLY:
/* Connect again in case the first sent was dropped on
* its way to the peer */
if (this->ConnectToPeer() == false)
return false;
if (this->WaitForPeerReply() == false)
return false;
this->network_connection_state = CONNECTED;
this->network_connection_state = CONN_CONNECTED;
break;
case CONNECTED:
case CONN_CONNECTED:
default:
return true;
}
@ -942,13 +943,13 @@ bool Network::Connect()
{
for (int i = 0; i < this->is_master ? 120 : 10; i++ )
{
if (this->network_connection_state == CONNECTED)
if (this->network_connection_state == CONN_CONNECTED)
return true;
/* Run the state machine */
this->ConnectFSM();
}
return false;a
return false;
}
void Network::Disconnect()

View File

@ -12,7 +12,7 @@
#define NETWORK_UPDATE_SIZE (256 * 1024)
#define NETWORK_SOUND_BUF_SIZE 1024
enum
typedef enum
{
/* Connection-related messages */
CONNECT_TO_BROKER = 99, /* Hello, broker */
@ -32,18 +32,18 @@ enum
KEYBOARD_UPDATE = 6,
JOYSTICK_UPDATE = 7,
ENTER_MENU = 8,
};
} network_message_type_t;
typedef enum
{
CONNECTED,
CONNECT_TO_BROKER,
WAIT_FOR_PEER_ADDRESS,
CONNECT_TO_PEER,
WAIT_FOR_PEER_REPLY,
CONN_CONNECTED,
CONN_CONNECT_TO_BROKER,
CONN_WAIT_FOR_PEER_ADDRESS,
CONN_CONNECT_TO_PEER,
CONN_WAIT_FOR_PEER_REPLY,
/* Client-only */
WAIT_FOR_PEER_LIST,
CONN_WAIT_FOR_PEER_LIST,
} network_connection_state_t;
struct NetworkUpdate

View File

@ -78,7 +78,7 @@ bool Network::InitSocket(const char *remote_host, int port)
set_sock_opts(this->sock);
/* Connect to the server. */
init_sockaddr(&this->connection_addr, remote_host, port);
this->InitSockaddr(&this->connection_addr, remote_host, port);
if (this->is_master)
{

View File

@ -48,6 +48,11 @@ typedef struct
#define IS_SUBMENU(p_msg) ( (p_msg)[0] == '^' )
static TTF_Font *menu_font;
#if defined(GEKKO)
#define FONT_PATH "/apps/frodo/FreeMono.ttf"
#else
#define FONT_PATH "FreeMono.ttf"
#endif
static submenu_t *find_submenu(menu_t *p_menu, int index)
{
@ -81,7 +86,7 @@ void menu_print_font(SDL_Surface *screen, int r, int g, int b,
buf[i] = ' ';
}
font_surf = TTF_RenderText_Blended(font, buf,
font_surf = TTF_RenderText_Blended(menu_font, buf,
color);
if (!font_surf)
{
@ -117,10 +122,10 @@ static void menu_draw(SDL_Surface *screen, menu_t *p_menu)
int y = (i - p_menu->start_entry_visible) * line_height;
if (p_menu->cur_sel == i) /* Selected - color */
menu_print_font(screen, p_menu->p_font, 255,255,0, x_start,
menu_print_font(screen, 255,255,0, x_start,
y_start + y, msg);
else /* Otherwise white */
menu_print_font(screen, p_menu->p_font, 255,255,255, x_start,
menu_print_font(screen, 255,255,255, x_start,
y_start + y, msg);
if (IS_SUBMENU(msg))
{
@ -441,7 +446,7 @@ int menu_select(const char **msgs, int *submenus)
menu_t menu;
int out;
menu_init(&menu, font, msgs,
menu_init(&menu, menu_font, msgs,
32, 32, FULL_DISPLAY_X - FULL_DISPLAY_X / 4,
FULL_DISPLAY_Y - FULL_DISPLAY_Y / 4);
out = menu_select_internal(real_screen, &menu, submenus);

View File

@ -29,8 +29,8 @@ extern "C" {
#define KEY_PAGEDOWN 64
#define KEY_PAGEUP 128
void menu_print_font(SDL_Surface *screen, TTF_Font *font, int r, int g, int b,
int x, int y, const char *msg);
void menu_print_font(SDL_Surface *screen, int r, int g, int b,
int x, int y, const char *msg);
int menu_select(const char **pp_msgs, int *p_submenus);