Skip to content
This repository has been archived by the owner on Feb 5, 2021. It is now read-only.

Commit

Permalink
CLionetHttp
Browse files Browse the repository at this point in the history
  • Loading branch information
LionNatsu committed Jul 24, 2014
1 parent 0f30ceb commit e0fe56a
Show file tree
Hide file tree
Showing 6 changed files with 543 additions and 60 deletions.
77 changes: 49 additions & 28 deletions CLionet.bas
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#undef opensocket

constructor CLionet()
this.startup()
this.opensocket()
end constructor

Expand Down Expand Up @@ -43,31 +44,51 @@ function CLionet.error_string() as zstring ptr
end function
#endif

function CLionet.parseAddress( address as string ) as uinteger
dim as uinteger dwAddress = inet_addr( address )
if dwAddress <> INADDR_NONE then return dwAddress
dim as hostent ptr host = gethostbyname( address )
if host = 0 then return 0
return *(host->h_addr)
end function

function CLionet.eventThread( boku as CLionet ptr ) as integer
print "thread"
dim as WSANETWORKEVENTS event_info
do
if boku->flag_thread = 2 then boku->flag_thread = 0 : exit do
if WSAWaitForMultipleEvents( 1, @boku->m_event, -1, 1000, -1 ) = WSA_WAIT_TIMEOUT then continue do
WSAEnumNetworkEvents( boku->m_socket, boku->m_event, @event_info )
#macro ROUTER( fnc, evtmsk, bitmsk )
#macro ROUTER_INIT_EXIT( fnc, msg )
if boku->onSocket <> 0 then
boku->onSocket( boku, msg, 0 )
elseif boku->fnc <> 0 then
boku->fnc( boku )
endif
#endmacro
#macro ROUTER( fnc, evtmsk, bitmsk, msg )
if ( event_info.lNetworkEvents and evtmsk ) <> 0 then
if boku->onSocket <> 0 then
boku->onSocket( boku, evtmsk, event_info.iErrorCode( bitmsk ) )
boku->onSocket( boku, msg, event_info.iErrorCode( bitmsk ) )
elseif boku->fnc <> 0 then
boku->fnc( boku, event_info.iErrorCode( bitmsk ) )
endif
endif
#endmacro
ROUTER (onAccept,FD_ACCEPT,FD_ACCEPT_BIT)
ROUTER (onConnect,FD_CONNECT,FD_CONNECT_BIT)
ROUTER (onRead,FD_READ,FD_READ_BIT)
ROUTER (onWrite,FD_WRITE,FD_WRITE_BIT)
ROUTER (onClose,FD_CLOSE,FD_CLOSE_BIT)
#undef ROUTER
#endmacro
dim as WSANETWORKEVENTS event_info
ROUTER_INIT_EXIT( onInit, CLE_INIT )
do
if boku->flag_thread = 2 then
boku->flag_thread = 0
ROUTER_INIT_EXIT( onInit, CLE_INIT )
exit do
endIf
if WSAWaitForMultipleEvents( 1, @boku->m_event, -1, 1000, -1 ) = WSA_WAIT_TIMEOUT then continue do
WSAEnumNetworkEvents( boku->m_socket, boku->m_event, @event_info )
ROUTER( onAccept, FD_ACCEPT, FD_ACCEPT_BIT, CLIONET_EVENT.CLE_ACCEPT )
ROUTER( onConnect, FD_CONNECT, FD_CONNECT_BIT, CLIONET_EVENT.CLE_CONNECT )
ROUTER( onRead, FD_READ, FD_READ_BIT, CLIONET_EVENT.CLE_READ )
ROUTER( onWrite, FD_WRITE, FD_WRITE_BIT, CLIONET_EVENT.CLE_WRITE )
ROUTER( onClose, FD_CLOSE,FD_CLOSE_BIT, CLIONET_EVENT.CLE_CLOSE )
sleep_ 0
loop
return 0
#undef ROUTER_INIT_EXIT
#undef ROUTER
end function

function CLionet.accept() as integer
Expand Down Expand Up @@ -116,16 +137,16 @@ function CLionet.bind( port as ushort ) as integer
end function

function CLionet.bind( addr as string, port as ushort ) as integer
return this.bind( .inet_addr( addr ), port )
return this.bind( CLionet.parseAddress( addr ), port )
end function

function CLionet.bind( addr as uinteger, port as ushort ) as integer
dim as sockaddr_in serveraddr
with serveraddr
.sin_family = AF_INET
.sin_addr.S_addr = addr
.sin_port = htons( port )
end with
serveraddr.sin_port = .htons( port )
return this.bind( cast( sockaddr ptr, @serveraddr ) )
end function

Expand All @@ -134,16 +155,16 @@ function CLionet.bind( lpName as any ptr ) as integer
end function

function CLionet.connect( addr as string, port as ushort ) as integer
return this.connect( .inet_addr( addr ), port )
return this.connect( CLionet.parseAddress( addr ), port )
end function

function CLionet.connect( addr as uinteger, port as ushort ) as integer
dim as sockaddr_in serveraddr
with serveraddr
.sin_family = AF_INET
.sin_addr.S_addr = addr
.sin_port = htons( port )
end with
serveraddr.sin_port = .htons( port )
return this.connect( cast( sockaddr ptr, @serveraddr ) )
end function

Expand All @@ -163,7 +184,7 @@ end function

function CLionet.opensocket() as integer
if this.m_socket then if this.closesocket() = 0 then return SOCKET_ERROR
this.m_socket = .socket_( AF_INET, SOCK_STREAM, IPPROTO_TCP )
this.m_socket = socket_( AF_INET, SOCK_STREAM, IPPROTO_TCP )
this.attachEvent()
return this.m_socket
end function
Expand All @@ -180,7 +201,7 @@ end property
property CLionet.async( mode as CLIONET_MODE )
if this.async_mode = mode then exit property
select case mode
case CLIONET_MODE.CLAM_EVENTSELECT
case CLIONET_MODE.CLM_EVENTSELECT
if this.flag_thread <> 0 then exit property
this.async_mode = mode
this.m_event = WSACreateEvent()
Expand All @@ -190,7 +211,7 @@ property CLionet.async( mode as CLIONET_MODE )
CreateThread( 0, 0, _
cast( LPTHREAD_START_ROUTINE, procptr( CLionet.eventThread ) ), _
@this, 0, @ThreadID )
case CLIONET_MODE.CLAM_BLOCK
case CLIONET_MODE.CLM_BLOCK
this.async_mode = mode
this.detachEvent()
this.flag_thread = 2
Expand All @@ -207,19 +228,19 @@ sub CLionet.exitEventThread()
end sub

sub CLionet.attachEvent()
if this.async_mode <> CLIONET_MODE.CLAM_EVENTSELECT then exit sub
if this.async_mode <> CLIONET_MODE.CLM_EVENTSELECT then exit sub
WSAEventSelect( this.m_socket, this.m_event, FD_READ or FD_CLOSE or FD_CONNECT or FD_ACCEPT or FD_WRITE )
end sub

sub CLionet.detachEvent()
if this.async_mode <> CLIONET_MODE.CLAM_EVENTSELECT then exit sub
if this.async_mode <> CLIONET_MODE.CLM_EVENTSELECT then exit sub
WSAEventSelect( this.m_socket, this.m_event, 0 )
end sub

property CLionet.remoteip() as zstring ptr
property CLionet.remoteip() as string
dim remoteAddr as sockaddr_in, i as integer = sizeof( sockaddr_in )
.getpeername( this.m_socket, cast( sockaddr ptr, @remoteAddr ), @i )
return .inet_ntoa( remoteAddr.sin_addr )
return *.inet_ntoa( remoteAddr.sin_addr )
end property

property CLionet.remoteport() as ushort
Expand All @@ -228,10 +249,10 @@ property CLionet.remoteport() as ushort
return .ntohs( remoteAddr.sin_port )
end property

