From 1f1c7da4ef0d8400752c1b5adc131db18cbba65d Mon Sep 17 00:00:00 2001 From: "simon.kagstrom" Date: Sat, 7 Feb 2009 11:28:29 +0000 Subject: [PATCH] Unbreak Wii again --- Makefile | 3 +- Src/Display_SDL.h | 2 - Src/Network.cpp | 4 + Src/NetworkWii.h | 198 ++++++++++++++++++++++++++++++++++++++++++++++ Src/main_wii.h | 3 + 5 files changed, 207 insertions(+), 3 deletions(-) create mode 100644 Src/NetworkWii.h diff --git a/Makefile b/Makefile index a773c61..0e62673 100644 --- a/Makefile +++ b/Makefile @@ -63,7 +63,8 @@ export DEPSDIR := $(CURDIR)/$(BUILD) CFILES := gcaudio.c CPPFILES := Display.cpp main.cpp Prefs.cpp SID.cpp REU.cpp IEC.cpp 1541fs.cpp \ 1541d64.cpp 1541t64.cpp 1541job.cpp SAM.cpp C64_SC.cpp CPUC64_SC.cpp VIC_SC.cpp \ - CIA_SC.cpp CPU1541_SC.cpp menu.cpp CPU_common.cpp VirtualKeyboard.cpp + CIA_SC.cpp CPU1541_SC.cpp menu.cpp CPU_common.cpp VirtualKeyboard.cpp \ + Network.cpp sFILES := SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.S))) BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*))) diff --git a/Src/Display_SDL.h b/Src/Display_SDL.h index 5d8f028..9bbf5d3 100644 --- a/Src/Display_SDL.h +++ b/Src/Display_SDL.h @@ -207,7 +207,6 @@ void C64Display::Update(uint8 *src_pixels) SDL_Rect srcrect = {0, 0, DISPLAY_X, DISPLAY_Y}; SDL_Rect dstrect = {0, 0, FULL_DISPLAY_X, FULL_DISPLAY_Y}; Uint8 *dst_pixels = (Uint8*)sdl_screen->pixels; - const Uint16 dst_pitch = sdl_screen->pitch; /* Draw 1-1 */ for (int y = 0; y < DISPLAY_Y; y++) @@ -332,7 +331,6 @@ int C64Display::BitmapXMod(void) void C64Display::FakeKeyPress(int kc, uint8 *CIA_key_matrix, uint8 *CIA_rev_matrix) { - int shifted = kc & 0x80; // Clear matrices for (int i = 0; i < 8; i ++) { diff --git a/Src/Network.cpp b/Src/Network.cpp index e0ea412..ad10a54 100644 --- a/Src/Network.cpp +++ b/Src/Network.cpp @@ -778,4 +778,8 @@ uint8 Network::sample_buf[NETWORK_SOUND_BUF_SIZE]; int Network::sample_head; int Network::sample_tail; +#if defined(GEKKO) +#include "NetworkWii.h" +#else #include "NetworkUnix.h" +#endif diff --git a/Src/NetworkWii.h b/Src/NetworkWii.h new file mode 100644 index 0000000..cf8e589 --- /dev/null +++ b/Src/NetworkWii.h @@ -0,0 +1,198 @@ +#include +#include +#include + +static int set_sock_opts(int sock) +{ + struct timeval tv; + int d = 1; + + memset(&tv, 0, sizeof(tv)); + tv.tv_sec = 2; + net_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, + &tv, sizeof(struct timeval)); + net_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, + &tv, sizeof(struct timeval)); + net_setsockopt(sock,SOL_SOCKET,SO_REUSEADDR, &d, sizeof(int)); + + return 0; +} + +/* From glibc docs */ +static int make_socket (uint16_t port) +{ + struct sockaddr_in name; + int sock; + + /* Create the socket. */ + sock = net_socket (PF_INET, SOCK_STREAM, 0); + if (sock < 0) + { + perror ("socket"); + exit (EXIT_FAILURE); + } + + set_sock_opts(sock); + + /* Give the socket a name. */ + name.sin_family = AF_INET; + name.sin_port = htons (port); + name.sin_addr.s_addr = htonl (INADDR_ANY); + if (net_bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0) + { + perror ("bind"); + exit (1); + } + + return sock; +} + +bool init_sockaddr (struct sockaddr_in *name, + const char *hostname, uint16_t port) +{ + struct hostent *hostinfo; + + name->sin_family = AF_INET; + name->sin_port = htons (port); + hostinfo = net_gethostbyname ((char*)hostname); + if (hostinfo == NULL) + { + fprintf (stderr, "Unknown host %s.\n", hostname); + return false; + } + //name->sin_addr = *(struct in_addr *) hostinfo->h_addr; + + return true; +} + + +bool Network::StartListener(int port) +{ + Network::listen_sock = make_socket(port); + + if (Network::listen_sock < 0) + return false; + if (net_listen(Network::listen_sock, MAX_NETWORK_PEERS) < 0) + { + perror("listen"); + return false; + } + + return true; +} + +bool Network::CheckNewConnection() +{ + struct timeval tv; + struct sockaddr_in peer_name; + size_t size; + int peer_sock; + fd_set listen_fds; + Network *peer; + + /* Not initialized yet */ + if (Network::listen_sock <= 0) + return false; + + /* No more than that thanks... */ + if (Network::n_peers >= MAX_NETWORK_PEERS) + return false; + + FD_ZERO(&listen_fds); + FD_SET(Network::listen_sock, &listen_fds); + + /* If something connects, create a new client */ + memset(&tv, 0, sizeof(tv)); + int v = net_select(Network::listen_sock + 1, &listen_fds, NULL, NULL, &tv); + + if ( v < 0) + { + perror("select"); + exit(1); + } + else if ( v == 0 ) + return false; + + size = sizeof(peer_name); + peer_sock = net_accept(Network::listen_sock, (struct sockaddr*)&peer_name, &size); + if (peer_sock < 0) + { + fprintf(stderr, "Accepting peer failed\n"); + return false; + } + + /* And add the new one! */ + Network::AddPeer(new Network(peer_sock, true)); + + return true; +} + +bool Network::ConnectTo(const char *hostname, int port) +{ + /* Again from glibc docs */ + struct sockaddr_in servername; + int sock; + + /* Create the socket. */ + sock = net_socket (PF_INET, SOCK_STREAM, 0); + if (sock < 0) + { + perror ("socket (client)"); + return false; + } + + set_sock_opts(sock); + + /* Connect to the server. */ + init_sockaddr (&servername, hostname, port); + if (net_connect(sock, (struct sockaddr *) &servername, + sizeof (servername)) != 0) + { + perror ("connect (client)"); + return false; + } + + Network::AddPeer( new Network(sock, false) ); + + return true; +} + +ssize_t Network::ReceiveFrom(void *dst, int sock, size_t sz, + struct sockaddr_in *from) +{ + socklen_t from_sz = from ? sizeof(struct sockaddr_in) : 0; + + return net_recvfrom(sock, dst, sz, 0, (struct sockaddr*)from, &from_sz); +} + +ssize_t Network::SendTo(void *src, int sock, size_t sz, struct sockaddr_in *to) +{ + socklen_t to_sz = sizeof(struct sockaddr_in); + + assert(to); + return net_sendto(sock, src, sz, 0, (struct sockaddr*)to, to_sz); +} + +bool Network::Select(int sock, struct timeval *tv) +{ + fd_set fds; + int v; + + FD_ZERO(&fds); + FD_SET(sock, &fds); + + v = net_select(sock + 1, &fds, NULL, NULL, tv); + if (v < 0) + { + fprintf(stderr, "Select failed\n"); + return false; + } + + /* v is 0 if the sock is not ready */ + return v > 0; +} + +void Network::CloseSocket() +{ + net_close(this->sock); +} diff --git a/Src/main_wii.h b/Src/main_wii.h index 310dfe5..7a7cd9f 100644 --- a/Src/main_wii.h +++ b/Src/main_wii.h @@ -16,6 +16,9 @@ extern int init_graphics(void); * Create application object and start it */ +char *fixme_tmp_network_client = 0; +int fixme_tmp_network_server = 0; + extern "C" int main(int argc, char **argv) { Frodo *the_app;