Beispiel #1
0
        public static Stream ToStream(Hash hash)
        {
            using (var writer = new ItemStreamWriter(BufferManager.Instance))
            {
                writer.Write((uint)hash.Algorithm);
                writer.Write(hash.Value);

                return(writer.GetStream());
            }
        }
Beispiel #2
0
        public Stream Export(BufferManager bufferManager)
        {
            using (var writer = new ItemStreamWriter(BufferManager.Instance))
            {
                writer.Write((uint)this.Algorithm);
                writer.Write(this.Value);

                return(writer.GetStream());
            }
        }
Beispiel #3
0
        protected override Stream Export(BufferManager bufferManager, int depth)
        {
            using (var writer = new ItemStreamWriter(bufferManager))
            {
                // Comment
                if (this.Comment != null)
                {
                    writer.Write((uint)SerializeId.Comment);
                    writer.Write(this.Comment);
                }
                // ExchangePublicKey
                if (this.ExchangePublicKey != null)
                {
                    writer.Write((uint)SerializeId.ExchangePublicKey);
                    writer.Write(this.ExchangePublicKey.Export(bufferManager));
                }
                // TrustSignatures
                if (this.ProtectedTrustSignatures.Count > 0)
                {
                    writer.Write((uint)SerializeId.TrustSignatures);
                    writer.Write((uint)this.ProtectedTrustSignatures.Count);

                    foreach (var value in this.TrustSignatures)
                    {
                        writer.Write(value.Export(bufferManager));
                    }
                }
                // DeleteSignatures
                if (this.ProtectedDeleteSignatures.Count > 0)
                {
                    writer.Write((uint)SerializeId.DeleteSignatures);
                    writer.Write((uint)this.ProtectedDeleteSignatures.Count);

                    foreach (var value in this.DeleteSignatures)
                    {
                        writer.Write(value.Export(bufferManager));
                    }
                }
                // Tags
                if (this.ProtectedTags.Count > 0)
                {
                    writer.Write((uint)SerializeId.Tags);
                    writer.Write((uint)this.ProtectedTags.Count);

                    foreach (var value in this.Tags)
                    {
                        writer.Write(value.Export(bufferManager));
                    }
                }

                return(writer.GetStream());
            }
        }
Beispiel #4
0
        protected override Stream Export(BufferManager bufferManager, int depth)
        {
            using (var writer = new ItemStreamWriter(bufferManager))
            {
                // Comment
                if (this.Comment != null)
                {
                    writer.Write((uint)SerializeId.Comment);
                    writer.Write(this.Comment);
                }

                return(writer.GetStream());
            }
        }
Beispiel #5
0
        protected override Stream Export(BufferManager bufferManager, int depth)
        {
            using (var writer = new ItemStreamWriter(bufferManager))
            {
                // Type
                if (this.Type != null)
                {
                    writer.Write((uint)SerializeId.Type);
                    writer.Write(this.Type);
                }
                // Tag
                if (this.Tag != null)
                {
                    writer.Write((uint)SerializeId.Tag);
                    writer.Write(this.Tag.Export(bufferManager));
                }
                // CreationTime
                if (this.CreationTime != DateTime.MinValue)
                {
                    writer.Write((uint)SerializeId.CreationTime);
                    writer.Write(this.CreationTime);
                }
                // Metadata
                if (this.Metadata != null)
                {
                    writer.Write((uint)SerializeId.Metadata);
                    writer.Write(this.Metadata.Export(bufferManager));
                }

                // Cash
                if (this.Cash != null)
                {
                    writer.Write((uint)SerializeId.Cash);
                    writer.Write(this.Cash.Export(bufferManager));
                }
                // Certificate
                if (this.Certificate != null)
                {
                    writer.Write((uint)SerializeId.Certificate);
                    writer.Write(this.Certificate.Export(bufferManager));
                }

                return(writer.GetStream());
            }
        }
Beispiel #6
0
        protected override Stream Export(BufferManager bufferManager, int depth)
        {
            using (var writer = new ItemStreamWriter(bufferManager))
            {
                // Uris
                if (this.ProtectedUris.Count > 0)
                {
                    writer.Write((uint)SerializeId.Uris);
                    writer.Write((uint)this.ProtectedUris.Count);

                    foreach (string uri in this.ProtectedUris)
                    {
                        writer.Write(uri);
                    }
                }

                return(writer.GetStream());
            }
        }
            protected override Stream Export(BufferManager bufferManager, int depth)
            {
                using (var writer = new ItemStreamWriter(bufferManager))
                {
                    // Tags
                    if (this.ProtectedTags.Count > 0)
                    {
                        writer.Write((uint)SerializeId.Tags);
                        writer.Write((uint)this.ProtectedTags.Count);

                        foreach (var item in this.ProtectedTags)
                        {
                            writer.Write(item.Export(bufferManager));
                        }
                    }

                    return(writer.GetStream());
                }
            }
