/// <summary> /// Send the byte array of PCHC message using https transport . /// </summary> /// <param name="httpRequestPayload">The http request payload.</param> /// <returns>The pchc response message.</returns> private RESPONSE_MESSAGE SendByte(byte[] httpRequestPayload) { // Set the timeout of http. int timeout = 20000; byte[] payloadBuffer = null; HttpWebResponse httpWebResponse; this.httpClientTransport.Send(HttpVersion.Version10, null, httpRequestPayload, HttpMethod.POST, timeout); try { httpWebResponse = this.httpClientTransport.Receive(ref payloadBuffer); } catch (WebException e) { if (e.Message.Contains(HttpStatusCode.Unauthorized.ToString())) { throw new HttpStatusCode401Exception(); } else if (e.Message.ToLower().Contains("timed out".ToLower())) { throw new NoRESPONSEMESSAGEException(); } else { // Un expected exception is received. throw; } } this.httpResponseMethod = httpWebResponse.Method; this.httpResponseUri = httpWebResponse.ResponseUri; if (payloadBuffer == null) { throw new NoRESPONSEMESSAGEException(); } try { this.responseMessage = DecodeMessage.DecodeResponseMessage(payloadBuffer); } catch (ArgumentOutOfRangeException e) { throw new NoRESPONSEMESSAGEException(e.Message); } return(this.responseMessage); }
/// <summary> /// Get the request body byte array from the specified http request. /// </summary> /// <param name="httpListenerRequest">The specified http request.</param> /// <exception cref="ObjectDisposedException"> /// Throw when an operation is performed on a disposed object. /// </exception> private void DecomposeHttpRequest(HttpListenerRequest httpListenerRequest) { if (httpListenerRequest == null) { throw new ArgumentNullException("httpListenerRequest"); } this.httpRequestUri = httpListenerRequest.Url; this.httpRequestMethod = httpListenerRequest.HttpMethod; try { Stream requestStream = httpListenerRequest.InputStream; byte[] payloadBuffer = new byte[this.bufferSize]; int readSize = 0; while (true) { int tempIndex = 0; readSize = requestStream.Read(payloadBuffer, 0, payloadBuffer.Length); if (readSize == 0) { break; } else { this.httpRequestPayload = DecodeMessage.GetBytes(payloadBuffer, ref tempIndex, readSize); } } requestStream.Close(); } catch (ObjectDisposedException e) { if (this.logger != null) { this.logger.AddWarning( string.Format("Object disposed exception is catched, detailed information: {0}.", e.Message)); } else { throw; } } if (this.logger != null) { this.logger.AddDebug("Unmarshal PCHC request message is successfully."); } }
/// <summary> /// THe handle for the received http request event. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">THe http request event.</param> private void ReceiveHttpRequest(object sender, HttpRequestEventArg e) { lock (this.locker) { HttpListenerRequest request = e.ListenerContext.Request; this.DecomposeHttpRequest(request); PCHC_MESSAGE_TYPE msgType = DecodeMessage.GetMessageType(this.httpRequestPayload); if (msgType == PCHC_MESSAGE_TYPE.INITIAL_OFFER_MESSAGE) { lock (this.initialQueue) { this.initialQueue.Enqueue(e.ListenerContext); } } else if (msgType == PCHC_MESSAGE_TYPE.SEGMENT_INFO_MESSAGE) { lock (this.segmentQueue) { this.segmentQueue.Enqueue(e.ListenerContext); } } } }
/// <summary> /// Expect the SEGMENT_INFO_MESSAGE request from the client. /// </summary> /// <param name="ipaddress">The expected ipAddress of the remote endpoint which send request.</param> /// <param name="timeout">The waiting timeout.</param> /// <returns>Return the SEGMENT_INFO_MESSAGE received.</returns> /// <exception cref="NoSEGMENTINFOMESSAGEReceivedException"> /// Throw when no SEGMENT_INFO_MESSAGE request from the client. /// </exception> public SEGMENT_INFO_MESSAGE ExpectSegmentInfoMessage(string ipaddress, TimeSpan timeout) { // Make sure the timeout is not less than 1000 milliseconds. if (timeout.TotalMilliseconds < 1000) { if (this.logger != null) { this.logger.AddWarning(string.Format( "The timeout total milliseconds: {0} from the param \"timeout\" is too small.", timeout.TotalMilliseconds)); } // Set the timeout to the default 5000 milliseconds. timeout = TimeSpan.FromMilliseconds(double.Parse("5000")); if (this.logger != null) { this.logger.AddInfo(string.Format( "The timeout total milliseconds from the param \"timeout\" is set to the default value: {0} milliseconds.", timeout.TotalMilliseconds)); } } DateTime startTime = DateTime.Now; while (this.segmentQueue.Count == 0) { // Waiting for the reqest. Thread.Sleep(100); if ((DateTime.Now - startTime).TotalMilliseconds > timeout.TotalMilliseconds) { throw new NoSEGMENTINFOMESSAGEReceivedException(string.Format( "Waiting for {0} milliseconds, no expected SEGMENT_INFO_MESSAGE is received.", timeout.TotalMilliseconds)); } } while (!this.segmentQueue.Peek().Request.RemoteEndPoint.Address.Equals(IPAddress.Parse(ipaddress))) { this.segmentQueue.Dequeue(); if (this.segmentQueue.Count == 0) { throw new InvalidOperationException(); } this.httpRequest = this.segmentQueue.Peek().Request; } try { this.DecomposeHttpRequest(this.httpRequest); } catch (ObjectDisposedException e) { if (this.logger != null) { this.logger.AddWarning( string.Format("Object disposed exception is catched, detailed information: {0}.", e.Message)); } else { throw; } } return(DecodeMessage.DecodeSegmentInfoMessage(this.httpRequestPayload)); }