| |
DLLBinder Example - Chatterbox
To use this code, make sure you have downloaded the DLLBinder Pro V1.0.0
and String Handler V1.1.0 XTRAs.
Feel free to make use of this code in any way you want. If you make any great
improvements, please send us a copy. Or if you have any other examples you would
like to appear in these LIBRARY pages, please send it to
info@the-mindseye.co.uk
Minds Eye Visualisation Services accepts no responibility for the reliability
of any of the source code appearing in these pages. Use it at your own risk.
|
|
|
|
------------------------------------------------------------------------
------------------------------------------------------------------------
-- SCRIPT NAME sctTCPServiceClientConn
-- SCRIPT TYPE Parent
-- DATE CREATED 10th February 2005
-- AUTHOR Minds Eye Visualisation
-- PURPOSE generic TCP client connection object.
-- the handlers in this script as well as its purpose
-- correspond closely to the handlers and purpose of the
-- sctTCPClientServiceConn parent script in the chatterbox
-- client movie mevChatterboxClient.dir
------------------------------------------------------------------------
------------------------------------------------------------------------
property m_hSocket
property m_oSender
property m_oReceiver
property m_oMessage
property m_nMaxMessageSize
property m_symStatus
------------------------------------------------------------------------
------------------------------------------------------------------------
-- FUNCTION new
-- PURPOSE child instance initialisation.
-- ACCESS External
------------------------------------------------------------------------
------------------------------------------------------------------------
on new me, hSocket
me.m_nMaxMessageSize=512
me.m_hSocket=hSocket
me.m_oSender=new(xtra "mevDllBinderPro")
me.m_oSender.mevcDllLoad("ws2_32.dll")
me.m_oReceiver=new(xtra "mevDllBinderPro")
me.m_oReceiver.mevcDllLoad("ws2_32.dll")
--this object is only created in response to receiving a hSocket to
--an established connection so can start waiting for msgs immediately
me.m_symStatus=#ConnectionOpen
me.ReceivePacket()
return me
end
------------------------------------------------------------------------
------------------------------------------------------------------------
-- FUNCTION CloseConnection
-- PURPOSE close a connection with the host service.
-- ACCESS External
------------------------------------------------------------------------
------------------------------------------------------------------------
on CloseConnection me
me.CloseSocket()
me.m_symStatus=#ConnectionClosing
end
------------------------------------------------------------------------
------------------------------------------------------------------------
-- FUNCTION GetStatus
-- PURPOSE returns the TCP service status.
-- ACCESS External
------------------------------------------------------------------------
------------------------------------------------------------------------
on GetStatus me
return me.m_symStatus
end
------------------------------------------------------------------------
------------------------------------------------------------------------
-- FUNCTION SendStream
-- PURPOSE sends a client message to the host service.
-- You should perform any message integrity tests here.
-- ACCESS External
------------------------------------------------------------------------
------------------------------------------------------------------------
on SendStream me, oMessage
if oMessage.mevcGetLength() > me.m_nMaxMessageSize then
alert "maximum message size exceeded!"
halt()
end if
me.SendPacket(oMessage)
end
------------------------------------------------------------------------
------------------------------------------------------------------------
-- FUNCTION ReceiveStream
-- PURPOSE receives any new messages;
-- returns the current connection status.
-- ACCESS External
------------------------------------------------------------------------
------------------------------------------------------------------------
on ReceiveStream me
oRetMsg=void
if (me.m_oReceiver.mevcGetStatus()=#ThreadRunning) then
lstInputArgs=[]
lstRetVal=[]
--check if this thread has completed
if (me.m_oReceiver.mevcGetFunThreadResults(\
lstInputArgs,\
lstRetVal)) then
--if Win32 API return val > 0 its the length of the received msg
if (lstRetVal[1]>0) then
--copy member message stream ref to return val
oRetMsg=me.m_oMessage
--reset the message stream object to size of received stream
oRetMsg.mevcSetBufferLength(lstRetVal[1])
--start waiting for another new message stream?
if me.m_symStatus=#ConnectionOpen then
me.ReceivePacket()
else if me.m_symStatus=#ConnectionClosing then
me.m_symStatus=#ConnectionClosed
end if
else if me.m_symStatus=#ConnectionClosing then
me.m_symStatus=#ConnectionClosed
else --treat as an error
me.m_symStatus=#ConnectionError
end if
end if
else --thread is not running but should be
alert "receiver thread not running"
halt()
end if
return oRetMsg
end
------------------------------------------------------------------------
------------------------------------------------------------------------
-- FUNCTION ReceivePacket
-- PURPOSE receives a packet of data from the host service.
-- ACCESS Internal
------------------------------------------------------------------------
-- C API INFO (see MSDN for more info)
--
-- This is a blocking call. it will not return until it has received
-- something or the connection is dropped
--
-- int recv(
-- SOCKET s,
-- char FAR * buf,
-- int len,
-- int flags
-- );
--
------------------------------------------------------------------------
------------------------------------------------------------------------
on ReceivePacket me
--create a new message stream
me.m_oMessage=new(xtra"mevString")
me.m_oMessage.mevcSetBufferLength(me.m_nMaxMessageSize)
strFnName="recv"
strInputArgFormat="D,r,d,d"
lstInputArgs=[]
lstInputArgs[1]=me.m_hSocket --the socket handle
lstInputArgs[2]=me.m_oMessage.mevcGetStringPtr() --packet string ref
lstInputArgs[3]=me.m_nMaxMessageSize --size of the packet
lstInputArgs[4]=0 --default flags val
strRetValFormat="d"
lstRetVal=[]
--make an asynchronous call so that we get control back immediately
nErr=me.m_oReceiver.mevcCallDllFunThread(\
strFnName,\
strInputArgFormat,\
lstInputArgs,\
strRetValFormat)
end
------------------------------------------------------------------------
------------------------------------------------------------------------
-- FUNCTION SendPacket
-- PURPOSE sends a packet of data to the host service.
-- ACCESS Internal
--
------------------------------------------------------------------------
-- C API INFO (see MSDN for more info)
--
-- int send(
-- SOCKET s,
-- const char FAR * buf,
-- int len,
-- int flags);
--
------------------------------------------------------------------------
------------------------------------------------------------------------
on SendPacket me, oMessage
strFnName="send"
strInputArgFormat="D,r,d,d"
lstInputArgs=[]
lstInputArgs[1]=me.m_hSocket --the socket handle
lstInputArgs[2]=oMessage.mevcGetStringPtr() --packet string ref
lstInputArgs[3]=oMessage.mevcGetLength() --size of the packet
lstInputArgs[4]=0 --default flags val
strRetValFormat="d"
lstRetVal=[]
nErr=m_oSender.mevcCallDllFun(\
strFnName, \
strInputArgFormat,\
lstInputArgs,\
strRetValFormat,\
lstRetVal)
end
------------------------------------------------------------------------
------------------------------------------------------------------------
-- FUNCTION CloseSocket
-- PURPOSE releases the socket. further references to the socket
-- will then fail with the error WSAENOTSOCK.
-- ACCESS Internal
--
------------------------------------------------------------------------
-- C API INFO (see MSDN for more info)
--
-- int closesocket(SOCKET s);
--
------------------------------------------------------------------------
------------------------------------------------------------------------
on CloseSocket me
strFnName="closesocket"
strInputArgFormat="D"
lstInputArgs=[me.m_hSocket]
strRetValFormat="d"
lstRetVal=[]
nErr=m_oSender.mevcCallDllFun(\
strFnName, \
strInputArgFormat,\
lstInputArgs,\
strRetValFormat,\
lstRetVal)
end
------------------------------------------------------------------------
------------------------------------------------------------------------
--end of script
|