Rework network handling to let go of the server/client approach

You now connect and become server/client on demand.
This commit is contained in:
simon.kagstrom 2009-10-28 18:04:30 +00:00
parent 76c522249c
commit 7be94b966a
4 changed files with 49 additions and 47 deletions

View File

@ -45,7 +45,7 @@
enum enum
{ {
NONE, NONE,
MASTER_CONNECT, CONNECT,
MASTER, MASTER,
CLIENT CLIENT
}; };

View File

@ -65,7 +65,7 @@ void C64::c64_ctor1(void)
if (fixme_tmp_network_server) { if (fixme_tmp_network_server) {
strcpy(this->server_hostname, fixme_tmp_network_server); strcpy(this->server_hostname, fixme_tmp_network_server);
this->peer = new Network(this->server_hostname, this->server_port, true); this->peer = new Network(this->server_hostname, this->server_port);
this->network_connection_type = MASTER; this->network_connection_type = MASTER;
printf("Waiting for connection\n"); printf("Waiting for connection\n");
if (this->peer->Connect() == false) if (this->peer->Connect() == false)
@ -75,18 +75,6 @@ void C64::c64_ctor1(void)
this->peer = NULL; this->peer = NULL;
} }
} }
if (fixme_tmp_network_client)
{
strcpy(this->server_hostname, fixme_tmp_network_client);
this->peer = new Network(this->server_hostname, this->server_port, false);
this->network_connection_type = CLIENT;
if (this->peer->Connect() == false)
{
printf("Could not connect to server. Bye\n");
delete this->peer;
this->peer = NULL;
}
}
} }
void C64::c64_ctor2(void) void C64::c64_ctor2(void)
@ -250,8 +238,8 @@ void C64::networking_menu(Prefs *np)
buf[0], /* 0 */ buf[0], /* 0 */
buf[1], /* 1 */ buf[1], /* 1 */
buf[2], /* 2 */ buf[2], /* 2 */
"Host a game", /* 3 */ "#2 ", /* 3 */
"Connect as client", /* 4 */ "Connect to the C64 network!", /* 4 */
NULL, NULL,
}; };
@ -282,21 +270,19 @@ void C64::networking_menu(Prefs *np)
this->server_port = atoi(m); this->server_port = atoi(m);
} }
} }
else if (opt == 3 || opt == 4) { else if (opt == 4) {
bool master = (opt == 3); if (strncmp(np->NetworkName, "Unset", 5) == 0)
this->peer = new Network(this->server_hostname,
this->server_port, master);
this->network_connection_type = master ? MASTER_CONNECT : CLIENT;
if (this->network_connection_type == CLIENT &&
this->peer->Connect() == false)
{ {
delete this->peer; char *msg = "Select name first";
this->peer = NULL;
this->network_connection_type = NONE; msgYesNo(msg, false, 160, 160);
continue;
} }
this->peer = new Network(this->server_hostname, this->server_port);
this->network_connection_type = CONNECT;
} }
} while (opt == 1 || opt == 2); } while (opt >= 0 && opt <= 2);
this->prefs_changed = true; this->prefs_changed = true;
} }
@ -496,7 +482,7 @@ void C64::network_vblank()
if (this->quit_thyself) if (this->quit_thyself)
{ {
if (this->network_connection_type != MASTER_CONNECT) if (this->network_connection_type != CONNECT)
remote->Disconnect(); remote->Disconnect();
delete remote; delete remote;
this->peer = NULL; this->peer = NULL;
@ -509,17 +495,23 @@ void C64::network_vblank()
js = &TheCIA1->Joystick1; js = &TheCIA1->Joystick1;
else else
js = &TheCIA1->Joystick2; js = &TheCIA1->Joystick2;
} else if (this->network_connection_type == MASTER_CONNECT) { } else if (this->network_connection_type == CONNECT) {
network_connection_error_t err = this->peer->ConnectFSM(); network_connection_error_t err = this->peer->ConnectFSM();
TheDisplay->display_status_string("WAITING FOR CONNECTION...", 1); TheDisplay->display_status_string("WAITING FOR CONNECTION...", 1);
if (err == OK) { if (err == OK) {
if (Network::is_master)
this->network_connection_type = MASTER; this->network_connection_type = MASTER;
TheDisplay->display_status_string("CLIENT CONNECTED!", 1); else
this->network_connection_type = CLIENT;
TheDisplay->display_status_string("CONNECTED!", 1);
} }
else if (err != AGAIN_ERROR) else if (err != AGAIN_ERROR)
{ {
if (err == VERSION_ERROR) {
TheDisplay->display_status_string("YOUR FRODO IS TOO OLD, DOWNLOAD NEW AT HTTP://FRODO-WII.GOOGLECODE.COM", 5);
}
delete remote; delete remote;
this->peer = NULL; this->peer = NULL;
} }

View File

@ -42,13 +42,13 @@
#define RLE_SIZE ( RAW_SIZE * 4 + 8) #define RLE_SIZE ( RAW_SIZE * 4 + 8)
#define DIFF_SIZE ( RAW_SIZE * 4 + 8) #define DIFF_SIZE ( RAW_SIZE * 4 + 8)
Network::Network(const char *remote_host, int port, bool is_master) Network::Network(const char *remote_host, int port)
{ {
const size_t size = NETWORK_UPDATE_SIZE; const size_t size = NETWORK_UPDATE_SIZE;
this->InitNetwork(); this->InitNetwork();
Network::is_master = is_master; Network::is_master = true; /* Assume true */
this->connected = false; this->connected = false;
/* "big enough" buffer */ /* "big enough" buffer */
@ -1040,17 +1040,19 @@ 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 (pi->n_peers == 0) msgs = (const char**)calloc(pi->n_peers + 2, sizeof(const char*));
return NO_PEERS_ERROR;
msgs = (const char**)calloc(pi->n_peers + 1, sizeof(const char*));
msgs[0] = "None (wait for peer to connect)";
printf("Got %d peers\n", pi->n_peers);
for (int i = 0; i < pi->n_peers; i++) { for (int i = 0; i < pi->n_peers; i++) {
msgs[i] = (const char*)pi->peers[i].name; msgs[i + 1] = (const char*)pi->peers[i].name;
#if 0
if (pi->peers[i].version != FRODO_NETWORK_PROTOCOL_VERSION) if (pi->peers[i].version != FRODO_NETWORK_PROTOCOL_VERSION)
{ {
free(msgs); free(msgs);
return VERSION_ERROR; return VERSION_ERROR;
} }
#endif
} }
int sel = menu_select(msgs, NULL); int sel = menu_select(msgs, NULL);
free(msgs); free(msgs);
@ -1058,6 +1060,15 @@ network_connection_error_t Network::WaitForPeerList()
/* FIXME! What to do here??? */ /* FIXME! What to do here??? */
if (sel < 0) if (sel < 0)
return SERVER_GARBAGE_ERROR; return SERVER_GARBAGE_ERROR;
if (sel == 0) {
/* We want to wait for a connection, and are therefore
* implicitly a master */
is_master = true;
return NO_PEERS_ERROR;
}
Network::is_master = false;
/* Correct the index */
sel--;
/* Setup the peer info */ /* Setup the peer info */
char buf[128]; char buf[128];
uint16 port = pi->peers[sel].public_port; uint16 port = pi->peers[sel].public_port;
@ -1130,14 +1141,10 @@ network_connection_error_t Network::ConnectFSM()
switch(this->network_connection_state) switch(this->network_connection_state)
{ {
case CONN_CONNECT_TO_BROKER: case CONN_CONNECT_TO_BROKER:
if (this->ConnectToBroker() == true)
{ {
if (Network::is_master) if (this->ConnectToBroker())
this->network_connection_state = CONN_WAIT_FOR_PEER_ADDRESS;
else
this->network_connection_state = CONN_WAIT_FOR_PEER_LIST; this->network_connection_state = CONN_WAIT_FOR_PEER_LIST;
} } break;
break;
case CONN_WAIT_FOR_PEER_ADDRESS: case CONN_WAIT_FOR_PEER_ADDRESS:
err = this->WaitForPeerAddress(); err = this->WaitForPeerAddress();
if (err == OK) if (err == OK)
@ -1150,6 +1157,8 @@ network_connection_error_t Network::ConnectFSM()
err = this->WaitForPeerList(); err = this->WaitForPeerList();
if (err == OK) if (err == OK)
this->network_connection_state = CONN_CONNECT_TO_PEER; this->network_connection_state = CONN_CONNECT_TO_PEER;
else if (err == NO_PEERS_ERROR)
this->network_connection_state = CONN_WAIT_FOR_PEER_ADDRESS;
else else
return err; return err;
break; break;
@ -1171,6 +1180,7 @@ network_connection_error_t Network::ConnectFSM()
return AGAIN_ERROR; return AGAIN_ERROR;
break; break;
case CONN_CONNECTED: case CONN_CONNECTED:
/* The lowest number is the default master */
default: default:
return OK; return OK;
} }

View File

@ -144,7 +144,7 @@ static inline NetworkUpdate *InitNetworkUpdate(NetworkUpdate *ud, uint16 type, u
class Network class Network
{ {
public: public:
Network(const char *remote_host, int port, bool is_master); Network(const char *remote_host, int port);
~Network(); ~Network();