Big problem : oSocket:OnRead and oSocket:bRead ?

Big problem : oSocket:OnRead and oSocket:bRead ?

Postby dutch » Wed Dec 02, 2009 4:05 am

I have used oSocket to send & received data but the protocol have to receive data and send ack. The problem is I don't which one will get the data (between oSocket:bRead or oSocket:OnRead())?

Code: Select all  Expand view
*--------------------*
function Client()

   oSocket := TSocket():New( nPort )

   oSocket:bRead    = { | oSocket | OnRead( oSocket ) }  // oSocket:GetData() }  //

   // Never use a MsgInfo() here because it hangs Windows!!!
   oSocket:bConnect = { || OnConnected() }

   oSocket:bClose   = { || MsgInfo( "Server has closed!" ) }

   oSocket:Connect( cSvrIP ) // use the server IP address here

return nil

*-------------------*
Function OnConnected
local n := 0
local AddChr
local StartAck2, EndAck, EndTrnOk

lRun := !lRun

if !empty(PMSChkPBX)
    // save to logfile()
    // SaveLog( 'PMS : '+PMSSTrnMsn )
    if SendData( PMSSTrnMsn ) > 0

        if !empty(PMSAcknow)
            StartAck2 := ''
            WaitPeriod(5000)  // Loop for 5 seconds
            do while (StartAck2 <> PMSAcknow) .and. WaitPeriod()   // Wait for PMS Acknowledge Chr
            StartAck2 := oSocket:OnRead()    // read Chr from comm port
            if right(StartAck2,len(PMSSTrnMsn)) = PMSSTrnMsn
                    StartAck2 := right(StartAck2,len(PMSSTrnMsn))
                    exit
            end
            if StartAck2 = PMSETrnMsn
                    exit
            end
            end
        end
       
    AddChr := BCC(PMSChkPBX)
        do while n <= 2
            SendData( PMSChkPBX+AddChr )
            // SaveLog( 'PMS : '+PMSChkPBX+AddChr )
        // oSocket:SendData( PMSChkPBX+AddChr )
        MsgWait('Please wait for connection : '+str(n+1,2),,1)
        if lConnect
            exit
        end
        n++
        end
        // if not reply for 3 times, END Socket()
        if n > 2
        oSocket:End()
        end
       
        EndAck  := ''
        EndTrnOk := .t.
        if !empty(PMSAcknow)
            EndTrnOk := .f.
           WaitPeriod(nDelay)
           do while (EndAck <> PMSAcknow) .and. WaitPeriod()
                EndAck := oSocket:OnRead()    // read Chr from IP
            end

           if (EndAck = PMSAcknow)   // .or. (right(EndThai,len(PMSAcknow)) = PMSAcknow) // PMS Acknowledge for End of Text
                EndTrnOk := .T.
            end

            if EndTrnOk  // Start send End Trn
            if !empty( PMSETrnMsn )
                SendData( PMSETrnMsn )
                end
           end
        end    
    end    
   
   
   // PABX Respond
   if EndTrnOk
        StartAck2 := ''
        WaitPeriod(3000)  // Loop for 5 seconds
        do while (right(StartAck2,len(PMSSTrnMsn)) <> PMSSTrnMsn) .and. WaitPeriod()    // Wait for PMS Acknowledge Chr
            StartAck2 := oSocket:OnRead()     // read Chr from comm port
        end

        if StartAck2 = PMSSTrnMsn
            if SendData( PMSAcknow ) > 0     // Send Acknowledge Chr
            PABXText := PMSAcknow
            end

            // wait for Data Link Recovery  L70070
            StartAck2 := ''
            WaitPeriod(3000)  // Loop for 5 seconds
            do while left(right(StartAck2,2),1) <> Chr(3) .and. WaitPeriod()   // Wait for PMS Acknowledge Chr
            StartAck2 := oSocket:OnRead()    // read Chr from comm port
            // if ChrIn = Chr(3) .and. 'L1450' $ StartAck2
            if StartAck2=STX+SA+UA+'L70070  '+ETX
                    exit
            end
            end

            if subs(StartAck2,1,8) == subs(PMSChkPBX,1,8)

            SendData( PMSAcknow ) > 0   // Send Acknowledge Chr

            // wait for EOT
            StartAck2 := ''
            WaitPeriod(3000)  // Loop for 5 seconds
            do while (StartAck2 <> PMSETrnMsn) .and. WaitPeriod()   // Wait for PMS Acknowledge Chr
                    StartAck2 := oSocket:OnRead()    // read Chr from comm port
            end
            end
        end
   end
end

if 'L1450' $ PABXText
else
   PABXText := ''
end 
   
   
end

lRun := !lRun

return nil

// (MEMVAR->lRun := .T., oWnd:SetMsg("Connected!") )
*-------------------------*
function OnRead( oSocket )
local cAnswer := oSocket:GetData()
if !empty(cAnswer)
    SaveLog( 'NEC: '+cAnswer )
end  
return cAnswer

Any help will be appreicate.
Regards,
Dutch
Regards,
Dutch

FWH 19.01 / xHarbour Simplex 1.2.3 / BCC73 / Pelles C / UEStudio
FWPPC 10.02 / Harbour for PPC (FTDN)
ADS V.9 / MySql / MariaDB
R&R 12 Infinity / Crystal Report XI R2
(Thailand)
User avatar
dutch
 
Posts: 1535
Joined: Fri Oct 07, 2005 5:56 pm
Location: Thailand

Re: Big problem : oSocket:OnRead and oSocket:bRead ?

Postby Antonio Linares » Wed Dec 02, 2009 8:48 am

Dutch,

bRead is evaluated when new data arrives, then OnRead() reads the arrived data.

So the sequence is:

Event (new data arrives) --> We proceed to read the arrived data --> once all expected data has arrived then you may send an acknowledge
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41324
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Big problem : oSocket:OnRead and oSocket:bRead ?

Postby dutch » Wed Dec 02, 2009 11:46 am

Dear Antonio,

I don't know what I understand is correct or not?

case 1. when unexpected data, We will use ::bRead routine. ??
case 2. when expected data, (after we send request *inquiry*) and it will send data back. We will use ::OnRead() ??
case 3. on 2nd case expected data will run to :bRead also?

Regards,
Dutch

PS. In the past, this program run via RS232 (Com port).
Regards,
Dutch

FWH 19.01 / xHarbour Simplex 1.2.3 / BCC73 / Pelles C / UEStudio
FWPPC 10.02 / Harbour for PPC (FTDN)
ADS V.9 / MySql / MariaDB
R&R 12 Infinity / Crystal Report XI R2
(Thailand)
User avatar
dutch
 
Posts: 1535
Joined: Fri Oct 07, 2005 5:56 pm
Location: Thailand

Re: Big problem : oSocket:OnRead and oSocket:bRead ?

Postby Antonio Linares » Wed Dec 02, 2009 1:45 pm

Dutch,

You should only read data when Windows tell you so, and it tells it to you invoking bRead.

So you should not wait for new data inside OnRead. That is a common mistake because this process is not properly understood.
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41324
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Big problem : oSocket:OnRead and oSocket:bRead ?

Postby Antonio Linares » Wed Dec 02, 2009 1:49 pm

Dutch,

Imagine this example:

1) I tell you to go somewhere and get an envelope for me.

2) You go there, get the envelope and return it to me.

3) Again I ask you to go for a new envelope. etc

A common error is:

1) I tell you to go somewhere and get an envelope for me.

2) You go and get the envelope, but you remain there waiting for more envelops for me

That is not what I told you :-)
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41324
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 20 guests