Core.Object | +--Engine.Actor | +--Engine.Info | +--Engine.InternetInfo | +--IpDrv.InternetLink | +--IpDrv.TcpLink | +--UBrowser.UBrowserBufferedTcpLink
string
CR
CRLF
InputBuffer
LF
OutputBuffer
int
WaitForCountChars
WaitMatchData
WaitResult
float
WaitTimeoutTime
WaitingFor
bool
bWaiting
void
DoBufferQueueIO()
// DoQueueIO is intended to be called from Tick();
GotMatch(int MatchData)
GotMatchTimeout(int MatchData)
ParseDelimited(string Text, string Delimiter, int Count, optional bool)
PeekChar()
// Take a look at the next waiting character, return 0 if no characters waiting
ReadBufferedLine(out string)
ReadChar()
// Read an individual character, returns 0 if no characters waiting
ResetBuffer()
SendBufferedData(string Text)
WaitFor(string What, float TimeOut, int MatchData)
WaitForCount(int Count, float TimeOut, int MatchData)
00001 //============================================================================= 00002 // UBrowserBufferedTcpLink 00003 //============================================================================= 00004 class UBrowserBufferedTcpLink extends TcpLink; 00005 00006 var string InputBuffer; 00007 var string OutputBuffer; 00008 00009 var string CRLF; 00010 var string CR; 00011 var string LF; 00012 00013 var bool bWaiting; 00014 var float WaitTimeoutTime; 00015 var string WaitingFor; 00016 var int WaitForCountChars; // if we're waiting for X bytes 00017 var string WaitResult; 00018 var int WaitMatchData; 00019 00020 function ResetBuffer() 00021 { 00022 InputBuffer = ""; 00023 OutputBuffer = ""; 00024 bWaiting = False; 00025 CRLF = Chr(10)$Chr(13); 00026 CR = Chr(13); 00027 LF = Chr(10); 00028 } 00029 00030 function WaitFor(string What, float TimeOut, int MatchData) 00031 { 00032 bWaiting = True; 00033 WaitingFor = What; 00034 WaitForCountChars = 0; 00035 WaitTimeoutTime = Level.TimeSeconds + TimeOut; 00036 WaitMatchData = MatchData; 00037 WaitResult = ""; 00038 } 00039 00040 function WaitForCount(int Count, float TimeOut, int MatchData) 00041 { 00042 bWaiting = True; 00043 WaitingFor = ""; 00044 WaitForCountChars = Count; 00045 WaitTimeoutTime = Level.TimeSeconds + TimeOut; 00046 WaitMatchData = MatchData; 00047 WaitResult = ""; 00048 } 00049 00050 function GotMatch(int MatchData) 00051 { 00052 // called when a match happens 00053 } 00054 00055 function GotMatchTimeout(int MatchData) 00056 { 00057 // when a match times out 00058 } 00059 00060 function string ParseDelimited(string Text, string Delimiter, int Count, optional bool bToEndOfLine) 00061 { 00062 local string Result; 00063 local int Found, i; 00064 local string s; 00065 00066 Result = ""; 00067 Found = 1; 00068 00069 for(i=0;i<Len(Text);i++) 00070 { 00071 s = Mid(Text, i, 1); 00072 if(InStr(Delimiter, s) != -1) 00073 { 00074 if(Found == Count) 00075 { 00076 if(bToEndOfLine) 00077 return Result$Mid(Text, i); 00078 else 00079 return Result; 00080 } 00081 00082 Found++; 00083 } 00084 else 00085 { 00086 if(Found >= Count) 00087 Result = Result $ s; 00088 } 00089 } 00090 00091 return Result; 00092 } 00093 00094 // Read an individual character, returns 0 if no characters waiting 00095 function int ReadChar() 00096 { 00097 local int c; 00098 00099 if(InputBuffer == "") 00100 return 0; 00101 c = Asc(Left(InputBuffer, 1)); 00102 InputBuffer = Mid(InputBuffer, 1); 00103 return c; 00104 } 00105 00106 // Take a look at the next waiting character, return 0 if no characters waiting 00107 function int PeekChar() 00108 { 00109 local int c; 00110 00111 if(InputBuffer == "") 00112 return 0; 00113 return Asc(Left(InputBuffer, 1)); 00114 } 00115 00116 function bool ReadBufferedLine(out string Text) 00117 { 00118 local int i; 00119 00120 i = InStr(InputBuffer, Chr(13)); 00121 if(i == -1) 00122 return False; 00123 00124 Text = Left(InputBuffer, i); 00125 if(Mid(InputBuffer, i+1, 1) == Chr(10)) 00126 i++; 00127 00128 InputBuffer = Mid(InputBuffer, i+1); 00129 return True; 00130 } 00131 00132 function SendBufferedData(string Text) 00133 { 00134 OutputBuffer = OutputBuffer $ Text; 00135 } 00136 00137 event ReceivedText(string Text) 00138 { 00139 InputBuffer = InputBuffer $ Text; 00140 } 00141 00142 // DoQueueIO is intended to be called from Tick(); 00143 function DoBufferQueueIO() 00144 { 00145 local int i; 00146 00147 while(bWaiting) 00148 { 00149 if(Level.TimeSeconds > WaitTimeoutTime) 00150 { 00151 bWaiting = False; 00152 GotMatchTimeout(WaitMatchData); 00153 } 00154 00155 if(WaitForCountChars > 0) 00156 { 00157 if(Len(InputBuffer) < WaitForCountChars) 00158 break; 00159 00160 WaitResult = Left(InputBuffer, WaitForCountChars); 00161 InputBuffer = Mid(InputBuffer, WaitForCountChars); 00162 bWaiting = False; 00163 GotMatch(WaitMatchData); 00164 } 00165 else 00166 { 00167 i = InStr(InputBuffer, WaitingFor); 00168 if(i == -1 && WaitingFor == CR) 00169 i = InStr(InputBuffer, LF); 00170 if(i != -1) 00171 { 00172 WaitResult = Left(InputBuffer, i + Len(WaitingFor)); 00173 InputBuffer = Mid(InputBuffer, i + Len(WaitingFor)); 00174 bWaiting = False; 00175 GotMatch(WaitMatchData); 00176 } 00177 else 00178 break; 00179 } 00180 } 00181 00182 if(IsConnected()) 00183 { 00184 i = SendText(OutputBuffer); 00185 OutputBuffer = Mid(OutputBuffer, i); 00186 } 00187 } 00188 00189 defaultproperties 00190 { 00191 ReceiveMode=RMODE_Event 00192 }