Advertisement
Ham62

Untitled

Apr 6th, 2016
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function GetData (pServ as ServerStruct, Byref sOut as String) as Integer
  2.   with pServ
  3.     var iBufSz = len(.sInBuff)
  4.     Print 1
  5.     if iBufSz = 0 then 'Init (clear state)
  6.       iBufSz = 16384: .sInBuff = space(iBufSz)
  7.       .iInBuffPos = 0: .iInBuffFlags = 0
  8.     end if        
  9.     for iTwice as integer = 0 to 1
  10.         Print 2
  11.       if .bInBuffDiscard then .iInBuffPos=0 'keep discarding
  12.       'get more data if none in the buffer
  13.       Print 3
  14.       if .iInBuffPos<=0 or .bInBuffDoCheck=0 then
  15.           Print 4
  16.         if hSelect(.hSock) = 0 then sOut="": return 0
  17.         var pBuff = strptr(.sInBuff)+.iInBuffPos 'start from here
  18.         var iResu = hReceive( .hSock , pBuff , iBufSz-.iInBuffPos )
  19.         Print 5
  20.         if iResu <= 0 then sOut="":return iResu-1 'error/done
  21.         .iInBuffPos += iResu
  22.       end if
  23.       'check for \n in the buffer
  24.       var iPosi = instr(.sInBuff,chr(10)),iAfter=iPosi
  25.       Print 6
  26.       if iPosi>1 andalso .sInBuff[iPosi-2] = 13 then iPosi -= 1 '\r\n or \n?
  27.       if iPosi then 'found end of line!
  28.           Print 7
  29.         if .bInBuffDiscard then 'discarding the entire line
  30.             Print 8
  31.           sOut="":.bInBuffDiscard=0
  32.         else 'returning the line...
  33.             Print 9
  34.           sOut=left$( .sInBuff , iPosi-1 )
  35.         end if
  36.         .iInBuffPos -= iAfter 'how many are left on the buffer?
  37.         if .iInBuffPos > 0 then 'There's more on buffer
  38.             Print 10
  39.           'copy to begin of the buffer (using string functions would mess buffer size)
  40.           memmove(strptr(.sInBuff),strptr(.sInBuff)+iAfter,.iInBuffPos)
  41.           .bInBuffDoCheck = 1 'more to check
  42.         else
  43.             Print 11
  44.           .bInBuffDoCheck = 0 'nothing to check later
  45.         end if
  46.         Print 10.1
  47.         return len(sOut)
  48.       else 'not found end of line
  49.           Print 12
  50.         'filled all of the buffer? yes? then discard until EOL
  51.         Print 13
  52.         if .iInBuffPos = iBufSz then .bInBuffDiscard = 1
  53.         'if it wasnt checking for extra then just
  54.         Print 14
  55.         if .bInBuffDoCheck=0 then sOut="": return 0
  56.         'otherwise try again! (now will hReceive)
  57.         .bInBuffDoCheck = 0
  58.       end if  
  59.       Print 15
  60.     next iTwice
  61.     'should never reach here!
  62.   end with
  63. end function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement