public override void SendAsyncBlocking (RequestMessage request) { Exception ex = null; try { SendRequest (request); } catch (IOException e) { ex = e; } catch (SocketException e) { ex = e; } if (ex != null) { ResponseMessage resp = new ErrorResponse (ex); this.InvokeAsyncResponseEvent (resp); return; } Stream stream = this.http_request.GetResponse ().GetResponseStream (); MemoryStream deserialize_stream = new MemoryStream (); // This buffer is annoyingly small on purpose, to avoid // having to deal with the case of multiple messages // in a single block. byte [] buffer = new byte [32]; while (! this.IsClosed) { Array.Clear (buffer, 0, buffer.Length); int bytes_read; bytes_read = stream.Read (buffer, 0, buffer.Length); if (bytes_read == 0) break; int end_index; end_index = Array.IndexOf<byte> (buffer, (byte) 0xff); if (end_index == -1) { deserialize_stream.Write (buffer, 0, bytes_read); } else { deserialize_stream.Write (buffer, 0, end_index); deserialize_stream.Seek (0, SeekOrigin.Begin); #if ENABLE_XML_DUMP StreamReader r = new StreamReader (deserialize_stream); Logger.Log.Debug ("Received response:\n{0}\n", r.ReadToEnd ()); deserialize_stream.Seek (0, SeekOrigin.Begin); #endif ResponseMessage resp; try { ResponseWrapper wrapper; wrapper = (ResponseWrapper) resp_serializer.Deserialize (deserialize_stream); resp = wrapper.Message; } catch (Exception e) { resp = new ErrorResponse (e); } this.InvokeAsyncResponseEvent (resp); deserialize_stream.Close (); deserialize_stream = new MemoryStream (); if (bytes_read - end_index - 1 > 0) deserialize_stream.Write (buffer, end_index+1, bytes_read-end_index-1); } } }
public ResponseMessageException (ErrorResponse response) : base (response.ErrorMessage) { this.details = response.Details; }
protected override void ReadCallback (IAsyncResult result) { if (this.IsClosed) return; try { Stream stream = (Stream) result.AsyncState; int bytes_read = 0; try { bytes_read = stream.EndRead (result); } catch (SocketException) { Logger.Log.Debug ("Caught SocketException in ReadCallback"); Close (); } catch (IOException) { Logger.Log.Debug ("Caught IOException in ReadCallback"); Close (); } // Connection hung up, we're through if (bytes_read == 0) { this.Close (); return; } int end_index = -1; int prev_index = 0; do { // 0xff signifies end of message end_index = Array.IndexOf<byte> (network_data, (byte) 0xff, prev_index); if (end_index > bytes_read) { // I'm not sure how this ever comes to be true, but it does, // even though the array is cleared end_index = -1; } int bytes_count = ((end_index == -1) ? bytes_read : end_index) - prev_index; this.BufferStream.Write (network_data, prev_index, bytes_count); if (end_index != -1) { MemoryStream deserialize_stream = this.BufferStream; this.BufferStream = new MemoryStream (); deserialize_stream.Seek (0, SeekOrigin.Begin); HandleResponse (deserialize_stream); // Move past the end-of-message marker prev_index = end_index + 1; } } while (end_index != -1); // Check to see if we're still connected, and keep // looking for new data if so. if (!this.IsClosed) BeginRead (); } catch (Exception e) { Logger.Log.Error ("Got an exception while trying to read data:"); Logger.Log.Error (e); ResponseMessage resp = new ErrorResponse (e); InvokeAsyncResponseEvent (resp); return; } }
public void SendAsync (RequestMessage request) { Exception exception = null; try { SendRequest (request); } catch (IOException e) { exception = e; } catch (SocketException e) { exception = e; } if (exception == null) { BeginRead (); return; } ResponseMessage response = new ErrorResponse (exception); if (AsyncResponse != null) AsyncResponse (response); }