1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
|
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// (C) Gamespy Industries
//
// Commonly shared network startup code
// WARNING: Please do not use this code as a basis for
// Game Network startup code. This is only for testing
// purposes.
//
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
#include "../gsCommon.h"
#include <demo.h>
#include <string.h>
#include <revolution/so.h>
#include <revolution/ncd.h>
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
static NCDIfConfig gIfConfig;
static NCDIpConfig gIpConfig;
static OSMutex gOsMemMutex;
static MEMHeapHandle heapHandleSocket = NULL;
#define SOCKET_HEAPSIZE_DEFAULT (1024*128)
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
static void SetDefaultIfConfig( NCDIfConfig* theIfConfig )
{
(void)memset( theIfConfig, 0, sizeof( NCDIfConfig ) );
theIfConfig->selectedMedia = NCD_IF_SELECT_WIRED;
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
static void SetDefaultIpConfig( NCDIpConfig* theIpConfig )
{
(void)memset( theIpConfig, 0, sizeof( NCDIpConfig ) );
theIpConfig->useDhcp = TRUE;
theIpConfig->adjust.maxTransferUnit = 1300; // Value can be 1460 depending on network library
theIpConfig->adjust.tcpRetransTimeout = 100;
theIpConfig->adjust.dhcpRetransCount = 4;
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Memory functions used by the network library
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
static void* _Alloc(unsigned long theName, long theSize)
{
void* a_memory_p = NULL;
(void)theName;
if (0 < theSize)
{
//OSLockMutex(&gOsMemMutex);
//a_memory_p = OSAlloc((u32)theSize);
//OSUnlockMutex(&gOsMemMutex);
// 02OCT07 BED: MEM2 was initialized with MEM_HEAP_OPT_THREAD_SAFE
// so we don't need to manually lock it
a_memory_p = MEMAllocFromExpHeapEx( heapHandleSocket, (u32) theSize, 32 );
}
return a_memory_p;
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
static void _Free(unsigned long theName, void* theMemoryPointer, long theSize)
{
GSI_UNUSED(theName);
if (theMemoryPointer && 0 < theSize)
{
//OSLockMutex(&gOsMemMutex);
//OSFree(theMemoryPointer);
//OSUnlockMutex(&gOsMemMutex);
// 02OCT07 BED: MEM2 was initialized with MEM_HEAP_OPT_THREAD_SAFE
// so we don't need to manually lock it
MEMFreeToExpHeap( heapHandleSocket, theMemoryPointer);
}
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// StartNetwork
//
BOOL StartNetwork()
{
s32 rc;
OSInitMutex(&gOsMemMutex);
// Start up the network interface USB device
SetDefaultIfConfig( &gIfConfig );
OSReport( "NCDSetIfConfig() ..." );
rc = NCDSetIfConfig( &gIfConfig );
if( rc != NCD_RESULT_SUCCESS )
{
OSReport( "failed (%d)\n", rc );
return FALSE;
}
OSReport( "success\n" );
SetDefaultIpConfig( &gIpConfig );
OSReport( "NCDSetIpConfig() ..." );
rc = NCDSetIpConfig( &gIpConfig );
if( rc != NCD_RESULT_SUCCESS )
{
OSReport( "failed (%d)\n", rc );
return FALSE;
}
OSReport( "success\n" );
// Waiting for the network interface to come with the
// configuration previously set.
OSReport( "NCDIsInterfaceDecided() " );
while( NCDIsInterfaceDecided() == FALSE )
{
NCDSleep( OSMillisecondsToTicks( 10 ) );
}
OSReport( "success\n" );
// Finished bringing up network interface
// Now initializing the socket library
OSReport( "SOInit() ..." );
{
SOLibraryConfig soLibConfig;
(void)memset(&soLibConfig, 0, sizeof(soLibConfig));
soLibConfig.alloc = _Alloc;
soLibConfig.free = _Free;
rc = SOInit(&soLibConfig);
if( rc != SO_SUCCESS )
{
switch(rc)
{
case SO_EALREADY:
OSReport("failed (SO_EALREADY)\n");
break;
case SO_EINVAL:
OSReport("failed (SO_EINVAL)\n");
break;
case SO_ENOMEM:
OSReport("failed (SO_ENOMEM)\n");
break;
default:
OSReport("failed (%d)\n", rc);
};
return FALSE;
}
}
OSReport( "success\n" );
//Now Starting the sockets library
OSReport( "SOStartup() " );
rc = SOStartup();
if( rc != SO_SUCCESS )
{
OSReport( "failed (%d)\n", rc );
return FALSE;
}
OSReport( "success\n" );
// Getting the IP address of the local machine via DHCP
while( SOGetHostID() == 0 )
{
NCDSleep( OSMillisecondsToTicks( 10 ) );
}
{
SOInAddr addr;
addr.addr = (u32)SOGetHostID();
OSReport( "IP address = %s\n", SOInetNtoA( addr ) );
}
return TRUE;
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// StopNetwork
//
void StopNetwork()
{
(void)SOCleanup();
}
extern int test_main(int argc, char **argv);
void main(int argc, char **argv)
{
// init 128k memory for sockets
void* arenaLo;
void* arenaHi;
DEMOInit(NULL); // Init the OS, game pad, graphics and video.
// Initialize heap for socket allocations
// Nintendo samples say this must be in MEM2
arenaLo = OSGetMEM2ArenaLo();
arenaHi = OSGetMEM2ArenaHi();
if ((u32) arenaHi - (u32) arenaLo < SOCKET_HEAPSIZE_DEFAULT)
OSHalt("Insufficient memory in MEM2 for socket lib. Check SOCKET_HEAPSIZE_DEFAULT");
heapHandleSocket = MEMCreateExpHeapEx( arenaLo, SOCKET_HEAPSIZE_DEFAULT, MEM_HEAP_OPT_THREAD_SAFE );
if (heapHandleSocket == MEM_HEAP_INVALID_HANDLE)
OSHalt("MEMCreateExpHeapEx failed.\n");
OSSetArenaLo((u8*)arenaLo + SOCKET_HEAPSIZE_DEFAULT);
// Start the network
if (!StartNetwork())
OSHalt("Network was not started\n");
OSReport("=======================================================================\n");
OSReport("Gamespy:// Starting App\n");
test_main(argc, argv);
OSReport("=======================================================================\n");
OSReport("Gamespy:// Ending App\n");
StopNetwork();
}
|