• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

LUA_TCP网络

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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< 

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
lua杂记01--链表发布时间:2022-07-22
下一篇:
GG修改器的基本使用与lua脚本的基本认识发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap