Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type ServerStruct
- hSock as socket
- Address as string 'Address of server
- Port as integer 'Port of server
- Nick as String 'Nickname for bot
- ChanList as String 'List of channels to join
- NSPass as String 'NickServ Password
- OperLogin as String 'Oper login info
- vhostLogin as String 'Login info for your bot's vhost
- BotOPs as String 'Operators of the bot
- WantQuit as uByte 'Do we want the server to disconnect us?
- sInBuff as string 'the actual variable holding the buffer
- iInBuffPos as integer 'how much is on the buffer?
- union
- iInBuffFlags as ubyte 'all below... combined
- type
- bInBuffDiscard :1 as ubyte 'we are discarding all until EOL is found?
- bInBuffDoCheck :1 as ubyte 'there's leftover data to check?
- end type
- end union
- end type
- Function GetData (pServ as ServerStruct, Byref sOut as String) as Integer
- with pServ
- var iBufSz = len(.sInBuff)
- if iBufSz = 0 then 'Init (clear state)
- iBufSz = 8192: .sInBuff = space(iBufSz)
- .iInBuffPos = 0: .iInBuffFlags = 0
- end if
- for iTwice as integer = 0 to 1
- if .bInBuffDiscard then .iInBuffPos=0 'keep discarding
- 'get more data if none in the buffer
- if .iInBuffPos<=0 or .bInBuffDoCheck=0 then
- if hSelect(.hSock) = 0 then sOut="": return 0
- var pBuff = strptr(.sInBuff)+.iInBuffPos 'start from here
- var iResu = hReceive( .hSock , pBuff , iBufSz-.iInBuffPos )
- 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
- if iPosi>1 andalso .sInBuff[iPosi-2] = 13 then iPosi -= 1 '\r\n or \n?
- if iPosi then 'found end of line!
- if .bInBuffDiscard then 'discarding the entire line
- sOut="":.bInBuffDiscard=0
- else 'returning the line...
- 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
- '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
- .bInBuffDoCheck = 0 'nothing to check later
- end if
- return len(sOut)
- else 'not found end of line
- 'filled all of the buffer? yes? then discard until EOL
- if .iInBuffPos = iBufSz then .bInBuffDiscard = 1
- 'if it wasnt checking for extra then just
- if .bInBuffDoCheck=0 then sOut="": return 0
- 'otherwise try again! (now will hReceive)
- .bInBuffDoCheck = 0
- end if
- next iTwice
- 'should never reach here!
- end with
- end function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement