private void DoWork() { try { TcpServer.Start(); while (!ShutdownTokenSource.Token.IsCancellationRequested) { try { var order = TcpServer.RecievedPackets.Take(ShutdownTokenSource.Token); BinaryReader reader = new BinaryReader(order.Value.Request); MemoryStream ms = new MemoryStream(); BinaryWriter writer = new BinaryWriter(ms); var code = (RemoteHeapCommandCodes)reader.ReadByte(); switch (code) { case RemoteHeapCommandCodes.ObtainHandle: ObtainHandleCommand.WriteResponse(writer, Heap.ObtainNewHandle()); break; case RemoteHeapCommandCodes.ReleaseHandle: { var handle = ReleaseHandleCommand.ReadRequest(reader).Handle; Heap.Release(handle); break; } case RemoteHeapCommandCodes.HandleExist: { long handle = HandleExistCommand.ReadRequest(reader).Handle; HandleExistCommand.WriteResponse(writer, Heap.Exists(handle)); break; } case RemoteHeapCommandCodes.WriteCommand: var cmd = WriteCommand.ReadRequest(reader); Heap.Write(cmd.Handle, cmd.Buffer, cmd.Index, cmd.Count); break; case RemoteHeapCommandCodes.ReadCommand: { var handle = ReadCommand.ReadRequest(reader).Handle; ReadCommand.WriteResponse(writer, Heap.Read(handle)); break; } case RemoteHeapCommandCodes.CommitCommand: Heap.Commit(); break; case RemoteHeapCommandCodes.CloseCommand: Heap.Close(); break; case RemoteHeapCommandCodes.SetTag: Heap.Tag = SetTagCommand.ReadRequest(reader).Tag; break; case RemoteHeapCommandCodes.GetTag: GetTagCommand.WriteResponse(writer, Heap.Tag); break; case RemoteHeapCommandCodes.Size: SizeCommand.WriteResponse(writer, Heap.Size); break; case RemoteHeapCommandCodes.DataBaseSize: DataBaseSizeCommand.WriteResponse(writer, Heap.DataSize); break; default: break; } ms.Position = 0; order.Value.Response = ms; order.Key.PendingPackets.Add(order.Value); } catch (OperationCanceledException) { break; } catch (Exception exc) { TcpServer.LogError(exc); } } } catch (Exception exc) { TcpServer.LogError(exc); } finally { TcpServer.Stop(); Worker = null; } }