Advertisement
sbaldovi

fuse.spectranet_win32_02.diff

Jun 19th, 2011
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 12.82 KB | None | 0 0
  1. Index: peripherals/nic/w5100_internals.h
  2. ===================================================================
  3. --- peripherals/nic/w5100_internals.h   (revision 4494)
  4. +++ peripherals/nic/w5100_internals.h   (working copy)
  5. @@ -29,6 +29,19 @@
  6.  #ifndef FUSE_W5100_INTERNALS_H
  7.  #define FUSE_W5100_INTERNALS_H
  8.  
  9. +#ifdef WIN32
  10. +#include <signal.h>
  11. +#endif             /* #ifdef WIN32 */
  12. +
  13. +#ifdef WIN32
  14. +#define close_socket( x ) closesocket( x )
  15. +typedef SOCKET socket_t;
  16. +#else
  17. +#define INVALID_SOCKET -1
  18. +#define close_socket( x ) close( x )
  19. +typedef int socket_t;
  20. +#endif             /* #ifdef WIN32 */
  21. +
  22.  typedef enum w5100_socket_mode {
  23.    W5100_SOCKET_MODE_CLOSED = 0x00,
  24.    W5100_SOCKET_MODE_TCP,
  25. @@ -105,7 +118,7 @@
  26.  
  27.    /* Host properties */
  28.  
  29. -  int fd;                   /* Socket file descriptor */
  30. +  socket_t fd;              /* Socket file descriptor */
  31.    int write_pending;        /* True if we're waiting to write data on this socket */
  32.  
  33.    /* Flag used to indicate that a socket has been closed since we started
  34. @@ -126,6 +139,10 @@
  35.  
  36.    nic_w5100_socket_t socket[4];
  37.  
  38. +#ifdef WIN32
  39. +  nic_w5100_socket_t dummy_socket;
  40. +#endif
  41. +
  42.    pthread_t thread;         /* Thread for doing I/O */
  43.    sig_atomic_t stop_io_thread; /* Flag to stop I/O thread */
  44.    int pipe_read;            /* Pipe for waking I/O thread */
  45. @@ -136,6 +153,9 @@
  46.  
  47.  void nic_w5100_socket_init( nic_w5100_socket_t *socket, int which );
  48.  void nic_w5100_socket_free( nic_w5100_socket_t *socket );
  49. +#ifdef WIN32
  50. +void nic_w5100_open_control_socket( nic_w5100_socket_t *socket );
  51. +#endif
  52.  
  53.  void nic_w5100_socket_reset( nic_w5100_socket_t *socket );
  54.  
  55. Index: peripherals/nic/w5100_socket.c
  56. ===================================================================
  57. --- peripherals/nic/w5100_socket.c  (revision 4494)
  58. +++ peripherals/nic/w5100_socket.c  (working copy)
  59. @@ -29,12 +29,17 @@
  60.  #include "config.h"
  61.  
  62.  #include <errno.h>
  63. -#include <netinet/in.h>
  64.  #include <pthread.h>
  65. -#include <sys/socket.h>
  66.  #include <sys/types.h>
  67.  #include <unistd.h>
  68.  
  69. +#ifdef WIN32
  70. +#include <Winsock2.h>
  71. +#else
  72. +#include <netinet/in.h>
  73. +#include <sys/socket.h>
  74. +#endif             /* #ifdef WIN32 */
  75. +
  76.  #include "fuse.h"
  77.  #include "ui/ui.h"
  78.  #include "w5100.h"
  79. @@ -51,7 +56,7 @@
  80.  nic_w5100_socket_init( nic_w5100_socket_t *socket, int which )
  81.  {
  82.    socket->id = which;
  83. -  socket->fd = -1;
  84. +  socket->fd = INVALID_SOCKET;
  85.    socket->ok_for_io = 0;
  86.    pthread_mutex_init( &socket->lock, NULL );
  87.  }
  88. @@ -105,10 +110,11 @@
  89.  void
  90.  nic_w5100_socket_free( nic_w5100_socket_t *socket )
  91.  {
  92. -  if( socket->fd != -1 ) {
  93. +  if( socket->fd != INVALID_SOCKET ) {
  94. +    printf( "nic_w5100_socket_free %d\n", socket->id );
  95.      w5100_socket_acquire_lock( socket );
  96. -    close( socket->fd );
  97. -    socket->fd = -1;
  98. +    close_socket( socket->fd );
  99. +    socket->fd = INVALID_SOCKET;
  100.      socket->ok_for_io = 0;
  101.      socket->write_pending = 0;
  102.      w5100_socket_release_lock( socket );
  103. @@ -162,7 +168,7 @@
  104.  
  105.      w5100_socket_acquire_lock( socket_obj );
  106.      socket_obj->fd = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
  107. -    if( socket_obj->fd == -1 ) {
  108. +    if( socket_obj->fd == INVALID_SOCKET ) {
  109.        printf("w5100: failed to open UDP socket for socket %d; errno = %d\n", socket_obj->id, errno);
  110.        w5100_socket_release_lock( socket_obj );
  111.        return;
  112. @@ -178,7 +184,7 @@
  113.  
  114.      w5100_socket_acquire_lock( socket_obj );
  115.      socket_obj->fd = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
  116. -    if( socket_obj->fd == -1 ) {
  117. +    if( socket_obj->fd == INVALID_SOCKET ) {
  118.        printf("w5100: failed to open TCP socket for socket %d; errno = %d\n", socket_obj->id, errno);
  119.        w5100_socket_release_lock( socket_obj );
  120.        return;
  121. @@ -218,8 +224,8 @@
  122.    if( socket->mode == W5100_SOCKET_MODE_UDP &&
  123.      socket->state == W5100_SOCKET_STATE_UDP ) {
  124.      w5100_socket_acquire_lock( socket );
  125. -    close( socket->fd );
  126. -    socket->fd = -1;
  127. +    close_socket( socket->fd );
  128. +    socket->fd = INVALID_SOCKET;
  129.      socket->ok_for_io = 0;
  130.      socket->state = W5100_SOCKET_STATE_CLOSED;
  131.      w5100_socket_release_lock( socket );
  132. @@ -334,6 +340,7 @@
  133.        printf("w5100: reading 0x%02x from unsupported register 0x%03x\n", b, reg );
  134.        break;
  135.    }
  136. +
  137.    return b;
  138.  }
  139.  
  140. @@ -414,7 +421,7 @@
  141.  {
  142.    w5100_socket_acquire_lock( socket );
  143.  
  144. -  if( socket->fd != -1 ) {
  145. +  if( socket->fd != INVALID_SOCKET ) {
  146.      socket->ok_for_io = 1;
  147.  
  148.      /* We can process a UDP read if we're in a UDP state and there are at least
  149. @@ -455,7 +462,7 @@
  150.    sa.sin_family = AF_INET;
  151.    memcpy( &sa.sin_port, socket->port, 2 );
  152.    sa.sin_addr.s_addr = htonl(INADDR_ANY);
  153. -  bytes_read = recvfrom( socket->fd, buffer + 8, bytes_free - 8, 0, (struct sockaddr*)&sa, &sa_length );
  154. +  bytes_read = recvfrom( socket->fd, (char *)buffer + 8, bytes_free - 8, 0, (struct sockaddr*)&sa, &sa_length );
  155.    printf("w5100: read 0x%03x bytes from socket %d\n", (int)bytes_read, socket->id);
  156.  
  157.    if( bytes_read != -1 ) {
  158. @@ -506,7 +513,7 @@
  159.    memcpy( &sa.sin_port, socket->dport, 2 );
  160.    memcpy( &sa.sin_addr.s_addr, socket->dip, 4 );
  161.  
  162. -  bytes_sent = sendto( socket->fd, data, length, 0, (struct sockaddr*)&sa, sizeof(sa) );
  163. +  bytes_sent = sendto( socket->fd, (const char *)data, length, 0, (struct sockaddr*)&sa, sizeof(sa) );
  164.    printf("w5100: sent 0x%03x bytes of %d to socket %d\n", (int)bytes_sent, length, socket->id);
  165.  
  166.    if( bytes_sent != -1 ) {
  167. @@ -528,7 +535,7 @@
  168.  
  169.    /* Process only if we're an open socket, and we haven't been closed and
  170.       re-opened since the select() started */
  171. -  if( socket->fd != -1 && socket->ok_for_io ) {
  172. +  if( socket->fd != INVALID_SOCKET && socket->ok_for_io ) {
  173.      if( FD_ISSET( socket->fd, &readfds ) )
  174.        w5100_socket_process_read( socket );
  175.  
  176. @@ -538,3 +545,12 @@
  177.  
  178.    w5100_socket_release_lock( socket );
  179.  }
  180. +
  181. +#ifdef WIN32
  182. +void
  183. +nic_w5100_open_control_socket( nic_w5100_socket_t *socket )
  184. +{
  185. +  socket->mode = W5100_SOCKET_MODE_UDP;
  186. +  w5100_socket_open( socket );
  187. +}
  188. +#endif
  189. \ No newline at end of file
  190. Index: peripherals/nic/w5100.c
  191. ===================================================================
  192. --- peripherals/nic/w5100.c (revision 4494)
  193. +++ peripherals/nic/w5100.c (working copy)
  194. @@ -29,12 +29,17 @@
  195.  #include "config.h"
  196.  
  197.  #include <errno.h>
  198. -#include <netinet/in.h>
  199.  #include <pthread.h>
  200. -#include <sys/socket.h>
  201.  #include <sys/types.h>
  202.  #include <unistd.h>
  203.  
  204. +#ifdef WIN32
  205. +#include <Winsock2.h>
  206. +#else
  207. +#include <netinet/in.h>
  208. +#include <sys/socket.h>
  209. +#endif             /* #ifdef WIN32 */
  210. +
  211.  #include "fuse.h"
  212.  #include "ui/ui.h"
  213.  #include "w5100.h"
  214. @@ -91,32 +96,57 @@
  215.  w5100_io_thread( void *arg )
  216.  {
  217.    nic_w5100_t *self = arg;
  218. -  int i;
  219. +  int i, retval;
  220. +  struct timeval *tvp = NULL;
  221.  
  222. +#ifdef WIN32
  223. +  struct timeval tv;
  224. +  tvp = &tv;
  225. +#endif
  226. +  
  227.    while( !self->stop_io_thread ) {
  228.      fd_set readfds, writefds;
  229. -    int max_fd = self->pipe_read;
  230. +    int max_fd = -1;
  231.  
  232.      FD_ZERO( &readfds );
  233. +    FD_ZERO( &writefds );
  234. +
  235. +#ifndef WIN32
  236. +    max_fd = self->pipe_read;
  237.      FD_SET( self->pipe_read, &readfds );
  238. +#else
  239. +    nic_w5100_socket_add_to_sets( &self->dummy_socket, &readfds, &writefds,
  240. +      &max_fd );
  241.  
  242. -    FD_ZERO( &writefds );
  243. +    tv.tv_sec = 1; /* wait 1 second max */
  244. +    tv.tv_usec = 0;
  245. +#endif
  246.  
  247.      for( i = 0; i < 4; i++ )
  248.        nic_w5100_socket_add_to_sets( &self->socket[i], &readfds, &writefds,
  249.          &max_fd );
  250.  
  251.      printf("w5100: io thread select\n");
  252. +    retval = select( max_fd + 1, &readfds, &writefds, NULL, tvp );
  253. +    if( retval < 0 ) {
  254. +#ifdef WIN32
  255. +      printf( "w5100: io thread wake from select with error %d\n",
  256. +              WSAGetLastError() );
  257. +#else
  258. +      printf( "w5100: io thread wake from select with error %d\n",
  259. +              retval );
  260. +#endif
  261. +    }
  262.  
  263. -    select( max_fd + 1, &readfds, &writefds, NULL, NULL );
  264. +    printf("w5100: io thread wake from select %d\n", retval);
  265.  
  266. -    printf("w5100: io thread wake\n");
  267. -
  268. +#ifndef WIN32
  269.      if( FD_ISSET( self->pipe_read, &readfds ) ) {
  270.        char bitbucket;
  271.        printf("w5100: discarding pipe data\n");
  272.        read( self->pipe_read, &bitbucket, 1 );
  273.      }
  274. +#endif
  275.  
  276.      for( i = 0; i < 4; i++ )
  277.        nic_w5100_socket_process_io( &self->socket[i], readfds, writefds );
  278. @@ -128,28 +158,54 @@
  279.  void
  280.  nic_w5100_wake_io_thread( nic_w5100_t *self )
  281.  {
  282. +#ifndef WIN32
  283.    const char dummy = 0;
  284.    write( self->pipe_write, &dummy, 1 );
  285. +#endif
  286.  }
  287.  
  288.  nic_w5100_t*
  289.  nic_w5100_alloc( void )
  290.  {
  291.    int error;
  292. -  int pipefd[2];
  293.    int i;
  294.  
  295. +#ifndef WIN32
  296. +  int pipefd[2];
  297. +#else
  298. +  WORD wVersionRequested;
  299. +  WSADATA wsaData;
  300. +
  301. +  wVersionRequested = MAKEWORD( 2, 2 );
  302. +  error = WSAStartup( wVersionRequested, &wsaData );
  303. +  if( error ) {
  304. +    ui_error( UI_ERROR_ERROR, "WSAStartup failed with error: %d", error );
  305. +    fuse_abort();
  306. +  }
  307. +
  308. +  if( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 2 ) {
  309. +    WSACleanup();
  310. +    ui_error( UI_ERROR_ERROR, "FUSE could not find a usable Winsock DLL" );
  311. +    fuse_abort();
  312. +  }
  313. +#endif /* ifndef WIN32 */
  314. +
  315.    nic_w5100_t *self = malloc( sizeof( *self ) );
  316.    if( !self ) {
  317.      ui_error( UI_ERROR_ERROR, "%s:%d out of memory", __FILE__, __LINE__ );
  318.      fuse_abort();
  319.    }
  320.  
  321. +#ifdef WIN32
  322. +  nic_w5100_socket_init( &self->dummy_socket, -1 );
  323. +#endif /* ifdef WIN32 */
  324. +
  325.    for( i = 0; i < 4; i++ )
  326.      nic_w5100_socket_init( &self->socket[i], i );
  327.  
  328.    w5100_reset( self );
  329.  
  330. +#ifndef WIN32
  331.    error = pipe( pipefd );
  332.    if( error ) {
  333.      ui_error( UI_ERROR_ERROR, "w5100: error %d creating pipe", error );
  334. @@ -158,6 +214,10 @@
  335.  
  336.    self->pipe_read = pipefd[0];
  337.    self->pipe_write = pipefd[1];
  338. +#else
  339. +  /* open dummy socket to allow SELECT model */
  340. +  nic_w5100_open_control_socket( &self->dummy_socket );
  341. +#endif
  342.  
  343.    self->stop_io_thread = 0;
  344.  
  345. @@ -182,7 +242,12 @@
  346.  
  347.    for( i = 0; i < 4; i++ )
  348.      nic_w5100_socket_free( &self->socket[i] );
  349. -    
  350. +
  351. +#ifdef WIN32
  352. +  nic_w5100_socket_free( &self->dummy_socket );
  353. +  WSACleanup();
  354. +#endif
  355. +
  356.    free( self );
  357.  }
  358.  
  359. Index: configure.in
  360. ===================================================================
  361. --- configure.in    (revision 4494)
  362. +++ configure.in    (working copy)
  363. @@ -110,7 +110,7 @@
  364.  AC_MSG_RESULT($win32)
  365.  if test "$win32" = yes; then
  366.    AC_CHECK_HEADER(windows.h,
  367. -                  LIBS="$LIBS -mwindows -lcomctl32 -lwinmm";
  368. +                  LIBS="$LIBS -mwindows -lcomctl32 -lwinmm -lpthread";
  369.                    AC_DEFINE([UI_WIN32], 1, [Defined if Win32 UI in use])
  370.                    AC_DEFINE([WINVER], 0x0400, [Minimal supported version of Windows is 95 or NT4])
  371.                    AC_DEFINE([_WIN32_IE], 0x400, [Internet Explorer is 4.0 or higher is required])
  372. Index: ui/win32/win32ui.c
  373. ===================================================================
  374. --- ui/win32/win32ui.c  (revision 4494)
  375. +++ ui/win32/win32ui.c  (working copy)
  376. @@ -24,6 +24,7 @@
  377.  */
  378.  
  379.  #include <config.h>
  380. +#include <fcntl.h>
  381.  #include <tchar.h>
  382.  
  383.  #include "debugger/debugger.h"
  384. @@ -277,6 +278,46 @@
  385.    return( DefWindowProc( hWnd, msg, wParam, lParam ) );
  386.  }
  387.  
  388. +void redirect_IO_to_console( void )
  389. +{
  390. +  int hConHandle;
  391. +  HANDLE std_handle;
  392. +  CONSOLE_SCREEN_BUFFER_INFO coninfo;
  393. +  FILE *fp;
  394. +
  395. +  /* allocate a console for this app */
  396. +  AllocConsole();
  397. +
  398. +  /* set the screen buffer to be big enough to let us scroll text */
  399. +  std_handle = GetStdHandle( STD_OUTPUT_HANDLE );
  400. +  if( !std_handle || std_handle == INVALID_HANDLE_VALUE ) return;
  401. +  GetConsoleScreenBufferInfo( std_handle, &coninfo );
  402. +  coninfo.dwSize.Y = 500;
  403. +  SetConsoleScreenBufferSize( std_handle, coninfo.dwSize );
  404. +
  405. +  /* redirect unbuffered STDOUT to the console */
  406. +  hConHandle = _open_osfhandle( (intptr_t)std_handle, _O_TEXT );
  407. +  fp = _fdopen( hConHandle, "w" );
  408. +  *stdout = *fp;
  409. +  setvbuf( stdout, NULL, _IONBF, 0 );
  410. +
  411. +  /* redirect unbuffered STDIN to the console */
  412. +  std_handle = GetStdHandle( STD_INPUT_HANDLE );
  413. +  if( !std_handle || std_handle == INVALID_HANDLE_VALUE ) return;
  414. +  hConHandle = _open_osfhandle( (intptr_t)std_handle, _O_TEXT );
  415. +  fp = _fdopen( hConHandle, "r" );
  416. +  *stdin = *fp;
  417. +  setvbuf( stdin, NULL, _IONBF, 0 );
  418. +
  419. +  /* redirect unbuffered STDERR to the console */
  420. +  std_handle = GetStdHandle( STD_ERROR_HANDLE );
  421. +  if( !std_handle || std_handle == INVALID_HANDLE_VALUE ) return;
  422. +  hConHandle = _open_osfhandle( (intptr_t)std_handle, _O_TEXT );
  423. +  fp = _fdopen( hConHandle, "w" );
  424. +  *stderr = *fp;
  425. +  setvbuf( stderr, NULL, _IONBF, 0 );
  426. +}
  427. +
  428.  /* this is where windows program begins */
  429.  int WINAPI
  430.  WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
  431. @@ -287,6 +328,10 @@
  432.    fuse_nCmdShow = nCmdShow;
  433.    fuse_hPrevInstance = hPrevInstance;
  434.  
  435. +#ifdef _DEBUG
  436. +  redirect_IO_to_console();
  437. +#endif
  438. +
  439.    return fuse_main(__argc, __argv);
  440.    /* FIXME: how do deal with returning wParam */
  441.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement