Separate server and peer addresses

This commit is contained in:
simon.kagstrom 2010-02-14 08:57:55 +00:00
parent f2a9185cc0
commit 7ab4f8f443
5 changed files with 37 additions and 26 deletions

View File

@ -234,7 +234,7 @@ void C64::network_vblank()
remote->EncodeJoystickUpdate(*js); remote->EncodeJoystickUpdate(*js);
if (remote->SendUpdate() == false) if (remote->SendPeerUpdate() == false)
{ {
/* Disconnect or broken data */ /* Disconnect or broken data */
printf("Could not send update\n"); printf("Could not send update\n");

View File

@ -92,11 +92,15 @@ Network::Network(const char *remote_host, int port)
Network::networking_started = true; Network::networking_started = true;
this->peer_selected = -1; this->peer_selected = -1;
/* Peer addresses, if it fails we are out of luck */ /* Peer addresses, if it fails we are out of luck */
if (this->InitSocket(remote_host, port) == false) panic_if (this->InitSocket() == false,
{ "Could not init the socket\n");
fprintf(stderr, "Could not init the socket\n");
exit(1); /* Setup the socket addresses */
} memset(&this->peer_addr, 0, sizeof(this->peer_addr));
memset(&this->server_addr, 0, sizeof(this->server_addr));
panic_if (this->InitSockaddr(&this->server_addr, remote_host, port) == false,
"Can't initialize socket address to server\n");
this->network_connection_state = CONN_CONNECT_TO_BROKER; this->network_connection_state = CONN_CONNECT_TO_BROKER;
this->connection_error_message = "Connection OK"; this->connection_error_message = "Connection OK";
} }
@ -589,7 +593,7 @@ bool Network::ReceiveUpdate(NetworkUpdate *dst, size_t total_sz,
return true; return true;
} }
bool Network::SendUpdate() bool Network::SendUpdate(struct sockaddr_in *addr)
{ {
NetworkUpdate *src = this->ud; NetworkUpdate *src = this->ud;
NetworkUpdate *stop = InitNetworkUpdate(this->cur_ud, STOP, sizeof(NetworkUpdate)); NetworkUpdate *stop = InitNetworkUpdate(this->cur_ud, STOP, sizeof(NetworkUpdate));
@ -616,7 +620,7 @@ bool Network::SendUpdate()
ssize_t v; ssize_t v;
v = this->SendTo((void*)p, this->sock, v = this->SendTo((void*)p, this->sock,
size_to_send, &this->peer_addr); size_to_send, addr);
if (v < 0 || (size_t)v != size_to_send) if (v < 0 || (size_t)v != size_to_send)
return false; return false;
cur_sz += size_to_send; cur_sz += size_to_send;
@ -1028,7 +1032,7 @@ bool Network::ConnectToBroker()
this->AddNetworkUpdate(ud); this->AddNetworkUpdate(ud);
out = this->AppendScreenshot(pi); out = this->AppendScreenshot(pi);
if (out) if (out)
out = this->SendUpdate(); out = this->SendServerUpdate();
this->ResetNetworkUpdate(); this->ResetNetworkUpdate();
return out; return out;
@ -1055,7 +1059,7 @@ network_connection_error_t Network::WaitForPeerAddress()
NetworkUpdatePingAck *p = (NetworkUpdatePingAck*)ud->data; NetworkUpdatePingAck *p = (NetworkUpdatePingAck*)ud->data;
/* Send ack and go back to this state again */ /* Send ack and go back to this state again */
this->SendPingAck(p->seq, ACK, ud->size); this->SendPingAck(p->seq, ACK, ud->size);
this->SendUpdate(); this->SendServerUpdate();
this->ResetNetworkUpdate(); this->ResetNetworkUpdate();
return AGAIN_ERROR; return AGAIN_ERROR;
} }
@ -1085,7 +1089,7 @@ bool Network::SelectPeer(uint32 id)
p->server_id = id; p->server_id = id;
this->AddNetworkUpdate(ud); this->AddNetworkUpdate(ud);
out = this->SendUpdate(); out = this->SendServerUpdate();
this->ResetNetworkUpdate(); this->ResetNetworkUpdate();
return out; return out;
@ -1107,7 +1111,7 @@ network_connection_error_t Network::WaitForPeerList()
NetworkUpdatePingAck *p = (NetworkUpdatePingAck*)ud->data; NetworkUpdatePingAck *p = (NetworkUpdatePingAck*)ud->data;
/* Send ack and go back to this state again */ /* Send ack and go back to this state again */
this->SendPingAck(p->seq, ACK, ud->size); this->SendPingAck(p->seq, ACK, ud->size);
this->SendUpdate(); this->SendServerUpdate();
this->ResetNetworkUpdate(); this->ResetNetworkUpdate();
return AGAIN_ERROR; return AGAIN_ERROR;
} }
@ -1175,7 +1179,7 @@ bool Network::ConnectToPeer()
bool out; bool out;
this->AddNetworkUpdate(ud); this->AddNetworkUpdate(ud);
out = this->SendUpdate(); out = this->SendServerUpdate();
this->ResetNetworkUpdate(); this->ResetNetworkUpdate();
return out; return out;
@ -1202,7 +1206,7 @@ network_connection_error_t Network::WaitForBandWidthReply()
this->ResetNetworkUpdate(); this->ResetNetworkUpdate();
this->SendPingAck(seq, BANDWIDTH_ACK, sz); this->SendPingAck(seq, BANDWIDTH_ACK, sz);
this->SendUpdate(); this->SendServerUpdate();
continue; continue;
} }
/* CONNECT_TO_PEER is sent twice, so we might get it here */ /* CONNECT_TO_PEER is sent twice, so we might get it here */
@ -1317,7 +1321,7 @@ network_connection_error_t Network::ConnectFSM()
case CONN_BANDWIDTH_PING: case CONN_BANDWIDTH_PING:
this->ResetNetworkUpdate(); this->ResetNetworkUpdate();
this->SendPingAck(this->is_master, BANDWIDTH_PING, 1024); this->SendPingAck(this->is_master, BANDWIDTH_PING, 1024);
this->SendUpdate(); this->SendServerUpdate();
this->bandwidth_ping_ms = SDL_GetTicks(); this->bandwidth_ping_ms = SDL_GetTicks();
this->ResetNetworkUpdate(); this->ResetNetworkUpdate();
this->network_connection_state = CONN_BANDWIDTH_REPLY; this->network_connection_state = CONN_BANDWIDTH_REPLY;
@ -1349,7 +1353,9 @@ void Network::Disconnect()
/* Add a stop at the end of the update */ /* Add a stop at the end of the update */
this->AddNetworkUpdate(disconnect); this->AddNetworkUpdate(disconnect);
this->SendUpdate(); this->SendServerUpdate();
if (this->network_connection_state)
this->SendPeerUpdate();
} }
bool Network::networking_started = false; bool Network::networking_started = false;

