protected override HttpImplement SetHttpImpl() { var httpImpl = new HttpClientImpl(); httpImpl.EnableDebug = true; return(httpImpl); }
public override async Task Send(HttpClientImpl client, Stream redirectionStream) { try { client.Context.Response.AppendHeader("Content-Type", "text/html"); if (IsReusable) { string cacheKey = "__PAGE____" + (_pageName ?? Server.GetFileName(client.Context.Request.RawUrl)); CacheEntry entry; bool created = Server.Cache.GetOrCreateCache(cacheKey, out entry, () => { return(new List <ChunkBase>()); }); if (created == false) { entry.TokenSource.Token.WaitHandle.WaitOne(); _chunks = entry.GetEntry <List <ChunkBase> >(); } else { try { _chunks = entry.GetEntry <List <ChunkBase> >(); await Render(client); } finally { entry.TokenSource.Cancel(); } } } else { _chunks = new List <ChunkBase>(); await Render(client); } foreach (var chunk in _chunks) { await chunk.Send(client, redirectionStream); } } finally { client.Context.Response.OutputStream.FlushAsync().ContinueWith((t) => { if (SendType == SendTypeEnum.CloseStream) { client.Context.Response.OutputStream.Close(); } } ); } }
public TaskPage() { NoteContainer = null; StatusContainer = null; InitializeComponent(); PointerCursor = new CoreCursor(CoreCursorType.Arrow, 0); HandCursor = new CoreCursor(CoreCursorType.Hand, 1); LoadCursor = new CoreCursor(CoreCursorType.Wait, 2); ClientImpl = new HttpClientImpl(Client); RequestTimer = new Timer(TimerCallback, null, (int)TimeSpan.FromSeconds(30).TotalMilliseconds, Timeout.Infinite); TaskDescriptor = "Looking for tasks..."; }
public override async Task Send(HttpClientImpl client, Stream redirectionStream) { redirectionStream = redirectionStream ?? client.Context.Response.OutputStream; foreach (var data in _staticData) { await redirectionStream.WriteAsync(data, 0, data.Length); } await redirectionStream.FlushAsync(); if (SendType == SendTypeEnum.CloseStream) { redirectionStream.Close(); } }
public virtual async Task Send(HttpClientImpl client, Stream redirectionStream) { try { redirectionStream = redirectionStream ?? client.Context.Response.OutputStream; await InternalSend(client, redirectionStream); await redirectionStream.FlushAsync(); if (SendType == SendTypeEnum.CloseStream) { redirectionStream.Close(); } } catch (Exception ex) { TracingChunksSource.TraceData(TraceEventType.Error, TRACEEVENT_ERROR, ex); throw; } }
public override async Task Send(HttpClientImpl client, Stream redirectionStream) { using (SqlConnection connection = new SqlConnection(_connectionString)) { await connection.OpenAsync(CancelToken); using (SqlCommand command = new SqlCommand(_commandText, connection)) { command.CommandType = System.Data.CommandType.Text; command.CommandTimeout = 60; await StartReader(client, redirectionStream); using (var reader = await command.ExecuteReaderAsync()) { bool read = await reader.ReadAsync(); if (read) { int recordIndex = 0; object[] fieldsFlyWeight = new object[reader.FieldCount]; while (read != false) { var values = reader.GetValues(fieldsFlyWeight); recordIndex++; await ProcessRecord(client, redirectionStream, recordIndex, fieldsFlyWeight); read = await reader.ReadAsync(); } } } await StopReader(client, redirectionStream); } } }
protected override async Task ProcessRecord(HttpClientImpl client, System.IO.Stream redirectionStream, int recordIndex, object[] values) { redirectionStream = (redirectionStream ?? client.Context.Response.OutputStream); await redirectionStream.WriteAsync(TR, 0, TR.Length); await redirectionStream.WriteAsync(TD, 0, TD.Length); await redirectionStream.WriteAsync(MACHINE_NAME_BYTES, 0, MACHINE_NAME_BYTES.Length); await redirectionStream.WriteAsync(TD_END, 0, TD_END.Length); foreach (var value in values) { await redirectionStream.WriteAsync(TD, 0, TD.Length); var valueBytes = System.Text.Encoding.UTF8.GetBytes(value.ToString()); await redirectionStream.WriteAsync(valueBytes, 0, valueBytes.Length); await redirectionStream.WriteAsync(TD_END, 0, TD_END.Length); } await redirectionStream.WriteAsync(TR_END, 0, TR_END.Length); }
private async Task Render(HttpClientImpl client) { FileChunk fileChunk = new FileChunk(Server); fileChunk.SendType = SendTypeEnum.None; PageDescription description = null; using (MemoryStream fileContents = new MemoryStream()) { await fileChunk.Send(client, fileContents); fileContents.Position = 0; using (StreamReader reader = new StreamReader(fileContents)) { var data = await reader.ReadToEndAsync(); description = await JsonConvert.DeserializeObjectAsync <PageDescription>(data); } } BuildChunks(Server, _chunks, description); }
public override async Task Send(HttpClientImpl client, Stream redirectionStream) { var context = client.Context; string cacheName = (_rawUrl ?? context.Request.RawUrl); CacheEntry cacheEntry = Server.Cache.GetCache(cacheName); if (redirectionStream == null) { redirectionStream = client.Context.Response.OutputStream; } if (cacheEntry != null) { cacheEntry.TokenSource.Token.WaitHandle.WaitOne(); for (int i = 0; i < cacheEntry.GetEntry <List <byte[]> >().Count; i++) { byte[] byteArray = cacheEntry.GetEntry <List <byte[]> >()[i]; await redirectionStream.WriteAsync(byteArray, 0, byteArray.Length); } } else { string diskName = Server.GetFileName(cacheName); if (File.Exists(diskName)) { bool created = Server.Cache.GetOrCreateCache(cacheName, out cacheEntry, () => { return(new List <byte[]>()); }); if (created == false) { bool timeOut = cacheEntry.TokenSource.Token.WaitHandle.WaitOne(); for (int i = 0; i < cacheEntry.GetEntry <List <byte[]> >().Count; i++) { byte[] byteArray = cacheEntry.GetEntry <List <byte[]> >()[i]; await redirectionStream.WriteAsync(byteArray, 0, byteArray.Length); } } else { try { using (FileStream fs = new FileStream(diskName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { byte[] buffer = new byte[1024]; int read = 0; while ((read = await fs.ReadAsync(buffer, 0, buffer.Length)) != 0) { byte[] bufferEntry = new byte[read]; Buffer.BlockCopy(buffer, 0, bufferEntry, 0, read); cacheEntry.GetEntry <List <byte[]> >().Add(bufferEntry); await redirectionStream.WriteAsync(bufferEntry, 0, bufferEntry.Length); } } } catch (Exception ex) { throw ex; } finally { cacheEntry.TokenSource.Cancel(); } } } else { byte[] buffer = Server.GetStatusCode(404); await redirectionStream.WriteAsync(buffer, 0, buffer.Length); } } await redirectionStream.FlushAsync(); if (SendType == SendTypeEnum.CloseStream) { redirectionStream.Close(); } }
protected virtual async Task ProcessRecord(HttpClientImpl client, Stream redirectionStream, int recordIndex, object[] values) { }
protected virtual async Task StartReader(HttpClientImpl client, Stream redirectionStream) { }
public override async Task Send(HttpClientImpl client, Stream redirectionStream) { if (_initialised == false) { await _initTask; } if (redirectionStream == null) { redirectionStream = client.Context.Response.OutputStream; } try { List <Task> sendTasks = new List <Task>(); List <Task> continueTasks = new List <Task>(); CancellationTokenSource source = new CancellationTokenSource(); try { for (int i = 0; i < _chunks.Count; i++) { var chunk = _chunks[i]; MemoryStream outputTo = new MemoryStream(); Task sendTask = chunk.Send(client, outputTo); if (sendTask.Status == TaskStatus.Faulted) { Debug.Assert(chunk.GetType().Name.Contains("CsvChunk")); } sendTasks.Add(sendTask); var continueTask = sendTask.ContinueWith( (previousTask) => { try { if (source.IsCancellationRequested == false) { using (outputTo) { if (previousTask.Status == TaskStatus.RanToCompletion) { outputTo.Position = 0; lock (redirectionStream) { outputTo.CopyTo(redirectionStream); } } } } else { Debug.WriteLine("Cancelled"); } } catch (Exception ex) { source.Cancel(true); } }, source.Token, TaskContinuationOptions.None, TaskScheduler.Current ); continueTasks.Add(continueTask); } foreach (var task in sendTasks) { await task; } foreach (var task in continueTasks) { await task; } TracingChunksSource.TraceEvent(TraceEventType.Verbose, TRACEEVENT_OK, "ParallelChunk"); //bool allCompleted = Task.WaitAll(tasks.ToArray(), 60000, source.Token); //if(allCompleted == false) //{ // TracingChunksSource.TraceEvent(TraceEventType.Warning, TRACEEVENT_TIMEOUT, client.Context.Request.Url); //} } catch (AggregateException aex) { source.Cancel(true); foreach (var ex in aex.InnerExceptions) { Debug.WriteLine(ex.Message); } } catch (OperationCanceledException opcex) { Debug.WriteLine(opcex.Message); } catch (Exception ex) { source.Cancel(true); Debug.WriteLine(ex.Message); } } finally { } }
protected override async Task InternalSend(HttpClientImpl client, System.IO.Stream outputStream) { int read = 0; List <byte[]> dataSet = new List <byte[]>(); byte[] buffer = new byte[1024]; int totalRead = 0; while ((read = await client.Context.Request.InputStream.ReadAsync(buffer, 0, buffer.Length)) != 0) { if (read > 0) { totalRead += read; byte[] data = new byte[read]; Buffer.BlockCopy(buffer, 0, data, 0, read); dataSet.Add(data); } } byte[] packet = new byte[totalRead]; int packetOffset = 0; foreach (var item in dataSet) { Buffer.BlockCopy(item, 0, packet, packetOffset, item.Length); packetOffset += item.Length; } string serializedJsonRequest = System.Text.Encoding.UTF8.GetString(packet); JSONMessageProcessorBase.Payload payload = new JSONMessageProcessorBase.Payload() { JSON = serializedJsonRequest, Cookies = client.Context.Request.Cookies, AcceptTypes = client.Context.Request.AcceptTypes, Method = client.Context.Request.HttpMethod, Url = client.Context.Request.Url, }; if (client.Context.User != null && client.Context.User.Identity != null) { payload.User = client.Context.User.Identity.Name; payload.IsAuthenticated = client.Context.User.Identity.IsAuthenticated; } var parameters = client.Context.Request.RawUrl.Split('/'); string action = parameters[1]; var localHandler = (IJSONMessageProcessor)Root.ServiceBusInstance.GetMessageProcessor(action); object responseObject = null; object outputRequest; if (localHandler.CanHandleRequest(payload, out outputRequest)) { responseObject = localHandler.HandleRequest(outputRequest); } else { ServiceBusMessage message = new ServiceBusMessage(action, packet, DataFormatEnum.JSON); var responseReference = new Reference <ReplyTask <CacheResponse> >(); await Root.ServiceBusInstance.SendAsync(message, responseReference); responseObject = await responseReference.Item; } string serializedResponseObject = null; if (responseObject is string) { serializedResponseObject = (string)responseObject; } else { serializedResponseObject = await JsonConvert.SerializeObjectAsync(responseObject); } var utf8Encoding = new System.Text.UTF8Encoding(false); byte[] encodingBuffer = new byte[2048]; int numCharsToRead = Math.Min(1024, serializedResponseObject.Length); int stringPosition = 0; int totalCharsRead = 0; while (stringPosition < serializedResponseObject.Length) { numCharsToRead = Math.Min(serializedResponseObject.Length - stringPosition, 1024); int encodedBytes = utf8Encoding.GetBytes(serializedResponseObject, stringPosition, numCharsToRead, encodingBuffer, 0); stringPosition += numCharsToRead; await outputStream.WriteAsync(encodingBuffer, 0, encodedBytes); } }
protected override async Task StopReader(HttpClientImpl client, System.IO.Stream redirectionStream) { redirectionStream = (redirectionStream ?? client.Context.Response.OutputStream); await redirectionStream.WriteAsync(TABLE_END, 0, TABLE_END.Length); }
protected override async Task InternalSend(HttpClientImpl client, Stream outputStream) { try { var sendCount = Interlocked.Increment(ref _sendCount); TracingChunksSource.TraceInformation("SendCount: {0}", sendCount); CsvProcessor.Payload payload = new App.Messaging.CsvProcessor.Payload() { IsRequest = true, Sql = _sql }; var payloadData = CsvProcessor.Payload.Serialize(payload); var peerInfo = Root.ServiceBusInstance.GetLocalService <IPeerManager>().FindRandomPeerForAddress("csvHandler"); if (peerInfo == null) { throw new Exception("No peer was found."); } ServiceBusMessage message = new ServiceBusMessage("csvHandler", payloadData, DataFormatEnum.BinaryNet, peerInfo.Name); var replyReference = new Reference <ReplyTask <App.Messaging.CsvProcessor.Payload> >(); TimeSpan started = Watch.Elapsed; Root.ServiceBusInstance.SendAsync(message, replyReference); await outputStream.WriteAsync(TextAreaStart, 0, TextAreaStart.Length); byte[] timeData = System.Text.Encoding.UTF8.GetBytes(DateTime.UtcNow.ToString()); await outputStream.WriteAsync(timeData, 0, timeData.Length); await outputStream.WriteAsync(NewLine, 0, NewLine.Length); try { var csvResult = await replyReference.Item; var ended = Watch.Elapsed; TimeSpan duration = (ended - started); if (duration.TotalSeconds > 1) { TracingChunksSource.TraceInformation(duration.TotalSeconds.ToString("0.000"), "CsvChunk.WaitReplyTime"); } byte[] data = System.Text.Encoding.UTF8.GetBytes(replyReference.Item.ReplyMessage.Originator); await outputStream.WriteAsync(data, 0, data.Length); await outputStream.WriteAsync(NewLine, 0, NewLine.Length); foreach (var line in csvResult.Result) { data = System.Text.Encoding.UTF8.GetBytes(line); await outputStream.WriteAsync(data, 0, data.Length); await outputStream.WriteAsync(NewLine, 0, NewLine.Length); } } catch (OperationCanceledException ex) { var data = System.Text.Encoding.UTF8.GetBytes(ex.Message); outputStream.Write(data, 0, data.Length); } await outputStream.WriteAsync(TextAreaEnd, 0, TextAreaEnd.Length); } catch (Exception ex) { TracingChunksSource.TraceData(TraceEventType.Error, TRACEEVENT_ERROR, ex); } }
protected virtual async Task InternalSend(HttpClientImpl client, Stream outputStream) { }
protected override async Task InternalSend(HttpClientImpl client, System.IO.Stream outputStream) { int read = 0; List <byte[]> dataSet = new List <byte[]>(); byte[] buffer = new byte[1024]; int totalRead = 0; while ((read = await client.Context.Request.InputStream.ReadAsync(buffer, 0, buffer.Length)) != 0) { if (read > 0) { totalRead += read; byte[] data = new byte[read]; Buffer.BlockCopy(buffer, 0, data, 0, read); dataSet.Add(data); } } byte[] packet = new byte[totalRead]; int packetOffset = 0; foreach (var item in dataSet) { Buffer.BlockCopy(item, 0, packet, packetOffset, item.Length); packetOffset += item.Length; } string jsonCacheRequest = System.Text.Encoding.UTF8.GetString(packet); var cacheRequest = await JsonConvert.DeserializeObjectAsync <CacheRequest>(jsonCacheRequest); var localHandler = (IJSONMessageProcessor)Root.ServiceBusInstance.GetMessageProcessor(Program.CACHING_MESSAGE_PROCESSOR_KEY); CacheResponse cacheResponse = null; object outputRequest; if (localHandler.CanHandleRequest(cacheRequest, out outputRequest)) { cacheResponse = (CacheResponse)localHandler.HandleRequest(outputRequest); } else { var serializer = Root.ServiceBusInstance.GetLocalService <ISerializer>(); var serializedCacheRequest = serializer.Serialize(cacheRequest); ServiceBusMessage message = new ServiceBusMessage("__CachingMessageProcessor", serializedCacheRequest, DataFormatEnum.BinaryNet); var responseReference = new Reference <ReplyTask <CacheResponse> >(); await Root.ServiceBusInstance.SendAsync(message, responseReference); cacheResponse = await responseReference.Item; } cacheResponse.ErrorDescription = new string('A', 160000); var serializedJson = await JsonConvert.SerializeObjectAsync(cacheResponse); var utf8Encoding = new System.Text.UTF8Encoding(false); byte[] encodingBuffer = new byte[2048]; int numCharsToRead = Math.Min(1024, serializedJson.Length); int stringPosition = 0; int totalCharsRead = 0; while (stringPosition < serializedJson.Length) { numCharsToRead = Math.Min(serializedJson.Length - stringPosition, 1024); int encodedBytes = utf8Encoding.GetBytes(serializedJson, stringPosition, numCharsToRead, encodingBuffer, 0); stringPosition += numCharsToRead; await outputStream.WriteAsync(encodingBuffer, 0, encodedBytes); } }