diff --git a/Src/C64_SDL.h b/Src/C64_SDL.h index 2ed3f74..cb41dfe 100644 --- a/Src/C64_SDL.h +++ b/Src/C64_SDL.h @@ -234,7 +234,7 @@ void C64::network_vblank() remote->EncodeJoystickUpdate(*js); - if (remote->SendUpdate() == false) + if (remote->SendPeerUpdate() == false) { /* Disconnect or broken data */ printf("Could not send update\n"); diff --git a/Src/Network.cpp b/Src/Network.cpp index 1406ab6..c6efdca 100644 --- a/Src/Network.cpp +++ b/Src/Network.cpp @@ -92,11 +92,15 @@ Network::Network(const char *remote_host, int port) Network::networking_started = true; this->peer_selected = -1; /* Peer addresses, if it fails we are out of luck */ - if (this->InitSocket(remote_host, port) == false) - { - fprintf(stderr, "Could not init the socket\n"); - exit(1); - } + panic_if (this->InitSocket() == false, + "Could not init the socket\n"); + + /* 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->connection_error_message = "Connection OK"; } @@ -589,7 +593,7 @@ bool Network::ReceiveUpdate(NetworkUpdate *dst, size_t total_sz, return true; } -bool Network::SendUpdate() +bool Network::SendUpdate(struct sockaddr_in *addr) { NetworkUpdate *src = this->ud; NetworkUpdate *stop = InitNetworkUpdate(this->cur_ud, STOP, sizeof(NetworkUpdate)); @@ -616,7 +620,7 @@ bool Network::SendUpdate() ssize_t v; 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) return false; cur_sz += size_to_send; @@ -1028,7 +1032,7 @@ bool Network::ConnectToBroker() this->AddNetworkUpdate(ud); out = this->AppendScreenshot(pi); if (out) - out = this->SendUpdate(); + out = this->SendServerUpdate(); this->ResetNetworkUpdate(); return out; @@ -1055,7 +1059,7 @@ network_connection_error_t Network::WaitForPeerAddress() NetworkUpdatePingAck *p = (NetworkUpdatePingAck*)ud->data; /* Send ack and go back to this state again */ this->SendPingAck(p->seq, ACK, ud->size); - this->SendUpdate(); + this->SendServerUpdate(); this->ResetNetworkUpdate(); return AGAIN_ERROR; } @@ -1085,7 +1089,7 @@ bool Network::SelectPeer(uint32 id) p->server_id = id; this->AddNetworkUpdate(ud); - out = this->SendUpdate(); + out = this->SendServerUpdate(); this->ResetNetworkUpdate(); return out; @@ -1107,7 +1111,7 @@ network_connection_error_t Network::WaitForPeerList() NetworkUpdatePingAck *p = (NetworkUpdatePingAck*)ud->data; /* Send ack and go back to this state again */ this->SendPingAck(p->seq, ACK, ud->size); - this->SendUpdate(); + this->SendServerUpdate(); this->ResetNetworkUpdate(); return AGAIN_ERROR; } @@ -1175,7 +1179,7 @@ bool Network::ConnectToPeer() bool out; this->AddNetworkUpdate(ud); - out = this->SendUpdate(); + out = this->SendServerUpdate(); this->ResetNetworkUpdate(); return out; @@ -1202,7 +1206,7 @@ network_connection_error_t Network::WaitForBandWidthReply() this->ResetNetworkUpdate(); this->SendPingAck(seq, BANDWIDTH_ACK, sz); - this->SendUpdate(); + this->SendServerUpdate(); continue; } /* 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: this->ResetNetworkUpdate(); this->SendPingAck(this->is_master, BANDWIDTH_PING, 1024); - this->SendUpdate(); + this->SendServerUpdate(); this->bandwidth_ping_ms = SDL_GetTicks(); this->ResetNetworkUpdate(); this->network_connection_state = CONN_BANDWIDTH_REPLY; @@ -1349,7 +1353,9 @@ void Network::Disconnect() /* Add a stop at the end of the update */ this->AddNetworkUpdate(disconnect); - this->SendUpdate(); + this->SendServerUpdate(); + if (this->network_connection_state) + this->SendPeerUpdate(); } bool Network::networking_started = false; diff --git a/Src/Network.h b/Src/Network.h index c9da837..51e80af 100644 --- a/Src/Network.h +++ b/Src/Network.h @@ -223,7 +223,17 @@ public: 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(); @@ -309,7 +319,7 @@ protected: 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 */ ssize_t ReceiveFrom(void *dst, int sock, size_t sz, @@ -389,6 +399,7 @@ protected: int peer_selected; int sock; struct sockaddr_in peer_addr; + struct sockaddr_in server_addr; const char *connection_error_message; diff --git a/Src/NetworkUnix.h b/Src/NetworkUnix.h index bbc8380..3b6bf49 100644 --- a/Src/NetworkUnix.h +++ b/Src/NetworkUnix.h @@ -35,7 +35,7 @@ bool Network::InitSockaddr (struct sockaddr_in *name, return true; } -bool Network::InitSocket(const char *remote_host, int port) +bool Network::InitSocket() { /* Create the socket. */ 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); - /* Connect to the server. */ - this->InitSockaddr(&this->peer_addr, remote_host, port); - return true; } diff --git a/Src/NetworkWii.h b/Src/NetworkWii.h index acda53f..ae47e84 100644 --- a/Src/NetworkWii.h +++ b/Src/NetworkWii.h @@ -29,7 +29,7 @@ bool Network::InitSockaddr (struct sockaddr_in *name, return true; } -bool Network::InitSocket(const char *remote_host, int port) +bool Network::InitSocket() { /* Create the socket. */ 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); - /* Setup the socket address */ - this->InitSockaddr(&this->peer_addr, remote_host, port); - return true; }