private static void ReadCallback(IAsyncResult asyncResult) { TranslateBase trans = asyncResult.AsyncState as TranslateBase; // ありえないが、というか駄目だったら素通りしていいの? end待ちのがのこりそうだが // TOOD 細かくチェック int size = trans.async.stream.EndRead(asyncResult); if (size > 0) { // next callback trans.async.memory.Write(trans.async.buffer, 0, size); IAsyncResult result = trans.async.stream.BeginRead(trans.async.buffer, 0, trans.async.buffer.Length, new AsyncCallback(ReadCallback), trans); } else { trans.async.stream.Close(); byte[] mem = trans.async.memory.ToArray(); if (mem != null) { string html = trans.async.encode.GetString(mem); trans.async.memory.Close(); string result = trans.GetTrim(html); trans.OutputText = result; } trans.Result = ConnectionResult.Success; trans.signal.Set(); } }
private static void ResponseCallback(IAsyncResult asyncResult) { TranslateBase trans = asyncResult.AsyncState as TranslateBase; // ありえないが、というか駄目だったら素通りしていいの? end待ちのがのこりそうだが if (trans != null) { System.Net.HttpWebRequest req = trans.Request; if (req != null) { // responce done try { System.Net.HttpWebResponse res = req.EndGetResponse(asyncResult) as System.Net.HttpWebResponse; if (res != null) { System.IO.Stream stream = res.GetResponseStream(); if (stream != null) { //trans.async = new ASyncState( stream, trans.Encode ); trans.async = new ASyncState(stream, Encode); // 継承先の静的プロパティとなる trans.async.Read(trans); } } } catch (System.Net.WebException e) { //System.Windows.Forms.MessageBox.Show( "応答取得に失敗しました" ); trans.Result = ConnectionResult.FailureResponce; trans.signal.Set(); } } } }
void process(object o) { TranslateBase trans = o as TranslateBase; bool ret = this.signal.WaitOne(30 * 1000); // ms if (!ret) { trans.Result = ConnectionResult.FailureTimeOut; } this.AsyncDone(trans); }
public IAsyncResult Read(TranslateBase trans) { IAsyncResult result = stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(ReadCallback), trans); return(result); }