Jump to content

WSA_NBTCP v1.00 UDF


ripdad
 Share

Recommended Posts

10060 is a timeout error. You may have to set the timeout higher. Or perhaps you might need to disable it by setting it to 0 (which is the equivalent as keep-alive).

-1 is a DLL error (which should not happen). I have never experienced that error either -- very strange.

Perhaps it's because the socket is no longer valid.

That's the reason it's good to close the socket after an error occurs.

You can always re-initiate a connection.

 

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

Deye,

I had some time to get familiar with the two UDF's using AdlibRegister.
It seems that lowering the default of 250ms to 50ms would help speed it up.

Also, I noticed there wasn't any error checking in the 3 TCPSend() functions.

I rewrote the __TCPServer_Recv().
You can also rewrite the __TCPClient_Recv() the same way.

Before I was trying to stay with the format of the original one on the client.

This is the way I think it "should" work, with only one _WSA_TCPRecv call.
Let me know if it helps improve your issue. (comments included)

Func __TCPServer_Recv()
    Local $nBytesReceived, $nError, $sData, $sRecv
    ;
    For $i = 1 To $_TCPServer_MaxClients
        If $__TCPServer_Sockets[$i] < 1 Then
            ContinueLoop
        EndIf
        ;
        $sData = ''; <- clear previous data
        ;
        For $j = 1 To 3; <- three tries. (this is needed because usually the first one or two recv calls is blank)
            $sRecv = _WSA_TCPRecv($__TCPServer_Sockets[$i], 65536, 1, 10)
            $nError = @error
            $nBytesReceived = @extended
            ;
            If $nError Then
                ConsoleWrite('__TCPServer_Recv Error: ' & $nError & @CRLF)
                __TCPServer_KillConnection($i)
                ExitLoop
            ElseIf $nBytesReceived Then
                $sData &= BinaryToString($sRecv); <- gather incoming data
                $j = 0; <- reset to continue loop while receiving data
            Else
                ; <- no bytes received
            EndIf
            Sleep(10); <- 10 milliseconds (if the loop runs in microseconds, it won't be efficient in the long run)
        Next
        ;
        If $sData <> '' Then; <- includes any data gathered before an error occurred
            Call($_TCPServer_OnReceiveCallback, $__TCPServer_Sockets[$i], _TCPServer_SocketToIP($__TCPServer_Sockets[$i]), $sData, $__TCPServer_Pars[$i])
        EndIf
    Next
EndFunc

 

-edit-

Here is one of the TCPSend() functions as an example of how I think it should be done.

Func _TCPServer_Send($iSocket, $sData)
    If $iSocket < 1 Then
        Return SetError(-1, 0, 0); <- invalid socket
    ElseIf Not StringLen($sData) Then
        Return SetError(-2, 0, 0); <- no data to send
    EndIf
    ;
    Local $nBytesSent = TCPSend($iSocket, $sData)
    Local $nError = @error
    ;
    If $nError Then
        Return SetError($nError, 0, 0)
    Else
        Return SetError(0, 0, $nBytesSent)
    EndIf
EndFunc

You can substitute _WSA_TCPSend(), if you have problems with TCPSend(), since I haven't tested how the native function works with non-blocking sockets.

The WSA_NBTCP UDF was never intended to mix with other types. You either use one or the other, but not a mix of them. Unpredictable results will probably occur.

 

Edited by ripdad

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

21 hours ago, ripdad said:

The WSA_NBTCP UDF was never intended to mix with other types. You either use one or the other, but not a mix of them. Unpredictable results will probably occur.

Possibly with another WSA_NBTCP UDF  custom made and optimized to force\make it play along 
or accommodate _WSA_client-server UDFs that can be based on WSA_NBTCP UDF as is

(Not something I'm going to ask anyone to do .. )

I was only curious in asserting the benefits over the native functions if it was going to be an easy and applicable switch over 

Thanks for all the input and follow up calls

Deye

Link to comment
Share on other sites

It should not be difficult to switch over -- and should be fairly transparent.

As for "asserting the benefits", there are several. I'll list a few the native functions don't have...

1. _WSA_TCPRecv() -- BytesReceived in @extended, the Timeout Feature, and at the moment, proper error returns.
2. _WSA_TCPSend() -- BytesSent in @extended and the Select Timeout, which you can adjust.
3. The UDF can be tweaked how you want - because the source code is available to you for instant access.

And you're welcome. Glad to help whenever I can.

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

  • 9 months later...

Hi ripdad !

I'm trying to replace native Autoit tcp functions by yours and I've got a problem.

How do you handle tcpconnect part when the client is trying to connect to a downed server ?

With native function, i'm using this code :

$RecvPacket = TCPRecv($ConnectedSocket, $PacketSize)
If @error Then
    TCPCloseSocket($ConnectedSocket)
    $ConnectedSocket = TCPConnect($server_ip, $port)
    While @error
      _Sleep(2000)
      $ConnectedSocket = TCPConnect($server_ip, $port)
    WEnd
EndIf

But using the same code with _WSA_TCPConnect not reconnects the client if the server goes back online.

EDIT : NVM, I was not using your functions correctly.

Edited by tatane
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...