View File

@ -223,7 +223,17 @@ public:
void CloseSocket(); void CloseSocket();
bool SendUpdate(); bool SendUpdate(struct sockaddr_in *addr);
bool SendPeerUpdate()
{
return this->SendUpdate(&this->peer_addr);
}
bool SendServerUpdate()
{
return this->SendUpdate(&this->server_addr);
}
bool ReceiveUpdate(); bool ReceiveUpdate();
@ -309,7 +319,7 @@ protected:
bool ReceiveData(void *dst, int sock, size_t sz); bool ReceiveData(void *dst, int sock, size_t sz);
bool InitSocket(const char *remote_host, int port); bool InitSocket();
/* Simple wrapper around our friend recvfrom */ /* Simple wrapper around our friend recvfrom */
ssize_t ReceiveFrom(void *dst, int sock, size_t sz, ssize_t ReceiveFrom(void *dst, int sock, size_t sz,
@ -389,6 +399,7 @@ protected:
int peer_selected; int peer_selected;
int sock; int sock;
struct sockaddr_in peer_addr; struct sockaddr_in peer_addr;
struct sockaddr_in server_addr;
const char *connection_error_message; const char *connection_error_message;

View File

@ -35,7 +35,7 @@ bool Network::InitSockaddr (struct sockaddr_in *name,
return true; return true;
} }
bool Network::InitSocket(const char *remote_host, int port) bool Network::InitSocket()
{ {
/* Create the socket. */ /* Create the socket. */
this->sock = socket (PF_INET, SOCK_DGRAM, 0); this->sock = socket (PF_INET, SOCK_DGRAM, 0);
@ -47,9 +47,6 @@ bool Network::InitSocket(const char *remote_host, int port)
set_sock_opts(this->sock); set_sock_opts(this->sock);
/* Connect to the server. */
this->InitSockaddr(&this->peer_addr, remote_host, port);
return true; return true;
} }

View File

@ -29,7 +29,7 @@ bool Network::InitSockaddr (struct sockaddr_in *name,
return true; return true;
} }
bool Network::InitSocket(const char *remote_host, int port) bool Network::InitSocket()
{ {
/* Create the socket. */ /* Create the socket. */
this->sock = net_socket (PF_INET, SOCK_DGRAM, 0); this->sock = net_socket (PF_INET, SOCK_DGRAM, 0);
@ -45,9 +45,6 @@ bool Network::InitSocket(const char *remote_host, int port)
set_sock_opts(this->sock); set_sock_opts(this->sock);
/* Setup the socket address */
this->InitSockaddr(&this->peer_addr, remote_host, port);
return true; return true;
} }