Beispiel #8
0
        protected override Stream Export(BufferManager bufferManager, int depth)
        {
            using (var writer = new ItemStreamWriter(bufferManager))
            {
                // Hashes
                if (this.ProtectedHashes.Count > 0)
                {
                    writer.Write((uint)SerializeId.Hashes);
                    writer.Write((uint)this.ProtectedHashes.Count);

                    foreach (var item in this.ProtectedHashes)
                    {
                        writer.Write(HashConveter.ToStream(item));
                    }
                }

                return(writer.GetStream());
            }
        }
Beispiel #9
0
        protected override Stream Export(BufferManager bufferManager, int depth)
        {
            using (var writer = new ItemStreamWriter(bufferManager))
            {
                // Hash
                if (this.Hash != null)
                {
                    writer.Write((uint)SerializeId.Hash);
                    writer.Write(this.Hash.Export(bufferManager));
                }
                // Value
                if (this.Value.Array != null)
                {
                    writer.Write((uint)SerializeId.Value);
                    writer.Write(this.Value.Array, this.Value.Offset, this.Value.Count);
                }

                return(writer.GetStream());
            }
        }
Beispiel #10
0
        protected override Stream Export(BufferManager bufferManager, int depth)
        {
            using (var writer = new ItemStreamWriter(bufferManager))
            {
                // Name
                if (this.Name != null)
                {
                    writer.Write((uint)SerializeId.Name);
                    writer.Write(this.Name);
                }
                // Id
                if (this.Id != null)
                {
                    writer.Write((uint)SerializeId.Id);
                    writer.Write(this.Id);
                }

                return(writer.GetStream());
            }
        }
Beispiel #11
0
        protected override Stream Export(BufferManager bufferManager, int depth)
        {
            using (var writer = new ItemStreamWriter(bufferManager))
            {
                // Depth
                if (this.Depth != 0)
                {
                    writer.Write((uint)SerializeId.Depth);
                    writer.Write((uint)this.Depth);
                }
                // Hash
                if (this.Hash != null)
                {
                    writer.Write((uint)SerializeId.Hash);
                    writer.Write(this.Hash.Export(bufferManager));
                }

                return(writer.GetStream());
            }
        }
Beispiel #12
0
        protected override Stream Export(BufferManager bufferManager, int depth)
        {
            if (depth > 256)
            {
                throw new ArgumentException();
            }

            using (var writer = new ItemStreamWriter(bufferManager))
            {
                // Name
                if (this.Name != null)
                {
                    writer.Write((uint)SerializeId.Name);
                    writer.Write(this.Name);
                }
                // Seeds
                if (this.ProtectedSeeds.Count > 0)
                {
                    writer.Write((uint)SerializeId.Seeds);
                    writer.Write((uint)this.ProtectedSeeds.Count);

                    foreach (var item in this.Seeds)
                    {
                        writer.Write(item.Export(bufferManager));
                    }
                }
                // Boxes
                if (this.ProtectedBoxes.Count > 0)
                {
                    writer.Write((uint)SerializeId.Boxes);
                    writer.Write((uint)this.ProtectedBoxes.Count);

                    foreach (var item in this.Boxes)
                    {
                        writer.Write(item.Export(bufferManager, depth + 1));
                    }
                }

                return(writer.GetStream());
            }
        }
        protected override Stream Export(BufferManager bufferManager, int depth)
        {
            using (var writer = new ItemStreamWriter(bufferManager))
            {
                // Tag
                if (this.Tag != null)
                {
                    writer.Write((uint)SerializeId.Tag);
                    writer.Write(this.Tag.Export(bufferManager));
                }
                // AuthorSignature
                if (this.AuthorSignature != null)
                {
                    writer.Write((uint)SerializeId.AuthorSignature);
                    writer.Write(this.AuthorSignature.Export(bufferManager));
                }
                // CreationTime
                if (this.CreationTime != DateTime.MinValue)
                {
                    writer.Write((uint)SerializeId.CreationTime);
                    writer.Write(this.CreationTime);
                }
                // Cost
                if (this.Cost != null)
                {
                    writer.Write((uint)SerializeId.Cost);
                    writer.Write(this.Cost.Export(bufferManager));
                }
                // Value
                if (this.Value != null)
                {
                    writer.Write((uint)SerializeId.Value);
                    writer.Write(this.Value.Export(bufferManager));
                }

                return(writer.GetStream());
            }
        }
Beispiel #14
0
        private (int, WaitQueue <ResponseInfo>) Send <TArgument>(AmoebaRequestType type, TArgument argument)
        {
            int id    = this.CreateId();
            var queue = new WaitQueue <ResponseInfo>();

            _queueMap.Add(id, queue);

            using (var writer = new ItemStreamWriter(_bufferManager))
            {
                writer.Write((uint)type);
                writer.Write((uint)id);

                Stream valueStream = null;

                if (argument != null)
                {
                    try
                    {
                        valueStream = new BufferStream(_bufferManager);
                        JsonUtils.Save(valueStream, argument);
                    }
                    catch (Exception)
                    {
                        if (valueStream != null)
                        {
                            valueStream.Dispose();
                            valueStream = null;
                        }

                        throw;
                    }
                }

                _messagingManager.Send(new UniteStream(writer.GetStream(), valueStream));
            }

            return(id, queue);
        }
        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));
                }
            }
        }