From 0916f0025d0f3c219f73864505875f5bb596dd37 Mon Sep 17 00:00:00 2001 From: "simon.kagstrom" Date: Mon, 16 Mar 2009 20:56:04 +0000 Subject: [PATCH] Send at most 4096 bytes so that wii networking *can* work. The Wii can now connect at least, but the connection (through the broker) still doesn't work. Well, well, might be some other problem. --- Src/Network.cpp | 82 +++++++++++++++++++++++++++++++++++++----------- Src/Network.h | 5 ++- Src/NetworkWii.h | 14 ++------- Src/main_wii.h | 12 +++++++ 4 files changed, 81 insertions(+), 32 deletions(-) diff --git a/Src/Network.cpp b/Src/Network.cpp index 501f7f6..4410f4e 100644 --- a/Src/Network.cpp +++ b/Src/Network.cpp @@ -510,28 +510,29 @@ bool Network::ReceiveUpdate(struct timeval *tv) bool Network::ReceiveUpdate(NetworkUpdate *dst, size_t total_sz, struct timeval *tv) { - Uint8 *pp = (Uint8*)dst; - NetworkUpdate *p; + Uint8 *p = (Uint8*)dst; size_t sz_left = total_sz; + bool has_stop = false; if (this->Select(this->sock, tv) == false) return false; - p = (NetworkUpdate*)pp; - size_t actual_sz; if (sz_left <= 0) return false; /* Receive the header */ - actual_sz = this->ReceiveFrom(pp, this->sock, - sz_left, NULL); - if (actual_sz < 0) - return false; + do { + size_t actual_sz = this->ReceiveFrom(p, this->sock, + 4096, NULL); + if (actual_sz < 0) + return false; - if (this->DeMarshalAllData(p) == false) - return false; - sz_left -= actual_sz; - pp = pp + actual_sz; + if (this->DeMarshalAllData((NetworkUpdate*)p, actual_sz, + &has_stop) == false) + return false; + sz_left -= actual_sz; + p = p + actual_sz; + } while (!has_stop); return true; } @@ -555,14 +556,50 @@ bool Network::SendUpdate() sz = this->GetNetworkUpdateSize(); if (sz <= 0) return false; - if (this->SendTo((void*)src, this->sock, - sz, &this->connection_addr) < 0) - return false; - this->traffic += sz; + size_t cur_sz = 0; + Uint8 *p = (Uint8*)src; + do + { + size_t size_to_send = this->FillNetworkBuffer((NetworkUpdate*)p); + ssize_t v; + + v = this->SendTo((void*)p, this->sock, + size_to_send, &this->connection_addr); + if (v < 0 || v != size_to_send) + return false; + cur_sz += size_to_send; + p += size_to_send; + } while (cur_sz < sz); + this->traffic += cur_sz; return true; } +size_t Network::FillNetworkBuffer(NetworkUpdate *cur) +{ + size_t sz = 0; + size_t cur_sz; + int cnt = 0; + + while(1) + { + cur_sz = ntohl(cur->size); + + if (sz + cur_sz >= 4096) + break; + + cnt++; + sz += cur_sz; + if (ntohs(cur->type) == STOP) + break; + cur = (NetworkUpdate*)((Uint8*)cur + cur_sz); + } + assert(sz <= 4096); + + return sz; +} + + void Network::AddNetworkUpdate(NetworkUpdate *update) { Uint8 *next = (Uint8*)this->cur_ud + update->size; @@ -691,18 +728,25 @@ bool Network::DeMarshalData(NetworkUpdate *p) return true; } -bool Network::DeMarshalAllData(NetworkUpdate *ud) +bool Network::DeMarshalAllData(NetworkUpdate *ud, size_t max_size, + bool *has_stop) { NetworkUpdate *p = ud; + int cnt = 0; + size_t sz = 0; - while (ntohs(p->type) != STOP) + while (ntohs(p->type) != STOP && + sz + ntohl(p->size) < max_size) { if (this->DeMarshalData(p) == false) return false; + sz += p->size; + cnt++; p = this->GetNext(p); } - /* The stop tag */ + /* The stop tag (maybe) */ + *has_stop = (ntohs(p->type) == STOP); return this->DeMarshalData(p); } diff --git a/Src/Network.h b/Src/Network.h index a90eea9..5582205 100644 --- a/Src/Network.h +++ b/Src/Network.h @@ -276,7 +276,8 @@ protected: bool MarshalAllData(NetworkUpdate *p); - bool DeMarshalAllData(NetworkUpdate *ud); + bool DeMarshalAllData(NetworkUpdate *ud, size_t max_size, + bool *has_stop); bool DeMarshalData(NetworkUpdate *ud); @@ -294,6 +295,8 @@ protected: bool ConnectFSM(); + size_t FillNetworkBuffer(NetworkUpdate *p); + NetworkUpdate *GetNext(NetworkUpdate *p) { return (NetworkUpdate*)((Uint8*)p + p->size); diff --git a/Src/NetworkWii.h b/Src/NetworkWii.h index 7a5620d..35b355d 100644 --- a/Src/NetworkWii.h +++ b/Src/NetworkWii.h @@ -45,7 +45,7 @@ bool Network::InitSocket(const char *remote_host, int port) if (this->sock < 0) { fprintf (stderr, "Could not init socket. Failed with %d\n", this->sock); - sleep(1); + sleep(5); return false; } int _true = false; @@ -132,15 +132,5 @@ void Network::CloseSocket() void Network::InitNetwork() { - char myIP[16]; - - /* From Snes9x-gx */ - while (net_init() == -EAGAIN); - - if (if_config(myIP, NULL, NULL, true) < 0) - { - fprintf(stderr, "\n\n\nError getting IP address via DHCP.\n"); - sleep(2); - exit(1); - } + fprintf(stderr, "\n\n"); } diff --git a/Src/main_wii.h b/Src/main_wii.h index 2c4bf5a..9d0f669 100644 --- a/Src/main_wii.h +++ b/Src/main_wii.h @@ -9,6 +9,7 @@ #include #include #include +#include #include "menu.h" @@ -24,11 +25,22 @@ int fixme_tmp_network_server = 0; extern "C" int main(int argc, char **argv) { Frodo *the_app; + char myIP[16]; timeval tv; gettimeofday(&tv, NULL); srand(tv.tv_usec); + /* From Snes9x-gx */ + while (net_init() == -EAGAIN); + + if (if_config(myIP, NULL, NULL, true) < 0) + { + fprintf(stderr, "\n\n\nError getting IP address via DHCP.\n"); + sleep(2); + exit(1); + } + printf("%s by Christian Bauer\n", VERSION_STRING); if (!init_graphics()) {