LUA_TCP网络

luaL_Buffer   从INIT开始到RESULT为止..栈的控制权在luaL_Buffer手里..不要动栈做修改..要修改也要平衡...添了就要删.

 

 1 BOOL APIENTRY DllMain( HMODULE hModule,
 2                        DWORD  ul_reason_for_call,
 3                        LPVOID lpReserved
 4                      )
 5 {
 6     WSADATA wsaData;
 7     WORD sockVersion;
 8 
 9     switch (ul_reason_for_call)
10     {
11     case DLL_PROCESS_ATTACH:
12         sockVersion = MAKEWORD( 2, 2 );
13         if ( WSAStartup( sockVersion, &wsaData ) != 0 )
14         {
15             return FALSE;
16         }
17 
18         if ( wsaData.wVersion != sockVersion )
19         {
20             WSACleanup();
21             return FALSE;;
22         }
23         break;
24     case DLL_THREAD_ATTACH:
25         break;
26     case DLL_THREAD_DETACH:
27         break;
28     case DLL_PROCESS_DETACH:
29         WSACleanup();
30         break;
31     }
32     return TRUE;
33 }

 

   1 #include "stdafx.h"
   2 #include "LuaSocket.h"
   3 
   4 #define RWBUFFLEN        (2048)
   5 
   6 #define CON_TIMEO        (3000)
   7 #define APT_TIMEO        (6000)
   8 
   9 #define TIMEO_RCV        (0x01)
  10 #define TIMEO_SND        (0x02)
  11 
  12 #define SELECT_READ      (0x01)
  13 #define SELECT_WRIT      (0x02)
  14 #define SELECT_EXCE      (0x04)
  15 
  16 typedef struct tag_socket
  17 {
  18     SOCKET fd;
  19     int nonb;
  20 } socket_t, *socket_ptr;
  21 
  22 typedef struct tag_selectsets
  23 {
  24     FD_SET readfds;
  25     FD_SET writefds;
  26     FD_SET exceptfds;
  27 } selectsets_t, *selectsets_ptr;
  28 
  29 typedef struct tag_selectfds
  30 {
  31     selectsets_ptr pfds;
  32 } selectfds_t, *selectfds_ptr;
  33 
  34 
  35 int tcp_new( lua_State* lua )
  36 {
  37     SOCKET fd;
  38     socket_ptr psocket;
  39 
  40     if ( ( fd = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ) ) == INVALID_SOCKET )
  41     {
  42         perror( "socket" );
  43         return 0;
  44     }
  45 
  46     psocket = ( socket_ptr )lua_newuserdata( lua, sizeof( socket_t ) );
  47     psocket->fd = fd;
  48     psocket->nonb = 0;
  49 
  50     luaL_getmetatable( lua, "luasocket.metatable" );
  51     lua_setmetatable( lua, -2 );
  52 
  53     return 1;
  54 }
  55 
  56 int socket_equal( lua_State* lua )
  57 {
  58     socket_ptr psocket1, psocket2;
  59 
  60     psocket1 = ( socket_ptr )lua_touserdata( lua, 1 );
  61     psocket2 = ( socket_ptr )lua_touserdata( lua, 2 );
  62 
  63     if ( psocket1->fd == psocket2->fd )
  64     {
  65         lua_pushboolean( lua, 1 );
  66         return 1;
  67     }
  68 
  69     lua_pushboolean( lua, 0 );
  70     return 1;
  71 }
  72 
  73 int table_readonly( lua_State* lua )
  74 {
  75     return 0;
  76 }
  77 
  78 int socket_close_gc( lua_State* lua )
  79 {
  80     socket_ptr psocket;
  81 
  82     psocket = ( socket_ptr )lua_touserdata( lua, 1 );
  83     if ( psocket->fd != INVALID_SOCKET )
  84     {
  85         closesocket( psocket->fd );
  86     }
  87 
  88     return 0;
  89 }
  90 
  91 int socket_close( lua_State* lua )
  92 {
  93     socket_ptr psocket;
  94 
  95     psocket = ( socket_ptr )lua_touserdata( lua, 1 );
  96     if ( psocket->fd != INVALID_SOCKET )
  97     {
  98         closesocket( psocket->fd );
  99         psocket->fd = INVALID_SOCKET;
 100     }
 101 
 102     return 0;
 103 }
 104 
 105 int socket_settimeo( lua_State* lua )
 106 {
 107     socket_ptr psocket;
 108     int msec, type;
 109 
 110     if ( lua_gettop( lua ) < 2 || lua_type( lua, 2 ) != LUA_TNUMBER )
 111     {
 112         goto err_exit;
 113     }
 114 
 115     psocket = ( socket_ptr )lua_touserdata( lua, 1 );
 116     msec = ( int )lua_tointeger( lua, 2 );
 117     if ( psocket->fd == INVALID_SOCKET || msec <= 0 )
 118     {
 119         goto err_exit;
 120     }
 121 
 122     type = TIMEO_RCV;
 123     if ( lua_gettop( lua ) > 2 && lua_type( lua, 3 ) == LUA_TNUMBER )
 124     {
 125         type = ( int )lua_tointeger( lua, 3 );
 126     }
 127 
 128     if ( type & TIMEO_RCV )
 129     {
 130         if ( setsockopt( psocket->fd, SOL_SOCKET, SO_RCVTIMEO, ( const char* )&msec, sizeof( int ) ) != 0 )
 131         {
 132             perror( "setsockopt\t<SO_RCVTIMEO>" );
 133             goto err_exit;
 134         }
 135     }
 136 
 137     if ( type & TIMEO_SND )
 138     {
 139         if ( setsockopt( psocket->fd, SOL_SOCKET, SO_SNDTIMEO, ( const char* )&msec, sizeof( int ) ) != 0 )
 140         {
 141             perror( "setsockopt\t<SO_SNDTIMEO>" );
 142             goto err_exit;
 143         }
 144     }
 145 
 146     lua_pushboolean( lua, 1 );
 147     return 1;
 148 err_exit:
 149     lua_pushboolean( lua, 0 );
 150     return 1;
 151 }
 152 
 153 int socket_setnonb( lua_State* lua )
 154 {
 155     socket_ptr psocket;
 156     unsigned long nonb;
 157 
 158     psocket = ( socket_ptr )lua_touserdata( lua, 1 );
 159     if ( psocket->fd == INVALID_SOCKET )
 160     {
 161         goto err_exit;
 162     }
 163 
 164     nonb = 1L;
 165     if ( lua_gettop( lua ) > 1 && lua_type( lua, 2 ) == LUA_TBOOLEAN )
 166     {
 167         nonb = ( lua_toboolean( lua, 2 ) == 0 ? 0L : 1L );
 168     }
 169 
 170     if ( psocket->nonb != ( int )nonb )
 171     {
 172         if ( ioctlsocket( psocket->fd, FIONBIO, &nonb ) != 0 )
 173         {
 174             perror( "ioctlsocket\t<FIONBIO>" );
 175             goto err_exit;
 176         }
 177 
 178         psocket->nonb = ( int )nonb;
 179     }
 180 
 181     lua_pushboolean( lua, 1 );
 182     return 1;
 183 err_exit:
 184     lua_pushboolean( lua, 0 );
 185     return 1;
 186 }
 187 
 188 int socket_setlinger( lua_State* lua )
 189 {
 190     socket_ptr psocket;
 191     struct linger lg;
 192 
 193     if ( lua_gettop( lua ) < 2 || lua_type( lua, 2 ) != LUA_TBOOLEAN )
 194     {
 195         goto err_exit;
 196     }
 197 
 198     lg.l_onoff = ( lua_toboolean( lua, 2 ) == 0 ? 0 : 1 );
 199     if ( lg.l_onoff == 1 )
 200     {
 201         if ( lua_gettop( lua ) < 3 || lua_type( lua, 3 ) != LUA_TNUMBER )
 202         {
 203             goto err_exit;
 204         }
 205 
 206         lg.l_linger = ( unsigned short )lua_tointeger( lua, 3 );
 207     }
 208 
 209     psocket = ( socket_ptr )lua_touserdata( lua, 1 );
 210     if ( setsockopt( psocket->fd, SOL_SOCKET, SO_LINGER, ( const char* )&lg, sizeof( struct linger ) ) != 0 )
 211     {
 212         perror( "setsockopt\t<SO_LINGER>" );
 213         goto err_exit;
 214     }
 215 
 216     lua_pushboolean( lua, 1 );
 217     return 1;
 218 err_exit:
 219     lua_pushboolean( lua, 0 );
 220     return 1;
 221 }
 222 
 223 int tcp_setnodelay( lua_State* lua )
 224 {
 225     socket_ptr psocket;
 226     int nodelay;
 227 
 228     psocket = ( socket_ptr )lua_touserdata( lua, 1 );
 229     if ( psocket->fd == INVALID_SOCKET )
 230     {
 231         goto err_exit;
 232     }
 233 
 234     nodelay = 1;
 235     if ( lua_gettop( lua ) > 1 && lua_type( lua, 2 ) == LUA_TBOOLEAN )
 236     {
 237         nodelay = ( lua_toboolean( lua, 2 ) == 0 ? 0 : 1 );
 238     }
 239 
 240     if ( setsockopt( psocket->fd, IPPROTO_TCP, TCP_NODELAY, ( const char* )&nodelay, sizeof( int ) ) != 0 )
 241     {
 242         perror( "setsockopt\t<TCP_NODELAY>" );
 243         goto err_exit;
 244     }
 245 
 246     lua_pushboolean( lua, 1 );
 247     return 1;
 248 err_exit:
 249     lua_pushboolean( lua, 0 );
 250     return 1;
 251 }
 252 
 253 
 254 int socket_peerip( lua_State* lua )
 255 {
 256     socket_ptr psocket;
 257     int plen;
 258     struct sockaddr ipname;
 259 
 260     psocket = ( socket_ptr )lua_touserdata( lua, 1 );
 261     if ( psocket->fd == INVALID_SOCKET )
 262     {
 263         return 0;
 264     }
 265 
 266     plen = sizeof( struct sockaddr );
 267     memset( &ipname, 0, sizeof( struct sockaddr ) );
 268     if ( getpeername( psocket->fd, &ipname, &plen ) !=  0 )
 269     {
 270         perror( "getpeername" );
 271         return 0;
 272     }
 273 
 274     lua_pushfstring( lua, "%d.%d.%d.%d", \
 275                      ( unsigned char )ipname.sa_data[2], \
 276                      ( unsigned char )ipname.sa_data[3], \
 277                      ( unsigned char )ipname.sa_data[4], \
 278                      ( unsigned char )ipname.sa_data[5] );
 279     lua_pushinteger( lua, ( int )ntohs( *( ( unsigned short* )( ipname.sa_data ) ) ) );
 280     return 2;
 281 }
 282 
 283 int socket_localip( lua_State* lua )
 284 {
 285     socket_ptr psocket;
 286     int plen;
 287     struct sockaddr ipname;
 288 
 289     psocket = ( socket_ptr )lua_touserdata( lua, 1 );
 290     if ( psocket->fd == INVALID_SOCKET )
 291     {
 292         return 0;
 293     }
 294 
 295     plen = sizeof( struct sockaddr );
 296     memset( &ipname, 0, sizeof( struct sockaddr ) );
 297     if ( getsockname( psocket->fd, &ipname, &plen ) !=  0 )
 298     {
 299         perror( "getsockname" );
 300         return 0;
 301     }
 302 
 303     lua_pushfstring( lua, "%d.%d.%d.%d", \
 304                      ( unsigned char )ipname.sa_data[2], \
 305                      ( unsigned char )ipname.sa_data[3], \
 306                      ( unsigned char )ipname.sa_data[4], \
 307                      ( unsigned char )ipname.sa_data[5] );
 308     lua_pushinteger( lua, ( int )ntohs( *( ( unsigned short* )( ipname.sa_data ) ) ) );
 309     return 2;
 310 }
 311 
 312 int tcp_listen( lua_State* lua )
 313 {
 314     int len;
 315     unsigned short port;
 316     int backlog;
 317     int on;
 318     struct sockaddr_in sin;
 319     socket_ptr psocket;
 320 
 321     len = lua_gettop( lua );
 322     if ( len < 2 )
 323     {
 324         goto err_exit;
 325     }
 326 
 327     psocket = ( socket_ptr )lua_touserdata( lua, 1 );
 328     if ( psocket->fd == INVALID_SOCKET )
 329     {
 330         goto err_exit;
 331     }
 332 
 333     if ( len == 2 )
 334     {
 335         if ( lua_type( lua, 2 ) != LUA_TNUMBER )
 336         {
 337             goto err_exit;
 338         }
 339 
 340         port = ( unsigned short )lua_tointeger( lua, 2 );
 341         backlog = 64;
 342     }
 343     else
 344     {
 345         if ( lua_type( lua, 2 ) != LUA_TNUMBER || lua_type( lua, 3 ) != LUA_TNUMBER )
 346         {
 347             goto err_exit;
 348         }
 349 
 350         if ( ( backlog = lua_tointeger( lua, 3 ) ) < 1 )
 351         {
 352             goto err_exit;
 353         }
 354 
 355         port = ( unsigned short )lua_tointeger( lua, 2 );
 356     }
 357 
 358 
 359     on = 1;
 360     if ( setsockopt( psocket->fd, SOL_SOCKET, SO_REUSEADDR, ( const char* )&on, sizeof( int ) ) != 0 )
 361     {
 362         perror( "setsockopt\t<SO_REUSEADDR>" );
 363         goto err_exit;
 364     }
 365 
 366     memset( &sin, 0, sizeof( struct sockaddr_in ) );
 367     sin.sin_family = AF_INET;
 368     sin.sin_addr.s_addr = htonl( INADDR_ANY );
 369     sin.sin_port = htons( port );
 370 
 371     if ( bind( psocket->fd, ( const struct sockaddr* )&sin, sizeof( sin ) ) != 0 )
 372     {
 373         perror( "bind" );
 374         goto err_exit;
 375     }
 376 
 377     if ( listen( psocket->fd, backlog ) != 0 )
 378     {
 379         perror( "listen" );
 380         goto err_exit;
 381     }
 382 
 383     lua_pushboolean( lua, 1 );
 384     return 1;
 385 err_exit:
 386     lua_pushboolean( lua, 0 );
 387     return 1;
 388 }
 389 
 390 int tcp_accept( lua_State* lua )
 391 {
 392     SOCKET fd;
 393     socket_ptr psocket;
 394     struct sockaddr_in sin;
 395     int clilen;
 396     struct timeval tv;
 397     unsigned long nonb;
 398     fd_set fdset;
 399     int msec;
 400     int sign;
 401 
 402     psocket = ( socket_ptr )lua_touserdata( lua, 1 );
 403     if ( psocket->fd == INVALID_SOCKET )
 404     {
 405         return 0;
 406     }
 407 
 408     msec = APT_TIMEO;
 409     if ( lua_gettop( lua ) > 1 && lua_type( lua, 2 ) == LUA_TNUMBER )
 410     {
 411         msec = lua_tointeger( lua, 2 );
 412         if ( msec < 3 )
 413         {
 414             msec = APT_TIMEO;
 415         }
 416     }
 417 
 418     sign = 0;
 419     if ( psocket->nonb == 0 )
 420     {
 421         nonb = 1L;
 422         if ( ioctlsocket( psocket->fd, FIONBIO, &nonb ) != 0 )
 423         {
 424             perror( "ioctlsocket\t<FIONBIO>" );
 425             return 0;
 426         }
 427 
 428         psocket->nonb = 1;
 429         sign = 1;
 430     }
 431 
 432     FD_ZERO( &fdset );
 433     FD_SET( psocket->fd, &fdset );
 434 
 435     tv.tv_sec = msec / 1000;
 436     tv.tv_usec = ( msec % 1000 ) * 1000;
 437     if ( select( 0, &fdset, 0, 0, &tv ) != 1 )
 438     {
 439         goto err_exit;
 440     }
 441 
 442     clilen = sizeof( sin );
 443     if ( ( fd = accept( psocket->fd, ( struct sockaddr* ) &sin, &clilen ) ) == INVALID_SOCKET )
 444     {
 445         perror( "accept" );
 446         goto err_exit;
 447     }
 448 
 449     psocket = ( socket_ptr )lua_newuserdata( lua, sizeof( socket_t ) );
 450     psocket->fd = fd;
 451 
 452     luaL_getmetatable( lua, "luasocket.metatable" );
 453     lua_setmetatable( lua, -2 );
 454 
 455     return 1;
 456 err_exit:
 457     if ( sign == 1 )
 458     {
 459         nonb = 0L;
 460         if ( ioctlsocket( psocket->fd, FIONBIO, &nonb ) == 0 )
 461         {
 462             psocket->nonb = 0;
 463         }
 464     }
 465 
 466     return 0;
 467 }
 468 
 469 int tcp_connect( lua_State* lua )
 470 {
 471     const char* host;
 472     unsigned short port;
 473     int msec;
 474     SOCKET fd;
 475     struct hostent* h;
 476     struct sockaddr_in sin;
 477     struct timeval tv;
 478     unsigned long nonb;
 479     fd_set fdset;
 480     socket_ptr psocket;
 481 
 482     if ( lua_gettop( lua ) < 2 )
 483     {
 484         return 0;
 485     }
 486 
 487     if ( lua_type( lua, 1 ) != LUA_TSTRING || ( host = lua_tostring( lua, 1 ) ) == NULL )
 488     {
 489         return 0;
 490     }
 491 
 492     if ( ( h = gethostbyname( host ) ) == NULL || h->h_addrtype != AF_INET )
 493     {
 494         return 0;
 495     }
 496 
 497     memset( &sin, 0, sizeof( sin ) );
 498     sin.sin_family = AF_INET;
 499     memcpy( &sin.sin_addr.s_addr, h->h_addr_list[0], h->h_length );
 500 
 501     if ( lua_type( lua, 2 ) != LUA_TNUMBER || ( port = ( unsigned short )lua_tointeger( lua, 2 ) ) == 0 )
 502     {
 503         return 0;
 504     }
 505 
 506     sin.sin_port = htons( port );
 507 
 508     msec = CON_TIMEO;
 509     if ( lua_gettop( lua ) > 2 && lua_type( lua, 3 ) == LUA_TNUMBER )
 510     {
 511         msec = lua_tointeger( lua, 3 );
 512         if ( msec < 3 )
 513         {
 514             msec = CON_TIMEO;
 515         }
 516     }
 517 
 518     if ( ( fd = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ) ) ==  INVALID_SOCKET )
 519     {
 520         perror( "socket" );
 521         return 0;
 522     }
 523 
 524     nonb = 1L;
 525     if ( ioctlsocket( fd, FIONBIO, &nonb ) != 0 )
 526     {
 527         closesocket( fd );
 528         return 0;
 529     }
 530 
 531     if ( connect( fd, ( const struct sockaddr* ) &sin, sizeof( sin ) ) != 0 )
 532     {
 533         FD_ZERO( &fdset );
 534         FD_SET( fd, &fdset );
 535 
 536         tv.tv_sec = msec / 1000;
 537         tv.tv_usec = ( msec % 1000 ) * 1000;
 538         if ( select( 0, 0, &fdset, 0, &tv ) != 1 )
 539         {
 540             closesocket( fd );
 541             return 0;
 542         }
 543     }
 544 
 545     nonb = 0L;
 546     if ( ioctlsocket( fd, FIONBIO, &nonb ) != 0 )
 547     {
 548         closesocket( fd );
 549         return 0;
 550     }
 551 
 552     psocket = ( socket_ptr )lua_newuserdata( lua, sizeof( socket_t ) );
 553     psocket->fd = fd;
 554     psocket->nonb = 0;
 555 
 556     luaL_getmetatable( lua, "luasocket.metatable" );
 557     lua_setmetatable( lua, -2 );
 558 
 559     return 1;
 560 }
 561 
 562 
 563 int tcp_read( lua_State* lua )
 564 {
 565     socket_ptr psocket;
 566     luaL_Buffer b;
 567     unsigned char chunk[RWBUFFLEN];
 568     int nread;
 569     char* ptr;
 570 
 571     psocket = ( socket_ptr )lua_touserdata( lua, 1 );
 572     if ( psocket->fd == INVALID_SOCKET )
 573     {
 574         return 0;
 575     }
 576 
 577     ptr = ( char* )chunk;
 578     luaL_buffinit( lua, &b );
 579     while ( 1 )
 580     {
 581         nread = recv( psocket->fd, ptr, ( int )RWBUFFLEN, 0 );
 582         if ( nread < 0 )
 583         {
 584             if ( WSAGetLastError() == WSAEINTR )
 585             {
 586                 continue;
 587             }
 588             else if ( WSAGetLastError() == WSAEWOULDBLOCK )
 589             {
 590                 break;
 591             }
 592             else
 593             {
 594                 /*luaL_pushresult( &b );
 595                 lua_settop( lua, -2 );
 596                 return 0;*/
 597                 break;
 598             }
 599         }
 600         else if ( nread == 0 )
 601         {
 602             break;
 603         }
 604 
 605         luaL_addlstring( &b, ptr, nread );
 606     }
 607 
 608     luaL_pushresult( &b );
 609     return 1;
 610 }
 611 
 612 int tcp_readn( lua_State* lua )
 613 {
 614     socket_ptr psocket;
 615     luaL_Buffer b;
 616     unsigned char chunk[RWBUFFLEN];
 617     int nleft, nread;
 618     char* ptr;
 619 
 620     if ( lua_gettop( lua ) < 2  || lua_type( lua, 2 ) != LUA_TNUMBER )
 621     {
 622         return 0;
 623     }
 624 
 625     psocket = ( socket_ptr )lua_touserdata( lua, 1 );
 626     if ( psocket->fd == INVALID_SOCKET )
 627     {
 628         return 0;
 629     }
 630 
 631     nleft = ( int )lua_tointeger( lua, 2 );
 632 
 633     ptr = ( char* )chunk;
 634     luaL_buffinit( lua, &b );
 635     while ( nleft > 0 )
 636     {
 637         if ( nleft >= RWBUFFLEN )
 638         {
 639             nread = recv( psocket->fd, ptr, ( int )RWBUFFLEN, 0 );
 640         }
 641         else
 642         {
 643             nread = recv( psocket->fd, ptr, nleft, 0 );
 644         }
 645 
 646         if ( nread < 0 )
 647         {
 648             if ( WSAGetLastError() == WSAEINTR )
 649             {
 650                 continue;
 651             }
 652             else if ( WSAGetLastError() == WSAEWOULDBLOCK )
 653             {
 654                 continue;
 655             }
 656             else
 657             {
 658                 luaL_pushresult( &b );
 659                 lua_settop( lua, -2 );
 660                 return 0;
 661             }
 662         }
 663         else if ( nread == 0 )
 664         {
 665             break;
 666         }
 667 
 668         luaL_addlstring( &b, ptr, nread );
 669         nleft -= nread;
 670     }
 671 
 672     luaL_pushresult( &b );
 673     return 1;
 674 }
 675 
 676 int tcp_write( lua_State* lua )
 677 {
 678     socket_ptr psocket;
 679     size_t nleft;
 680     const char* ptr;
 681     int nwritten, nwrittens;
 682 
 683     if ( lua_gettop( lua ) < 2 || lua_type( lua, 2 ) != LUA_TSTRING )
 684     {
 685         return 0;
 686     }
 687 
 688     psocket = ( socket_ptr )lua_touserdata( lua, 1 );
 689     if ( psocket->fd == INVALID_SOCKET )
 690     {
 691         return 0;
 692     }
 693 
 694     ptr = lua_tolstring( lua, 2, &nleft );
 695     if ( ptr == NULL )
 696     {
 697         return 0;
 698     }
 699 
 700     nwrittens = 0;
 701     while ( ( int )nleft > 0 )
 702     {
 703         nwritten = send( psocket->fd, ptr, ( int )nleft, 0 );
 704         if ( nwritten < 0 )
 705         {
 706             if ( WSAGetLastError() == WSAEINTR )
 707             {
 708                 continue;
 709             }
 710             else if ( WSAGetLastError() == WSAEWOULDBLOCK )
 711             {
 712                 break;
 713             }
 714 
 715             return 0;
 716         }
 717         else if ( nwritten == 0 )
 718         {
 719             break;
 720         }
 721 
 722         nleft -= nwritten;
 723         ptr += nwritten;
 724         nwrittens += nwritten;
 725     }
 726 
 727     lua_pushinteger( lua, nwrittens );
 728     return 1;
 729 }
 730 
 731 int tcp_writen( lua_State* lua )
 732 {
 733     socket_ptr psocket;
 734     size_t nleft;
 735     const char* ptr;
 736     int nwritten, nwrittens;
 737 
 738     if ( lua_gettop( lua ) < 2 || lua_type( lua, 2 ) != LUA_TSTRING )
 739     {
 740         return 0;
 741     }
 742 
 743     psocket = ( socket_ptr )lua_touserdata( lua, 1 );
 744     if ( psocket->fd == INVALID_SOCKET )
 745     {
 746         return 0;
 747     }
 748 
 749     ptr = lua_tolstring( lua, 2, &nleft );
 750     if ( ptr == NULL )
 751     {
 752         return 0;
 753     }
 754 
 755     nwrittens = 0;
 756     while ( ( int )nleft > 0 )
 757     {
 758         nwritten = send( psocket->fd, ptr, ( int )nleft, 0 );
 759         if ( nwritten < 0 )
 760         {
 761             if ( WSAGetLastError() == WSAEINTR )
 762             {
 763                 continue;
 764             }
 765             else if ( WSAGetLastError() == WSAEWOULDBLOCK )
 766             {
 767                 continue;
 768             }
 769 
 770             return 0;
 771         }
 772         else if ( nwritten == 0 )
 773         {
 774             break;
 775         }
 776 
 777         nleft -= nwritten;
 778         ptr += nwritten;
 779         nwrittens += nwritten;
 780     }
 781 
 782     lua_pushinteger( lua, nwrittens );
 783     return 1;
 784 }
 785 
 786 int select_new( lua_State* lua )
 787 {
 788     selectfds_ptr psel;
 789 
 790     psel = ( selectfds_ptr )lua_newuserdata( lua, sizeof( selectfds_t ) );
 791 
 792     psel->pfds = ( selectsets_ptr )malloc( sizeof( selectsets_t ) );
 793     if ( psel->pfds == NULL )
 794     {
 795         return 0;
 796     }
 797 
 798     FD_ZERO( &psel->pfds->readfds );
 799     FD_ZERO( &psel->pfds->writefds );
 800     FD_ZERO( &psel->pfds->exceptfds );
 801 
 802     luaL_getmetatable( lua, "luasocket.select.metatable" );
 803     lua_setmetatable( lua, -2 );
 804 
 805     return 1;
 806 }
 807 
 808 int select_destroy( lua_State* lua )
 809 {
 810     selectfds_ptr psel;
 811 
 812     psel = ( selectfds_ptr )lua_touserdata( lua, 1 );
 813     if ( psel->pfds != NULL )
 814     {
 815         free( psel->pfds );
 816     }
 817 
 818     return 0;
 819 }
 820 
 821 int select_clear( lua_State* lua )
 822 {
 823     selectfds_ptr psel;
 824     int type;
 825 
 826     psel = ( selectfds_ptr )lua_touserdata( lua, 1 );
 827 
 828     type = SELECT_READ | SELECT_WRIT | SELECT_EXCE;
 829     if ( lua_gettop( lua ) > 1 && lua_type( lua, 2 ) == LUA_TNUMBER )
 830     {
 831         type = ( int )lua_tointeger( lua, 2 );
 832     }
 833 
 834     if ( type & SELECT_READ )
 835     {
 836         FD_ZERO( &psel->pfds->readfds );
 837     }
 838 
 839     if ( type & SELECT_WRIT )
 840     {
 841         FD_ZERO( &psel->pfds->writefds );
 842     }
 843 
 844     if ( type & SELECT_EXCE )
 845     {
 846         FD_ZERO( &psel->pfds->exceptfds );
 847     }
 848 
 849     lua_pushboolean( lua, 1 );
 850     return 1;
 851 }
 852 
 853 int select_addfd( lua_State* lua )
 854 {
 855     socket_ptr psocket;
 856     selectfds_ptr psel;
 857     int type;
 858 
 859     if ( lua_gettop( lua ) < 2 || lua_type( lua, 2 ) != LUA_TUSERDATA )
 860     {
 861         lua_pushboolean( lua, 0 );
 862         return 1;
 863     }
 864 
 865     psel = ( selectfds_ptr )lua_touserdata( lua, 1 );
 866     psocket = ( socket_ptr )lua_touserdata( lua, 2 );
 867 
 868     type = SELECT_READ | SELECT_WRIT | SELECT_EXCE;
 869     if ( lua_gettop( lua ) > 2 && lua_type( lua, 3 ) == LUA_TNUMBER )
 870     {
 871         type = ( int )lua_tointeger( lua, 3 );
 872     }
 873 
 874     if ( type & SELECT_READ )
 875     {
 876         FD_SET( psocket->fd, &psel->pfds->readfds );
 877     }
 878 
 879     if ( type & SELECT_WRIT )
 880     {
 881         FD_SET( psocket->fd, &psel->pfds->writefds );
 882     }
 883 
 884     if ( type & SELECT_EXCE )
 885     {
 886         FD_SET( psocket->fd, &psel->pfds->exceptfds );
 887     }
 888 
 889     lua_pushboolean( lua, 1 );
 890     return 1;
 891 }
 892 
 893 int select_delfd( lua_State* lua )
 894 {
 895     socket_ptr psocket;
 896     selectfds_ptr psel;
 897     int type;
 898 
 899     if ( lua_gettop( lua ) < 2 || lua_type( lua, 2 ) != LUA_TUSERDATA )
 900     {
 901         lua_pushboolean( lua, 0 );
 902         return 1;
 903     }
 904 
 905     psel = ( selectfds_ptr )lua_touserdata( lua, 1 );
 906     psocket = ( socket_ptr )lua_touserdata( lua, 2 );
 907 
 908     type = SELECT_READ | SELECT_WRIT | SELECT_EXCE;
 909     if ( lua_gettop( lua ) > 2 && lua_type( lua, 3 ) == LUA_TNUMBER )
 910     {
 911         type = ( int )lua_tointeger( lua, 3 );
 912     }
 913 
 914     if ( type & SELECT_READ )
 915     {
 916         FD_CLR( psocket->fd, &psel->pfds->readfds );
 917     }
 918 
 919     if ( type & SELECT_WRIT )
 920     {
 921         FD_CLR( psocket->fd, &psel->pfds->writefds );
 922     }
 923 
 924     if ( type & SELECT_EXCE )
 925     {
 926         FD_CLR( psocket->fd, &psel->pfds->exceptfds );
 927     }
 928 
 929     lua_pushboolean( lua, 1 );
 930     return 1;
 931 }
 932 
 933 int select_isset( lua_State* lua )
 934 {
 935     selectfds_ptr psel;
 936     socket_ptr psocket;
 937     int type;
 938 
 939     if ( lua_gettop( lua ) < 2 || lua_type( lua, 2 ) != LUA_TUSERDATA )
 940     {
 941         lua_pushboolean( lua, 0 );
 942         return 1;
 943     }
 944 
 945     psel = ( selectfds_ptr )lua_touserdata( lua, 1 );
 946     psocket = ( socket_ptr )lua_touserdata( lua, 2 );
 947 
 948     type = SELECT_READ;
 949     if ( lua_gettop( lua ) > 2 && lua_type( lua, 3 ) == LUA_TNUMBER )
 950     {
 951         type = ( int )lua_tointeger( lua, 3 );
 952     }
 953 
 954     if ( type & SELECT_READ )
 955     {
 956         if ( FD_ISSET( psocket->fd, &psel->pfds->readfds ) )
 957         {
 958             lua_pushboolean( lua, 1 );
 959             return 1;
 960         }
 961     }
 962     else if ( type & SELECT_WRIT )
 963     {
 964         if ( FD_ISSET( psocket->fd, &psel->pfds->writefds ) )
 965         {
 966             lua_pushboolean( lua, 1 );
 967             return 1;
 968         }
 969     }
 970     else if ( type & SELECT_EXCE )
 971     {
 972         if ( FD_ISSET( psocket->fd, &psel->pfds->exceptfds ) )
 973         {
 974             lua_pushboolean( lua, 1 );
 975             return 1;
 976         }
 977     }
 978 
 979     lua_pushboolean( lua, 0 );
 980     return 1;
 981 }
 982 
 983 int select_select( lua_State* lua )
 984 {
 985     selectfds_ptr psel;
 986     struct timeval tv;
 987     int msec;
 988     int ret;
 989 
 990     psel = ( selectfds_ptr )lua_touserdata( lua, 1 );
 991 
 992     msec = 0;
 993     if ( lua_gettop( lua ) > 1 && lua_type( lua, 2 ) == LUA_TNUMBER )
 994     {
 995         msec = ( int )lua_tointeger( lua, 2 );
 996     }
 997 
 998     if ( msec > 0 )
 999     {
1000         tv.tv_sec = msec / 1000;
1001         tv.tv_usec = ( msec % 1000 ) * 1000;
1002         ret = select( 0, &psel->pfds->readfds, &psel->pfds->writefds, &psel->pfds->exceptfds, &tv );
1003     }
1004     else
1005     {
1006         ret = select( 0, &psel->pfds->readfds, &psel->pfds->writefds, &psel->pfds->exceptfds, NULL );
1007     }
1008 
1009     lua_pushinteger( lua, ret );
1010     return 1;
1011 }
1012 
1013 static luaL_Reg _mylibs[] =
1014 {
1015     {"tcp", tcp_new},
1016     {"connect", tcp_connect},
1017     {"newsel", select_new},
1018     {NULL, NULL}
1019 };
1020 
1021 int luaopen_LuaSocket( lua_State* lua )
1022 {
1023     /****************************************************/
1024     luaL_newmetatable( lua, "luasocket.metatable" );
1025 
1026     lua_pushstring( lua, "__index" );
1027     lua_pushvalue( lua, -2 );
1028     lua_rawset( lua, -3 );
1029 
1030     lua_pushstring( lua, "__newindex" );
1031     lua_pushcfunction( lua, table_readonly );
1032     lua_rawset( lua, -3 );
1033 
1034     lua_pushstring( lua, "__gc" );
1035     lua_pushcfunction( lua, socket_close_gc );
1036     lua_rawset( lua, -3 );
1037 
1038     lua_pushstring( lua, "__eq" );
1039     lua_pushcfunction( lua, socket_equal );
1040     lua_rawset( lua, -3 );
1041 
1042     lua_pushstring( lua, "close" );
1043     lua_pushcfunction( lua, socket_close );
1044     lua_rawset( lua, -3 );
1045 
1046     lua_pushstring( lua, "timeout" );
1047     lua_pushcfunction( lua, socket_settimeo );
1048     lua_rawset( lua, -3 );
1049 
1050     lua_pushstring( lua, "nonb" );
1051     lua_pushcfunction( lua, socket_setnonb );
1052     lua_rawset( lua, -3 );
1053 
1054     lua_pushstring( lua, "linger" );
1055     lua_pushcfunction( lua, socket_setlinger );
1056     lua_rawset( lua, -3 );
1057 
1058     lua_pushstring( lua, "nodelay" );
1059     lua_pushcfunction( lua, tcp_setnodelay );
1060     lua_rawset( lua, -3 );
1061 
1062     lua_pushstring( lua, "peerip" );
1063     lua_pushcfunction( lua, socket_peerip );
1064     lua_rawset( lua, -3 );
1065 
1066     lua_pushstring( lua, "localip" );
1067     lua_pushcfunction( lua, socket_localip );
1068     lua_rawset( lua, -3 );
1069 
1070     lua_pushstring( lua, "listen" );
1071     lua_pushcfunction( lua, tcp_listen );
1072     lua_rawset( lua, -3 );
1073 
1074     lua_pushstring( lua, "accept" );
1075     lua_pushcfunction( lua, tcp_accept );
1076     lua_rawset( lua, -3 );
1077 
1078     lua_pushstring( lua, "readn" );
1079     lua_pushcfunction( lua, tcp_readn );
1080     lua_rawset( lua, -3 );
1081 
1082     lua_pushstring( lua, "writen" );
1083     lua_pushcfunction( lua, tcp_writen );
1084     lua_rawset( lua, -3 );
1085 
1086     lua_pushstring( lua, "read" );
1087     lua_pushcfunction( lua, tcp_read );
1088     lua_rawset( lua, -3 );
1089 
1090     lua_pushstring( lua, "write" );
1091     lua_pushcfunction( lua, tcp_write );
1092     lua_rawset( lua, -3 );
1093 
1094     lua_pushstring( lua, "MSK_TIMEO_RCV" );
1095     lua_pushinteger( lua, TIMEO_RCV );
1096     lua_rawset( lua, -3 );
1097 
1098     lua_pushstring( lua, "MSK_TIMEO_SND" );
1099     lua_pushinteger( lua, TIMEO_SND );
1100     lua_rawset( lua, -3 );
1101 
1102     /****************************************************/
1103     luaL_newmetatable( lua, "luasocket.select.metatable" );
1104 
1105     lua_pushstring( lua, "__index" );
1106     lua_pushvalue( lua, -2 );
1107     lua_rawset( lua, -3 );
1108 
1109     lua_pushstring( lua, "__newindex" );
1110     lua_pushcfunction( lua, table_readonly );
1111     lua_rawset( lua, -3 );
1112 
1113     lua_pushstring( lua, "__gc" );
1114     lua_pushcfunction( lua, select_destroy );
1115     lua_rawset( lua, -3 );
1116 
1117     lua_pushstring( lua, "clear" );
1118     lua_pushcfunction( lua, select_clear );
1119     lua_rawset( lua, -3 );
1120 
1121     lua_pushstring( lua, "add" );
1122     lua_pushcfunction( lua, select_addfd );
1123     lua_rawset( lua, -3 );
1124 
1125     lua_pushstring( lua, "del" );
1126     lua_pushcfunction( lua, select_delfd );
1127     lua_rawset( lua, -3 );
1128 
1129     lua_pushstring( lua, "isset" );
1130     lua_pushcfunction( lua, select_isset );
1131     lua_rawset( lua, -3 );
1132 
1133     lua_pushstring( lua, "select" );
1134     lua_pushcfunction( lua, select_select );
1135     lua_rawset( lua, -3 );
1136 
1137     lua_pushstring( lua, "MSK_SEL_READ" );
1138     lua_pushinteger( lua, SELECT_READ );
1139     lua_rawset( lua, -3 );
1140 
1141     lua_pushstring( lua, "MSK_SEL_WRIT" );
1142     lua_pushinteger( lua, SELECT_WRIT );
1143     lua_rawset( lua, -3 );
1144 
1145     lua_pushstring( lua, "MSK_SEL_EXCE" );
1146     lua_pushinteger( lua, SELECT_EXCE );
1147     lua_rawset( lua, -3 );
1148 
1149     /****************************************************/
1150 
1151     luaL_newlib( lua, _mylibs );
1152     return 1;
1153 }

 

posted on 2012-12-26 21:39  郁郁思扬  阅读(2504)  评论(0编辑  收藏  举报

导航