/// <summary> /// Returns the whole incoming message. /// </summary> /// <remarks> /// In case the message was sent via multiple frames it waits until all frames are /// collected and then returns the result message. /// </remarks> /// <returns>received message</returns> public byte[] GetWholeMessage() { using (EneterTrace.Entering()) { return(StreamUtil.ReadToEnd(InputStream)); } }
/// <summary> /// Returns the whole response message in bytes. /// </summary> /// <param name="httpResponse">A response received from the HTTP server.</param> /// <returns>The response message in bytes.</returns> public static byte[] GetResponseMessage(this HttpWebResponse httpResponse) { using (EneterTrace.Entering()) { byte[] aResult = null; Stream aWebResponseStream = httpResponse.GetResponseStream(); if (aWebResponseStream != null) { aResult = StreamUtil.ReadToEnd(httpResponse.GetResponseStream()); } return(aResult); } }
public void StoreObject(IStorageService storage, string storageName, object obj) { using (MemoryStream stream = new MemoryStream(BUFF_SIZE)) { m_objectWriteDelegate(stream, obj); stream.Seek(0, SeekOrigin.Begin); byte[] data = StreamUtil.ReadToEnd(stream); if (UseCompression) { data = ZipUtil.ZipArchive(data, m_compression); } storage.WriteStorage(storageName, data); } }
private void DoPolling() { using (EneterTrace.Entering()) { myIsListeningToResponses = true; myListeningToResponsesStartedEvent.Set(); try { // Create URI for polling. string aParameters = "?id=" + myResponseReceiverId; Uri aPollingUri = new Uri(myUri, aParameters); bool aServiceClosedConnection = false; while (!myStopReceivingRequestedFlag && !aServiceClosedConnection) { myStopPollingWaitingEvent.WaitOne(myPollingFrequencyMiliseconds); if (!myStopReceivingRequestedFlag) { // Send poll request to get messages from the service. WebResponse anHttpResponse = HttpWebClient.Get(aPollingUri); // Convert the response to the fast memory stream using (MemoryStream aBufferedResponse = new MemoryStream()) { Stream aResponseStream = anHttpResponse.GetResponseStream(); // Write the incoming response to the fast memory stream. StreamUtil.ReadToEnd(aResponseStream, aBufferedResponse); aBufferedResponse.Position = 0; // The response can contain more messages. while (!myStopReceivingRequestedFlag && !aServiceClosedConnection && aBufferedResponse.Position < aBufferedResponse.Length) { Action <MessageContext> aResponseHandler = myResponseMessageHandler; ProtocolMessage aProtocolMessage = myProtocolFormatter.DecodeMessage((Stream)aBufferedResponse); MessageContext aMessageContext = new MessageContext(aProtocolMessage, myUri.Host); if (aProtocolMessage != null && aProtocolMessage.MessageType == EProtocolMessageType.CloseConnectionRequest) { aServiceClosedConnection = true; } try { if (aResponseHandler != null) { aResponseHandler(aMessageContext); } } catch (Exception err) { EneterTrace.Warning(TracedObject + ErrorHandler.DetectedException, err); } } } } } } catch (WebException err) { string anErrorStatus = err.Status.ToString(); EneterTrace.Error(TracedObject + "detected an error during listening to response messages. Error status: '" + anErrorStatus + "'. ", err); } catch (Exception err) { EneterTrace.Error(TracedObject + ErrorHandler.FailedInListeningLoop, err); } myIsListeningToResponses = false; myListeningToResponsesStartedEvent.Reset(); // If the connection was closed from the service. if (!myStopReceivingRequestedFlag) { Action <MessageContext> aResponseHandler = myResponseMessageHandler; CleanConnection(false); try { aResponseHandler(null); } catch (Exception err) { EneterTrace.Warning(TracedObject + ErrorHandler.DetectedException, err); } } } }
public static byte[] ZipArchive(Stream stream, Compression compression) { return(ZipArchive( StreamUtil.ReadToEnd(stream), compression)); }