public WTree(IHeap heap) { if (heap == null) { throw new NullReferenceException("heap"); } this.heap = heap; if (heap.Exists(HANDLE_SETTINGS)) { //create root branch with dummy handle RootBranch = new Branch(this, NodeType.Leaf, 0); //read settings - settings will set the RootBranch.NodeHandle using (MemoryStream ms = new MemoryStream(heap.Read(HANDLE_SETTINGS))) Settings.Deserialize(this, ms); //read scheme using (MemoryStream ms = new MemoryStream(heap.Read(HANDLE_SCHEME))) scheme = Scheme.Deserialize(new BinaryReader(ms)); ////load branch cache //using (MemoryStream ms = new MemoryStream(Heap.Read(HANDLE_ROOT))) // RootBranch.Cache.Load(this, new BinaryReader(ms)); isRootCacheLoaded = false; } else { //obtain reserved handles var handle = heap.ObtainNewHandle(); if (handle != HANDLE_SETTINGS) { throw new Exception("Logical error."); } scheme = new WaterfallTree.Scheme(); handle = heap.ObtainNewHandle(); if (handle != HANDLE_SCHEME) { throw new Exception("Logical error."); } handle = heap.ObtainNewHandle(); if (handle != HANDLE_ROOT) { throw new Exception("Logical error."); } handle = heap.ObtainNewHandle(); if (handle != HANDLE_RESERVED) { throw new Exception("Logical error."); } RootBranch = new Branch(this, NodeType.Leaf); //the constructor will invoke Heap.ObtainHandle() isRootCacheLoaded = true; } CacheThread = new Thread(DoCache); CacheThread.Start(); }
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; } }
public WTree(IHeap heap) { if (heap == null) throw new NullReferenceException("heap"); this.heap = heap; if (heap.Exists(HANDLE_SETTINGS)) { //create root branch with dummy handle RootBranch = new Branch(this, NodeType.Leaf, 0); //read settings - settings will set the RootBranch.NodeHandle using (MemoryStream ms = new MemoryStream(heap.Read(HANDLE_SETTINGS))) Settings.Deserialize(this, ms); //read scheme using (MemoryStream ms = new MemoryStream(heap.Read(HANDLE_SCHEME))) scheme = Scheme.Deserialize(new BinaryReader(ms)); ////load branch cache //using (MemoryStream ms = new MemoryStream(Heap.Read(HANDLE_ROOT))) // RootBranch.Cache.Load(this, new BinaryReader(ms)); isRootCacheLoaded = false; } else { //obtain reserved handles var handle = heap.ObtainNewHandle(); if (handle != HANDLE_SETTINGS) throw new Exception("Logical error."); scheme = new WaterfallTree.Scheme(); handle = heap.ObtainNewHandle(); if (handle != HANDLE_SCHEME) throw new Exception("Logical error."); handle = heap.ObtainNewHandle(); if (handle != HANDLE_ROOT) throw new Exception("Logical error."); handle = heap.ObtainNewHandle(); if (handle != HANDLE_RESERVED) throw new Exception("Logical error."); RootBranch = new Branch(this, NodeType.Leaf); //the constructor will invoke Heap.ObtainHandle() isRootCacheLoaded = true; } CacheThread = new Thread(DoCache); CacheThread.Start(); }