Page 1 of 2

Asm code not working anymore on PB v4 :(

Posted: Fri May 26, 2006 8:52 pm
by Phlos
Hello,

This code perfectly works on PB v3, but it doesn't on v4 (i debugged it and it crashs on "ret", the "return adress" is overwritten, the local stack allocated is too small for all data) :cry:

Code: Select all

Procedure OpenNetworkConnectionEx(ServerName$, Port.l, TimeOut.l)
  !Conn equ Esp+12
  !Wsa  equ Esp+28
  !Tout equ Esp+428
  !WaitX equ Esp+436
  !Sock equ Esp+444
  
  WsaData.WSADATA
  Connection.SOCKADDR_IN
  TimeOuts.TIMEVAL
  WaitSock.ULARGE_INTEGER
  Sock.l
  
  !If ~ defined _gethostbyname@4 | defined @f
    !extrn _gethostbyname@4
    !@@:
    !End If

  !If ~ defined _WSAStartup@8 | defined @f
    !extrn _WSAStartup@8
    !@@:
  !End If

  !If ~ defined _socket@12 | defined @f
    !extrn _socket@12
    !@@:
  !End If

  !If ~ defined _htons@4 | defined @f
    !extrn _htons@4
    !@@:
  !End If

  !If ~ defined _ioctlsocket@12 | defined @f
    !extrn _ioctlsocket@12
    !@@:
  !End If

  !If ~ defined _connect@12 | defined @f
    !extrn _connect@12
    !@@:
  !End If

  !If ~ defined _select@20 | defined @f
    !extrn _select@20
    !@@:
  !End If

  !If ~ defined _closesocket@4 | defined @f
    !extrn _closesocket@4
    !@@:
  !End If

  !If ~ defined _WSACleanup@0 | defined @f
    !extrn _WSACleanup@0
    !@@:
  !End If

  !MOV    Eax, [Esp]
  !OR     Eax, Eax
  !JZ     @f
  !PUSH   Eax
  !CALL   _gethostbyname@4
  !OR     Eax, Eax
  !JZ     @f
  !MOVSX  Ebx, word [Eax+8]
  !CMP    Ebx, 2
  !JNE    @f
  !MOV    Ecx, [Eax+12]
  !MOV    Ecx, [Ecx]
  !MOV    Ecx, [Ecx] 
  !MOV    [Conn+4], Ecx 
  !LEA    Eax, [Wsa]
  !PUSH   Eax
  !PUSH   $0002
  !CALL   _WSAStartup@8
  !OR     Eax, Eax
  !JNZ    @f 
  !CMP    byte [Wsa], 2
  !JL     @f
  !PUSH   6
  !PUSH   1
  !PUSH   2
  !CALL   _socket@12
  !CMP    Eax, -1
  !JE     @f 
  !MOV    [Sock], Eax
  !MOV    word [Conn], 2
  !PUSH   dword [Esp+4]
  !CALL   _htons@4
  !MOV    word [Conn+2], ax
  !MOV    Eax, dword [Esp+8]
  !MOV    dword [Tout], Eax
  !MOV    dword [WaitX], 1
  !MOV    Eax, dword [Sock]
  !MOV    dword [WaitX+4], Eax
  !LEA    Eax, [WaitX]
  !PUSH   Eax
  !PUSH   -2147195266
  !PUSH   dword [Sock+8]
  !CALL   _ioctlsocket@12
  !PUSH   dword 16
  !LEA    Eax, [Conn+4]
  !PUSH   Eax
  !PUSH   dword [Sock+8]
  !CALL   _connect@12
  !LEA    Eax, [Tout]
  !PUSH   Eax
  !PUSH   0
  !LEA    Eax, [WaitX+8]
  !PUSH   Eax
  !PUSH   0
  !PUSH   0
  !CALL   _select@20
  !CMP    Eax, 0
  !JNA    @f
  !LEA    Eax, [Tout+4]
  !PUSH   Eax
  !PUSH   -2147195266
  !PUSH   dword [Sock+8]
  !CALL   _ioctlsocket@12
  !CALL   _WSACleanup@0
  !MOV    Eax, [Sock]
  ProcedureReturn 
  !@@:
  !CMP    dword [Sock], -1
  !JNE    @f
  !PUSH   dword [Sock]
  !CALL   _closesocket@4 
  !@@:
  !CMP    byte [Wsa], 0
  !JE     @f
  !CALL   _WSACleanup@0
  !@@:
  
  !Restore Conn
  !Restore Wsa
  !Restore Tout
  !Restore WaitX
  !Restore Sock
EndProcedure
                                    
                                    
InitNetwork()

Host$ = "www.purearea.net"
ServerID = OpenNetworkConnectionEx(Host$, 80, 1)
If ServerID = 0
  MessageRequester("", "Timeout")
Else
  MessageRequester("", "OK")
  CloseNetworkConnection(ServerID)
EndIf 
Can someone change it for v4 please ? :oops:

Posted: Fri May 26, 2006 9:58 pm
by Phlos
In simple way :
Procedure Function()
!Var equ Esp
!Var2 equ Esp+8

!MOV dword [Var], 0

!Restore Var2
!Restore Var
EndProcedure

Function()

Debug "OK in PBv3, CRASH in PBv4 !"
It perfectly works on PB v3 and crash on PB v4 ! :shock:
Fred what did you do ? :cry:

Posted: Fri May 26, 2006 11:19 pm
by flaith
can that approach be correct for you ?

Code: Select all

Procedure Function()
    Protected Var, Var2

    !MOV dword [p.v_Var], 0

EndProcedure

Function()

Debug "OK in PBv3, CRASH in PBv4 !"

Posted: Sat May 27, 2006 12:25 am
by Phlos
Thank you. I "translated" the code to PB v4 with your knowledge. :D

Code: Select all

Procedure OpenNetworkConnectionEx(ServerName$, Port.l, TimeOut.l)
  Protected WsaData.WSADATA
  Protected Connection.SOCKADDR_IN
  Protected TimeOuts.TIMEVAL
  Protected WaitSock.ULARGE_INTEGER
  Protected Sock.l
  
  !If ~ defined _gethostbyname@4 | defined @f
  !EXTRN _gethostbyname@4
  !@@:
  !End If
  
  !If ~ defined _WSAStartup@8 | defined @f
  !EXTRN _WSAStartup@8
  !@@:
  !End If
  
  !If ~ defined _socket@12 | defined @f
  !EXTRN _socket@12
  !@@:
  !End If
  
  !If ~ defined _htons@4 | defined @f
  !EXTRN _htons@4
  !@@:
  !End If
  
  !If ~ defined _ioctlsocket@12 | defined @f
  !EXTRN _ioctlsocket@12
  !@@:
  !End If
  
  !If ~ defined _connect@12 | defined @f
  !EXTRN _connect@12
  !@@:
  !End If
  
  !If ~ defined _select@20 | defined @f
  !EXTRN _select@20
  !@@:
  !End If
  
  !If ~ defined _closesocket@4 | defined @f
  !EXTRN _closesocket@4
  !@@:
  !End If
  
  !If ~ defined _WSACleanup@0 | defined @f
  !EXTRN _WSACleanup@0
  !@@:
  !End If

  !MOV    Eax, [p.v_ServerName$]
  !OR     Eax, Eax
  !JZ     @f
  !PUSH   Eax
  !CALL   _gethostbyname@4
  !OR     Eax, Eax
  !JZ     @f
  !MOVSX  Ebx, word [Eax+8]
  !CMP    Ebx, 2
  !JNE    @f
  !MOV    Ecx, [Eax+12]
  !MOV    Ecx, [Ecx]
  !MOV    Ecx, [Ecx] 
  !MOV    [p.v_Connection+4], Ecx 
  !LEA    Eax, [p.v_WsaData]
  !PUSH   Eax
  !PUSH   $0002
  !CALL   _WSAStartup@8
  !OR     Eax, Eax
  !JNZ    @f 
  !CMP    byte [p.v_WsaData], 2
  !JL     @f
  !PUSH   6
  !PUSH   1
  !PUSH   2
  !CALL   _socket@12
  !CMP    Eax, -1
  !JE     @f 
  !MOV    [p.v_Sock], Eax
  !MOV    word [p.v_Connection], 2
  !PUSH   dword [p.v_Port]
  !CALL   _htons@4
  !MOV    word [p.v_Connection+2], ax
  !MOV    Eax, dword [p.v_TimeOut]
  !MOV    dword [p.v_TimeOuts], Eax
  !MOV    dword [p.v_WaitSock], 1
  !MOV    Eax, dword [p.v_Sock]
  !MOV    dword [p.v_WaitSock+4], Eax
  !LEA    Eax, [p.v_WaitSock]
  !PUSH   Eax
  !PUSH   -2147195266
  !PUSH   dword [p.v_Sock+8]
  !CALL   _ioctlsocket@12
  !PUSH   dword 16
  !LEA    Eax, [p.v_Connection+4]
  !PUSH   Eax
  !PUSH   dword [p.v_Sock+8]
  !CALL   _connect@12
  !LEA    Eax, [p.v_TimeOuts]
  !PUSH   Eax
  !PUSH   0
  !LEA    Eax, [p.v_WaitSock+8]
  !PUSH   Eax
  !PUSH   0
  !PUSH   0
  !CALL   _select@20
  !CMP    Eax, 0
  !JNA    @f
  !LEA    Eax, [p.v_TimeOuts+4]
  !PUSH   Eax
  !PUSH   -2147195266
  !PUSH   dword [p.v_Sock+8]
  !CALL   _ioctlsocket@12
  !CALL   _WSACleanup@0
  !MOV    Eax, [p.v_Sock]
  ProcedureReturn 
  !@@:
  !CMP    dword [p.v_Sock], -1
  !JNE    @f
  !PUSH   dword [p.v_Sock]
  !CALL   _closesocket@4 
  !@@:
  !CMP    byte [p.v_WsaData], 0
  !JE     @f
  !CALL   _WSACleanup@0
  !@@:
EndProcedure


InitNetwork()

Host$ = "www.purearea.net"
ServerID = OpenNetworkConnectionEx(Host$, 80, 1)
If ServerID = 0
  MessageRequester("", "Timeout")
Else
  MessageRequester("", "OK")
  CloseNetworkConnection(ServerID)
EndIf


But now it crashs on "CloseNetworkConnection(ServerID)" :roll:
Maybe I translated something wrong in the asm code ? :?:

Posted: Sat May 27, 2006 12:33 am
by flaith
Maybe because you have to make your own "CloseNetworkConnection" :wink:

Posted: Sat May 27, 2006 12:36 am
by Phlos

Code: Select all

ConnectionID(Connection)
What a shame, PureBasic v4 doesn't use anymore system socket but "own handle" :evil:
That's probably why it crashs on "CloseNetworkConnection(ServerID)" :(

So no more Pure Basic for me, we cannot even have TimeOut on Network Connection, even if we try to code our own function ...... :evil:

Posted: Sat May 27, 2006 12:37 am
by Phlos
flaith wrote:Maybe because you have to make your own "CloseNetworkConnection" :wink:
And what I do with other network function...I recode everything ? :evil:

Posted: Sat May 27, 2006 1:10 am
by Dare2
Hi Phlos,

Perhaps stick with 3.94 for situations where it is best to do so? Think of it as having two languages for the price of one. :) (V3.94 and V4.00).

V4.00 is superior in many ways. However if a conversion is going to be a massive undertaking, then 3.94 is still there.

Posted: Sat May 27, 2006 1:12 am
by Phlos
Unfortunately I cannot go back to v3 because I use Mutex from v4 (lot of Threads with Network connections) :cry:

So the only solution is to stop the dev of my app (cannot go back to v3, cannot use v4) :shock:

Posted: Sat May 27, 2006 3:43 am
by josku_x
sounds pretty awful.

Posted: Sat May 27, 2006 7:09 am
by Dare2
Phlos wrote:cannot go back to v3, cannot use v4
Cannot? Or choose not? :)

Use Api mutex.

Posted: Sat May 27, 2006 8:33 am
by maw
Phlos wrote:So no more Pure Basic for me, we cannot even have TimeOut on Network Connection, even if we try to code our own function ...... :evil:
How childish... And the rest of your posts in this thread aren't much better. Why don't you take a timeout and come back when you can discuss in a more mature manner!

Posted: Sat May 27, 2006 11:14 am
by Flype
yes there are always solutions.

and you can't expect mixing purebasic and api functions to work in all case. when programming networks in C or others language you have to make use of socket libraries. if you started to make use win32 api for network programming, do it entirely with win32 api. no mixing.

:roll:

Posted: Sat May 27, 2006 8:46 pm
by Phlos
It just makes me angry because some people already asked for a TimeOut on network connections...and this is really not a big deal for a man that have an awesome skill to make his own compiler :?

That's why I am so angry, it just take few minutes to add this option, no ? :?

Posted: Sun May 28, 2006 9:43 am
by maw
Phlos wrote:It just makes me angry because some people already asked for a TimeOut on network connections...and this is really not a big deal for a man that have an awesome skill to make his own compiler :?
That's why I am so angry, it just take few minutes to add this option, no ? :?
Why should Fred give this request priority over the hundreds of other requests users has made? Because you want it? You're coming of like a spoiled brat and personally I hope you never get this feature because you certainly don't deserve it!

And if you really think it's only a matter of minutes to add something then you're even more clueless than I thought...