property CLionet.localip() as zstring ptr
property CLionet.localip() as string
dim localAddr as sockaddr_in, i as integer = sizeof( sockaddr_in )
.getsockname( this.m_socket, cast( sockaddr ptr, @localAddr ), @i )
return .inet_ntoa( localAddr.sin_addr )
return *.inet_ntoa( localAddr.sin_addr )
end property

property CLionet.localport() as ushort
Expand Down
31 changes: 22 additions & 9 deletions CLionet.bi
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@
#undef opensocket

enum CLIONET_MODE
CLAM_BLOCK
CLAM_EVENTSELECT
CLM_BLOCK
CLM_EVENTSELECT
end enum
enum CLIONET_EVENT
CLE_ACCEPT
CLE_CONNECT
CLE_READ
CLE_WRITE
CLE_CLOSE
CLE_INIT
CLE_EXIT
end enum
type CLionet extends object
public:
Expand Down Expand Up @@ -37,20 +46,24 @@ type CLionet extends object
declare virtual function accept( scklistener as SOCKET ) as integer
declare virtual function send( in_buf as any ptr, length as integer ) as integer
declare virtual function recv( out_buf as any ptr, length as integer, f_peek as integer = 0 ) as integer
declare property thesocket() as SOCKET
declare property remoteip() as zstring ptr
declare property remoteport() as ushort
declare property localip() as zstring ptr
declare property localport() as ushort
declare property async( mode as CLIONET_MODE )
declare property async() as CLIONET_MODE
declare virtual property thesocket() as SOCKET
declare virtual property remoteip() as string
declare virtual property remoteport() as ushort
declare virtual property localip() as string
declare virtual property localport() as ushort
declare virtual property async( mode as CLIONET_MODE )
declare virtual property async() as CLIONET_MODE
onSocket as sub ( boku as CLionet ptr, msg as integer, errcode as integer )
onInit as sub ( boku as CLionet ptr )
onExit as sub ( boku as CLionet ptr )
onAccept as sub ( boku as CLionet ptr, errcode as integer )
onConnect as sub ( boku as CLionet ptr, errcode as integer )
onRead as sub ( boku as CLionet ptr, errcode as integer )
onWrite as sub ( boku as CLionet ptr, errcode as integer )
onClose as sub ( boku as CLionet ptr, errcode as integer )
g_key as any ptr
protected:
declare static function parseAddress( address as string ) as uinteger
declare static function eventThread( boku as CLionet ptr ) as integer
declare virtual function connect( lpName as any ptr ) as integer
declare virtual function bind( lpName as any ptr ) as integer
Expand Down
25 changes: 16 additions & 9 deletions CLionet.fbp
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,39 @@ CompileIfNewer=0
IncVersion=0
RunCmd=0
[Make]
Current=1
Module=Module Build,fbc -c
Recompile=2
Current=3
1=Windows Console (Server),fbc -s console -d __SERVER__ -x "Server.exe"
2=Windows Console (Client),fbc -s console -x "Client.exe"
Recompile=2
Module=Module Build,fbc -c
3=Windows Console,fbc -s console -x "Lionet.exe" -g
Output=
Run=
Run=Lionet.exe
Delete=
[TabOrder]
TabOrder=1,2,1001,3
TabOrder=1,1002,5,1001,2,3
[File]
1=Lionet.bas
2=CLionet.bi
3=README.md
Main=1
1001=CLionet.bas
4=inc\winsock2.bi
4=
5=CLionetHttp.bi
1002=CLionetHttp.bas
[BreakPoint]
1=
2=
3=
1001=
4=
5=
1002=
[FileInfo]
1=0,32,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
2=0,22,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
1=0,58,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
2=0,71,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
3=0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
1001=0,204,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
1001=0,210,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
4=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
5=0,18,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
1002=0,139,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Loading

0 comments on commit e0fe56a

Please sign in to comment.