Open RenRoe opened 3 years ago
It would be nice to change or append the events of the class, and make it more compatible to WinSock control.
Winsock control has completely different events. There is a Winsock replacement control in the repo in contrib
directory that uses cAsyncSocket
class to emulate Winsock control.
There is no point in emulating Winsock control directly in the cAsyncSocket
class which is prototyped on MFC implementation, not on Winsock control methods/properties/events.
To let this run, there occurred a problem, that duplicating a socket copies the notification settings of the listener socket. The duplicated socket cannot send data.
You mean not copying notification settings? There is a optional EventMask
parameter on Attach
method that should be already calling WSAAsyncSelect
.
You might try usign this param to set notifications to ucsSfdRead Or ucsSfdWrite Or ucsSfdAccept Or ucsSfdConnect Or ucsSfdClose
like you do in your snippet although the default value of ucsSfdAll
seems good enough to me.
Btw, your fix to Bind
is calling WSAAsyncSelect
with wrong parameters and cannot possible fix anything. Something else must have been going wrong here because this cannot be a fix for anything.
Every message of the helper window will be delivered to the callback reference instead of event raising. No problems with reentrancy anymore.
What reentrancy problems do you have? Can you post a sample code so that I can debug any problems with reentrancy you have?
Best would be to make a minimal project/project group that reproduces any problems you have and post a ZIP with all these sources so I can debug it here on my machine.
1.)
WinSock control uses the event
ConnectionRequest(byval requestID As Long)
This request ID indeed is the socket handle returned by WS2_32 accept.
I use this temporary socket handle to be duplicated to another process already started to handle the connection in another process.
Example of forwarding of a socket handle to another process:
The DoAccept of the child process uses the Attach function of a cAsyncSocket object
It would be nice to change or append the events of the class, and make it more compatible to WinSock control.
2.)
To let this run, there occurred a problem, that duplicating a socket copies the notification settings of the listener socket. The duplicated socket cannot send data.
The Bind method needs to be modified:
Public Function Bind(Optional SocketAddress As String, Optional ByVal SocketPort As Long) As Boolean Const FUNC_NAME As String = "Bind" Dim uAddr As SOCKADDR_IN
QH: Exit Function EH: PrintError FUNC_NAME End Function
Here you see the settings for notification of the listener, in additionWSAAsyncSelect to enable all types of events.
3.) there are a couple of problems with event handling
It would be a good idea to create an interface IAsyncSocket
Option Explicit
Public Sub BeforeNotify(ByVal EventMask As UcsAsyncSocketEventMaskEnum, Cancel As Boolean)
End Sub
Public Sub OnClose()
End Sub
Public Sub OnConnect()
End Sub
Public Sub OnConnectionRequest(ByVal requestID As Long)
End Sub
Public Sub OnError(ByVal ErrorCode As Long, ByVal EventMask As UcsAsyncSocketEventMaskEnum)
End Sub
Public Sub OnMessagePending(Handled As Boolean)
End Sub
Public Sub OnReceive()
End Sub
Public Sub OnResolve(IpAddress As String)
End Sub
Public Sub OnSend()
End Sub
and setting a callback reference while creation of the socket object. Every message of the helper window will be delivered to the callback reference instead of event raising. No problems with reentrancy anymore.
'new private member of cAsyncSocket Private mInterface As IAsyncSocket
The callback interface is an additional parameter of Create.
Public Function Create( Optional ByVal SocketPort As Long, Optional ByVal SocketType As UcsAsyncSocketTypeEnum = ucsSckStream, Optional ByVal EventMask As UcsAsyncSocketEventMaskEnum = ucsSfdAll, Optional SocketAddress As String, _ Optional pCallbackInterface As IAsyncSocket) As Boolean
QH: Exit Function EH: PrintError FUNC_NAME End Function
Private Sub pvDoNotify(ByVal wParam As Long, ByVal lParam As Long) Dim eEvent As UcsAsyncSocketEventMaskEnum Dim bCancel As Boolean Dim lBytes As Long
QH: End Sub