protected override void ProtectedImport(Stream stream, BufferManager bufferManager, int depth) { using (var reader = new ItemStreamReader(stream, bufferManager)) { while (reader.Available > 0) { int id = (int)reader.GetUInt32(); if (id == (int)SerializeId.CorrectionAlgorithm) { this.CorrectionAlgorithm = (CorrectionAlgorithm)reader.GetByte(); } else if (id == (int)SerializeId.Length) { this.Length = (long)reader.GetUInt64(); } else if (id == (int)SerializeId.Hashes) { for (int i = (int)reader.GetUInt32() - 1; i >= 0; i--) { this.ProtectedHashes.Add(Hash.Import(reader.GetStream(), bufferManager)); } } } } }
protected override void ProtectedImport(Stream stream, BufferManager bufferManager, int depth) { using (var reader = new ItemStreamReader(stream, bufferManager)) { while (reader.Available > 0) { int id = (int)reader.GetUInt32(); if (id == (int)SerializeId.TargetSignature) { this.TargetSignature = Signature.Import(reader.GetStream(), bufferManager); } else if (id == (int)SerializeId.AuthorSignature) { this.AuthorSignature = Signature.Import(reader.GetStream(), bufferManager); } else if (id == (int)SerializeId.CreationTime) { this.CreationTime = reader.GetDateTime(); } else if (id == (int)SerializeId.Value) { this.Value = ItemBase <T> .Import(reader.GetStream(), bufferManager); } } } }
protected override void ProtectedImport(Stream stream, BufferManager bufferManager, int depth) { using (var reader = new ItemStreamReader(stream, bufferManager)) { while (reader.Available > 0) { int id = (int)reader.GetUInt32(); if (id == (int)SerializeId.Type) { this.Type = reader.GetString(); } else if (id == (int)SerializeId.CreationTime) { this.CreationTime = reader.GetDateTime(); } else if (id == (int)SerializeId.Metadata) { this.Metadata = Metadata.Import(reader.GetStream(), bufferManager); } else if (id == (int)SerializeId.Certificate) { this.Certificate = Certificate.Import(reader.GetStream(), bufferManager); } } } }
protected override void ProtectedImport(Stream stream, BufferManager bufferManager, int depth) { if (depth > 256) { throw new ArgumentException(); } using (var reader = new ItemStreamReader(stream, bufferManager)) { while (reader.Available > 0) { int id = (int)reader.GetUInt32(); if (id == (int)SerializeId.Name) { this.Name = reader.GetString(); } else if (id == (int)SerializeId.Seeds) { for (int i = (int)reader.GetUInt32() - 1; i >= 0; i--) { this.ProtectedSeeds.Add(Seed.Import(reader.GetStream(), bufferManager)); } } else if (id == (int)SerializeId.Boxes) { for (int i = (int)reader.GetUInt32() - 1; i >= 0; i--) { this.ProtectedBoxes.Add(Box.Import(reader.GetStream(), bufferManager, depth + 1)); } } } } }
protected override void ProtectedImport(Stream stream, BufferManager bufferManager, int depth) { using (var reader = new ItemStreamReader(stream, bufferManager)) { this.Algorithm = (HashAlgorithm)reader.GetUInt32(); this.Value = reader.GetBytes(); } }
public static Hash FromStream(Stream stream) { using (var reader = new ItemStreamReader(stream, BufferManager.Instance)) { var algorithm = (HashAlgorithm)reader.GetUInt32(); var value = reader.GetBytes(); return(new Hash(algorithm, value)); } }
protected override void ProtectedImport(Stream stream, BufferManager bufferManager, int depth) { using (var reader = new ItemStreamReader(stream, bufferManager)) { while (reader.Available > 0) { int id = (int)reader.GetUInt32(); if (id == (int)SerializeId.Comment) { this.Comment = reader.GetString(); } } } }
private void MessagingManager_ReceiveEvent(Stream responseStream) { using (var reader = new ItemStreamReader(new WrapperStream(responseStream, true), _bufferManager)) { var type = (AmoebaResponseType)reader.GetUInt32(); int id = (int)reader.GetUInt32(); if (_queueMap.TryGetValue(id, out var queue)) { queue.Enqueue(new ResponseInfo() { Type = type, Stream = new RangeStream(responseStream) }); } } }
protected override void ProtectedImport(Stream stream, BufferManager bufferManager, int depth) { using (var reader = new ItemStreamReader(stream, bufferManager)) { while (reader.Available > 0) { int id = (int)reader.GetUInt32(); if (id == (int)SerializeId.Uris) { for (int i = (int)reader.GetUInt32() - 1; i >= 0; i--) { this.ProtectedUris.Add(reader.GetString()); } } } } }
protected override void ProtectedImport(Stream stream, BufferManager bufferManager, int depth) { using (var reader = new ItemStreamReader(stream, bufferManager)) { while (reader.Available > 0) { int id = (int)reader.GetUInt32(); if (id == (int)SerializeId.Hash) { this.Hash = Hash.Import(reader.GetStream(), bufferManager); } else if (id == (int)SerializeId.Value) { using (var rangeStream = reader.GetStream()) { if (this.Value.Array != null) { bufferManager.ReturnBuffer(this.Value.Array); } byte[] buffer = null; try { buffer = bufferManager.TakeBuffer((int)rangeStream.Length); rangeStream.Read(buffer, 0, (int)rangeStream.Length); } catch (Exception e) { if (buffer != null) { bufferManager.ReturnBuffer(buffer); } throw e; } this.Value = new ArraySegment <byte>(buffer, 0, (int)rangeStream.Length); } } } } }
protected override void ProtectedImport(Stream stream, BufferManager bufferManager, int depth) { using (var reader = new ItemStreamReader(stream, bufferManager)) { while (reader.Available > 0) { int id = (int)reader.GetUInt32(); if (id == (int)SerializeId.Depth) { this.Depth = (int)reader.GetUInt32(); } else if (id == (int)SerializeId.Hash) { this.Hash = Hash.Import(reader.GetStream(), bufferManager); } } } }
protected override void ProtectedImport(Stream stream, BufferManager bufferManager, int depth) { using (var reader = new ItemStreamReader(stream, bufferManager)) { while (reader.Available > 0) { int id = (int)reader.GetUInt32(); if (id == (int)SerializeId.Id) { this.Id = reader.GetBytes(); } else if (id == (int)SerializeId.Location) { this.Location = Location.Import(reader.GetStream(), bufferManager); } } } }
protected override void ProtectedImport(Stream stream, BufferManager bufferManager, int depth) { using (var reader = new ItemStreamReader(stream, bufferManager)) { while (reader.Available > 0) { int id = (int)reader.GetUInt32(); if (id == (int)SerializeId.Comment) { this.Comment = reader.GetString(); } else if (id == (int)SerializeId.ExchangePublicKey) { this.ExchangePublicKey = ExchangePublicKey.Import(reader.GetStream(), bufferManager); } else if (id == (int)SerializeId.TrustSignatures) { for (int i = (int)reader.GetUInt32() - 1; i >= 0; i--) { this.ProtectedTrustSignatures.Add(Signature.Import(reader.GetStream(), bufferManager)); } } else if (id == (int)SerializeId.DeleteSignatures) { for (int i = (int)reader.GetUInt32() - 1; i >= 0; i--) { this.ProtectedDeleteSignatures.Add(Signature.Import(reader.GetStream(), bufferManager)); } } else if (id == (int)SerializeId.Tags) { for (int i = (int)reader.GetUInt32() - 1; i >= 0; i--) { this.ProtectedTags.Add(Tag.Import(reader.GetStream(), bufferManager)); } } } } }
private void MessagingManager_ReceiveEvent <TService>(TService serviceManager, Stream requestStream, Action <Stream> sendAction, Action exitAction) where TService : StateManagerBase, IService { using (var reader = new ItemStreamReader(new WrapperStream(requestStream, true), _bufferManager)) { var type = (AmoebaRequestType)reader.GetUInt32(); int id = (int)reader.GetUInt32(); if (type == AmoebaRequestType.Exit) { SendResponse(AmoebaResponseType.Result, id, (object)null); exitAction(); } else if (type == AmoebaRequestType.Cancel) { if (_tasks.TryGetValue(id, out var responseTask)) { responseTask.Stop(); } } else { var responseTask = ResponseTask.Create((token) => { try { switch (type) { case AmoebaRequestType.GetState: { SendResponse(AmoebaResponseType.Result, id, serviceManager.State); break; } case AmoebaRequestType.Start: { serviceManager.Start(); SendResponse(AmoebaResponseType.Result, id, (object)null); break; } case AmoebaRequestType.Stop: { serviceManager.Stop(); SendResponse(AmoebaResponseType.Result, id, (object)null); break; } case AmoebaRequestType.GetReport: { SendResponse(AmoebaResponseType.Result, id, serviceManager.Report); break; } case AmoebaRequestType.GetNetworkConnectionReports: { SendResponse(AmoebaResponseType.Result, id, serviceManager.GetNetworkConnectionReports()); break; } case AmoebaRequestType.GetCacheContentReports: { SendResponse(AmoebaResponseType.Result, id, serviceManager.GetCacheContentReports()); break; } case AmoebaRequestType.GetDownloadContentReports: { SendResponse(AmoebaResponseType.Result, id, serviceManager.GetDownloadContentReports()); break; } case AmoebaRequestType.GetConfig: { SendResponse(AmoebaResponseType.Result, id, serviceManager.Config); break; } case AmoebaRequestType.SetConfig: { var config = JsonUtils.Load <ServiceConfig>(requestStream); serviceManager.SetConfig(config); SendResponse(AmoebaResponseType.Result, id, (object)null); break; } case AmoebaRequestType.SetCloudLocations: { var cloudLocations = JsonUtils.Load <Location[]>(requestStream); serviceManager.SetCloudLocations(cloudLocations); SendResponse(AmoebaResponseType.Result, id, (object)null); break; } case AmoebaRequestType.GetSize: { SendResponse(AmoebaResponseType.Result, id, serviceManager.Size); break; } case AmoebaRequestType.Resize: { long size = JsonUtils.Load <long>(requestStream); serviceManager.Resize(size); SendResponse(AmoebaResponseType.Result, id, (object)null); break; } case AmoebaRequestType.CheckBlocks: { try { serviceManager.CheckBlocks(new Progress <CheckBlocksProgressReport>((report) => { SendResponse(AmoebaResponseType.Output, id, report); }), token).Wait(); } catch (Exception) { } SendResponse(AmoebaResponseType.Result, id, (object)null); break; } case AmoebaRequestType.AddContent: { var arguments = JsonUtils.Load <(string, DateTime)>(requestStream); var result = serviceManager.AddContent(arguments.Item1, arguments.Item2, token).Result; SendResponse(AmoebaResponseType.Result, id, result); break; } case AmoebaRequestType.RemoveContent: { string path = JsonUtils.Load <string>(requestStream); serviceManager.RemoveContent(path); SendResponse(AmoebaResponseType.Result, id, (object)null); break; } case AmoebaRequestType.Diffusion: { string path = JsonUtils.Load <string>(requestStream); serviceManager.Diffusion(path); SendResponse(AmoebaResponseType.Result, id, (object)null); break; } case AmoebaRequestType.AddDownload: { var arguments = JsonUtils.Load <(Metadata, string, long)>(requestStream); serviceManager.AddDownload(arguments.Item1, arguments.Item2, arguments.Item3); SendResponse(AmoebaResponseType.Result, id, (object)null); break; } case AmoebaRequestType.RemoveDownload: { var arguments = JsonUtils.Load <(Metadata, string)>(requestStream); serviceManager.RemoveDownload(arguments.Item1, arguments.Item2); SendResponse(AmoebaResponseType.Result, id, (object)null); break; } case AmoebaRequestType.ResetDownload: { var arguments = JsonUtils.Load <(Metadata, string)>(requestStream); serviceManager.ResetDownload(arguments.Item1, arguments.Item2); SendResponse(AmoebaResponseType.Result, id, (object)null); break; } case AmoebaRequestType.SetProfile: { var arguments = JsonUtils.Load <(Profile, DigitalSignature)>(requestStream); serviceManager.SetProfile(arguments.Item1, arguments.Item2, token).Wait(); SendResponse(AmoebaResponseType.Result, id, (object)null); break; } case AmoebaRequestType.SetStore: { var arguments = JsonUtils.Load <(Store, DigitalSignature)>(requestStream); serviceManager.SetStore(arguments.Item1, arguments.Item2, token).Wait(); SendResponse(AmoebaResponseType.Result, id, (object)null); break; } case AmoebaRequestType.SetMailMessage: { var arguments = JsonUtils.Load <(Signature, MailMessage, ExchangePublicKey, DigitalSignature)>(requestStream); serviceManager.SetMailMessage(arguments.Item1, arguments.Item2, arguments.Item3, arguments.Item4, token).Wait(); SendResponse(AmoebaResponseType.Result, id, (object)null); break; } case AmoebaRequestType.SetChatMessage: { var arguments = JsonUtils.Load <(Tag, ChatMessage, DigitalSignature, TimeSpan)>(requestStream); serviceManager.SetChatMessage(arguments.Item1, arguments.Item2, arguments.Item3, arguments.Item4, token).Wait(); SendResponse(AmoebaResponseType.Result, id, (object)null); break; } case AmoebaRequestType.GetProfile: { var signature = JsonUtils.Load <Signature>(requestStream); var result = serviceManager.GetProfile(signature, token).Result; SendResponse(AmoebaResponseType.Result, id, result); break; } case AmoebaRequestType.GetStore: { var signature = JsonUtils.Load <Signature>(requestStream); var result = serviceManager.GetStore(signature, token).Result; SendResponse(AmoebaResponseType.Result, id, result); break; } case AmoebaRequestType.GetMailMessages: { var arguments = JsonUtils.Load <(Signature, ExchangePrivateKey)>(requestStream); var result = serviceManager.GetMailMessages(arguments.Item1, arguments.Item2, token).Result; SendResponse(AmoebaResponseType.Result, id, result); break; } case AmoebaRequestType.GetChatMessages: { var tag = JsonUtils.Load <Tag>(requestStream); var result = serviceManager.GetChatMessages(tag, token).Result; SendResponse(AmoebaResponseType.Result, id, result); break; } } } catch (Exception e) { SendResponse(AmoebaResponseType.Error, id, e.Message); } finally { _tasks.Remove(id); } }); _tasks.Add(id, responseTask); responseTask.Start(); } } void SendResponse <T>(AmoebaResponseType type, int id, T value) { using (var writer = new ItemStreamWriter(_bufferManager)) { writer.Write((uint)type); writer.Write((uint)id); Stream valueStream = null; if (value != null) { try { valueStream = new BufferStream(_bufferManager); JsonUtils.Save(valueStream, value); } catch (Exception) { if (valueStream != null) { valueStream.Dispose(); valueStream = null; } return; } } sendAction(new UniteStream(writer.GetStream(), valueStream)); } } }