Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function GetData (pServ as ServerStruct, Byref sOut as String) as Integer
- with pServ
- var iBufSz = len(.sInBuff)
- Print 1
- if iBufSz = 0 then 'Init (clear state)
- iBufSz = 16384: .sInBuff = space(iBufSz)
- .iInBuffPos = 0: .iInBuffFlags = 0
- end if
- for iTwice as integer = 0 to 1
- Print 2
- if .bInBuffDiscard then .iInBuffPos=0 'keep discarding
- 'get more data if none in the buffer
- Print 3
- if .iInBuffPos<=0 or .bInBuffDoCheck=0 then
- Print 4
- if hSelect(.hSock) = 0 then sOut="": return 0
- var pBuff = strptr(.sInBuff)+.iInBuffPos 'start from here
- var iResu = hReceive( .hSock , pBuff , iBufSz-.iInBuffPos )
- Print 5
- if iResu <= 0 then sOut="":return iResu-1 'error/done
- .iInBuffPos += iResu
- end if
- 'check for \n in the buffer
- var iPosi = instr(.sInBuff,chr(10)),iAfter=iPosi
- Print 6
- if iPosi>1 andalso .sInBuff[iPosi-2] = 13 then iPosi -= 1 '\r\n or \n?
- if iPosi then 'found end of line!
- Print 7
- if .bInBuffDiscard then 'discarding the entire line
- Print 8
- sOut="":.bInBuffDiscard=0
- else 'returning the line...
- Print 9
- sOut=left$( .sInBuff , iPosi-1 )
- end if
- .iInBuffPos -= iAfter 'how many are left on the buffer?
- if .iInBuffPos > 0 then 'There's more on buffer
- Print 10
- 'copy to begin of the buffer (using string functions would mess buffer size)
- memmove(strptr(.sInBuff),strptr(.sInBuff)+iAfter,.iInBuffPos)
- .bInBuffDoCheck = 1 'more to check
- else
- Print 11
- .bInBuffDoCheck = 0 'nothing to check later
- end if
- Print 10.1
- return len(sOut)
- else 'not found end of line
- Print 12
- 'filled all of the buffer? yes? then discard until EOL
- Print 13
- if .iInBuffPos = iBufSz then .bInBuffDiscard = 1
- 'if it wasnt checking for extra then just
- Print 14
- if .bInBuffDoCheck=0 then sOut="": return 0
- 'otherwise try again! (now will hReceive)
- .bInBuffDoCheck = 0
- end if
- Print 15
- next iTwice
- 'should never reach here!
- end with
- end function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement