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

View File

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

View File

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

View File

@ -48,6 +48,11 @@ typedef struct
#define IS_SUBMENU(p_msg) ( (p_msg)[0] == '^' ) #define IS_SUBMENU(p_msg) ( (p_msg)[0] == '^' )
static TTF_Font *menu_font; 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) 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] = ' '; buf[i] = ' ';
} }
font_surf = TTF_RenderText_Blended(font, buf, font_surf = TTF_RenderText_Blended(menu_font, buf,
color); color);
if (!font_surf) 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; int y = (i - p_menu->start_entry_visible) * line_height;
if (p_menu->cur_sel == i) /* Selected - color */ 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); y_start + y, msg);
else /* Otherwise white */ 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); y_start + y, msg);
if (IS_SUBMENU(msg)) if (IS_SUBMENU(msg))
{ {
@ -441,7 +446,7 @@ int menu_select(const char **msgs, int *submenus)
menu_t menu; menu_t menu;
int out; int out;
menu_init(&menu, font, msgs, menu_init(&menu, menu_font, msgs,
32, 32, FULL_DISPLAY_X - FULL_DISPLAY_X / 4, 32, 32, FULL_DISPLAY_X - FULL_DISPLAY_X / 4,
FULL_DISPLAY_Y - FULL_DISPLAY_Y / 4); FULL_DISPLAY_Y - FULL_DISPLAY_Y / 4);
out = menu_select_internal(real_screen, &menu, submenus); out = menu_select_internal(real_screen, &menu, submenus);

View File

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