예제 #1
0
        protected virtual Message BuildMessage(int messageType, byte[] messageData)
        {
            switch (messageType)
            {
            case RequestChunkMessage.Id:

                return(RequestChunkMessage.Parse(messageData));

            case ChunkToProcessMessage.Id:

                return(ChunkToProcessMessage.Parse(messageData));

            case ChunkCompletedMessage.Id:

                return(ChunkCompletedMessage.Parse(messageData));

            default:

                return(null);
            }
        }
예제 #2
0
        public async void HandleConnection(TcpClient client)
        {
            var messageReader        = new MessageReader(client);
            var chunksBeingProcessed = new List <Chunk>();

            try
            {
                while (true)
                {
                    var message = await messageReader.GetMessage();

                    if (message == null)
                    {
                        break; // Client disconnected; remove client from the list of active clients.
                    }

                    switch (message)
                    {
                    case RequestChunkMessage _:
                        var chunkToProcess = ChunksToProcess.Take();
                        chunksBeingProcessed.Add(chunkToProcess);

                        chunkToProcess.IsBeingWorkedOn = true;

                        var networkStream = client.GetStream();

                        var chunkToProcessMessage = new ChunkToProcessMessage(
                            chunkToProcess.Scene.SceneId,
                            chunkToProcess.ChunkId,
                            chunkToProcess.Width,
                            chunkToProcess.Scene.Height,
                            chunkToProcess.Height,
                            chunkToProcess.StartY,
                            chunkToProcess.MaxSubPixelX,
                            chunkToProcess.MaxSubPixelY,
                            chunkToProcess.SubPixelStartX,
                            chunkToProcess.SubPixelStartY,
                            chunkToProcess.SubPixelEndX,
                            chunkToProcess.SubPixelEndY,
                            chunkToProcess.Scene.SceneXml);

                        var messageBytes = chunkToProcessMessage.ToBytes();
                        await networkStream.WriteAsync(messageBytes, 0, messageBytes.Length);

                        break;

                    case ChunkCompletedMessage chunkCompletedMessage:
                        var sceneViewModel = SceneViewModels.SingleOrDefault(x => x.Scene.SceneId == chunkCompletedMessage.SceneId);
                        var chunk          = sceneViewModel?.Scene.Chunks.SingleOrDefault(x => x.ChunkId == chunkCompletedMessage.ChunkId);

                        if (chunk != null)
                        {
                            chunksBeingProcessed.Remove(chunk);

                            chunk.IsBeingWorkedOn = false;
                            chunk.IsCompleted     = true;

                            sceneViewModel.Scene.UpdateChunk(chunk, chunkCompletedMessage.ColorData);
                            sceneViewModel.UpdateChunk(chunk);
                        }

                        break;
                    }
                }
            }
            catch
            {
            }
            finally
            {
                if (Clients.Contains(client))
                {
                    Clients.Remove(client);
                }

                foreach (var chunk in chunksBeingProcessed) // Add any unfinished chunks back into the work queue.
                {
                    ChunksToProcess.Add(chunk);
                }
            }
        }