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
{
NONE,
MASTER_CONNECT,
CONNECT,
MASTER,
CLIENT
};

View File

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

View File

@ -42,13 +42,13 @@
#define RLE_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;
this->InitNetwork();
Network::is_master = is_master;
Network::is_master = true; /* Assume true */
this->connected = false;
/* "big enough" buffer */
@ -1040,17 +1040,19 @@ network_connection_error_t Network::WaitForPeerList()
return SERVER_GARBAGE_ERROR;
pi = (NetworkUpdateListPeers *)this->ud->data;
if (pi->n_peers == 0)
return NO_PEERS_ERROR;
msgs = (const char**)calloc(pi->n_peers + 1, sizeof(const char*));
msgs = (const char**)calloc(pi->n_peers + 2, 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++) {
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)
{
free(msgs);
return VERSION_ERROR;
}
#endif
}
int sel = menu_select(msgs, NULL);
free(msgs);
@ -1058,6 +1060,15 @@ network_connection_error_t Network::WaitForPeerList()
/* 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 */
is_master = true;
return NO_PEERS_ERROR;
}
Network::is_master = false;
/* Correct the index */
sel--;
/* Setup the peer info */
char buf[128];
uint16 port = pi->peers[sel].public_port;
@ -1130,14 +1141,10 @@ network_connection_error_t Network::ConnectFSM()
switch(this->network_connection_state)
{
case CONN_CONNECT_TO_BROKER:
if (this->ConnectToBroker() == true)
{
if (Network::is_master)
this->network_connection_state = CONN_WAIT_FOR_PEER_ADDRESS;
else
if (this->ConnectToBroker())
this->network_connection_state = CONN_WAIT_FOR_PEER_LIST;
}
break;
} break;
case CONN_WAIT_FOR_PEER_ADDRESS:
err = this->WaitForPeerAddress();
if (err == OK)
@ -1150,6 +1157,8 @@ network_connection_error_t Network::ConnectFSM()
err = this->WaitForPeerList();
if (err == OK)
this->network_connection_state = CONN_CONNECT_TO_PEER;
else if (err == NO_PEERS_ERROR)
this->network_connection_state = CONN_WAIT_FOR_PEER_ADDRESS;
else
return err;
break;
@ -1171,6 +1180,7 @@ network_connection_error_t Network::ConnectFSM()
return AGAIN_ERROR;
break;
case CONN_CONNECTED:
/* The lowest number is the default master */
default:
return OK;
}

View File

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