private void RequestCallback(IAsyncResult asyncResult) { HTTPRequestData state = asyncResult.AsyncState as HTTPRequestData; if (state != null) { Stream requestStream = null; try { requestStream = state.Connection.EndGetRequestStream(asyncResult); } catch { requestStream = null; OnError(TransportErrorCode.LostConnection); } if (requestStream != null) { requestStream.Write(state.Data, 0, state.Data.Length); requestStream.Close(); state.Connection.BeginGetResponse(ResponseCallback, state); } } }
private void StartSend() { if ((State == TransportState.Connected) || (State == TransportState.Connecting)) { if (outbox.CanDequeue) { string appUri = String.Format("http://{0}:{1}/HTTPGateway/requestor", Core.Application.HTTP.Host, Core.Application.HTTP.Port); if (appUri != null) { byte[] toSend = null; byte[] temp = outbox.BeginDequeue(); if (temp != null) { using (MemoryStream mem = new MemoryStream()) { // session ID mem.WriteBytes(sessionID); // payload size mem.WriteShort((short)(temp.Length + PriorityHeaderSize)); // if encrypting, we need another stream if (Core.Network.TransactionalEncryption != null) { byte[] toEncode = null; using (MemoryStream enc = new MemoryStream()) { // priority header (always priority 3 -> highest transactional one) int msgLength = temp.Length; enc.WriteByte((byte)((3 & 0x0F) | (((msgLength) & 0x000F) << 4))); enc.WriteByte((byte)(msgLength >> 4)); // data enc.WriteBytes(temp); // encrypt toEncode = enc.ToArray(); Core.Network.TransactionalEncryption.Encrypt(toEncode, toEncode.Length); } // writing encoded data if (toEncode != null) { mem.WriteBytes(toEncode); } } else { // priority header (always priority 3 -> highest transactional one) int msgLength = temp.Length; mem.WriteByte((byte)((3 & 0x0F) | (((msgLength) & 0x000F) << 4))); mem.WriteByte((byte)(msgLength >> 4)); // data mem.WriteBytes(temp); } toSend = mem.ToArray(); } } else { // sending just session ID toSend = (byte[])sessionID.Clone(); if (isFirst) { isFirst = false; } } if (toSend != null) { HttpWebRequest connection = WebRequest.CreateHttp(appUri); connection.Method = "POST"; // create state object HTTPRequestData state = new HTTPRequestData() { Connection = connection, Data = toSend }; // save state for possible cancellation currentState = state; // start timeout timer timeOutTimer.Start(); // do request connection.BeginGetRequestStream(RequestCallback, state); } } } } }
private void OnRequestEnded() { currentState = null; timeOutTimer.Stop(); }
private void ResponseCallback(IAsyncResult asyncResult) { HTTPRequestData state = asyncResult.AsyncState as HTTPRequestData; if (state != null) { HttpWebResponse response = null; try { response = state.Connection.EndGetResponse(asyncResult) as HttpWebResponse; } catch { response = null; OnError(TransportErrorCode.LostConnection); } if (response != null) { byte[] res = null; if (response.StatusCode == HttpStatusCode.OK) { using (Stream st = response.GetResponseStream()) res = st.ReadAll(); } if (IsValidResponse(res)) { // remove sent message outbox.EndDequeue(); // set state State = TransportState.Connected; // process response Pair <byte[], List <WaveMessage> > results = input.Process(res); if ((results.First != null) && (results.First.Length == HTTPSessionHeaderSize)) { sessionID = (byte[])results.First.Clone(); } if ((results.Second != null) && (results.Second.Count > 0)) { ThreadHelper.Sync( () => { foreach (WaveMessage msg in results.Second) { OnNewMessage(msg); } }); } } else { // restore last message outbox.CancelDequeue(); if (response.StatusCode != HttpStatusCode.OK) { OnError(TransportErrorCode.LostConnection); } } ThreadHelper.Sync( () => { OnRequestEnded(); // if there is data to send - start another send if (outbox.Count > 0) { StartSend(); } }); State = TransportState.Connecting; } } }