Example #1
0
        public async ValueTask Disconnect()
        {
            var c = DataLinkEncoder.EncodeDisconnect().ToSharedData();

            await Write(c);

            _dlc.Remove(this);
            _outPipe.Writer.Complete();
        }
Example #2
0
        private async Task <DataLink> CreateDataLinkAsClientAsync(IPEndPoint remote, Stream stream, DataLinkCollection dlc, ReadOnlyMemory <byte> args)
        {
            using (var cm = DataLinkEncoder.Encode(new DCConnectMessage(dlc.ServiceName, dlc.Name, LocalPort, args)))
            {
                await stream.WriteAsync(cm.Memory);

                await stream.FlushAsync();
            }
            var decoder = new DataLinkDecoder();

            // 1024 is enough for current connect message.
            using (var owner = MemoryPool <byte> .Shared.Rent(1024))
            {
                int count = 0;
                while (true)
                {
                    count += await stream.ReadAsync(owner.Memory.Slice(count));

                    if (decoder.TryParseTransportMessage(new ReadOnlySequence <byte>(owner.Memory.Slice(0, count)), out var tm))
                    {
                        if (tm.Type != DataLinkMessageType.Connect)
                        {
                            stream.Close();
                            return(null);
                        }
                        var connect = decoder.ParseConnect(tm.Content);
                        if (connect.ServiceName != dlc.ServiceName)
                        {
                            stream.Close();
                            return(null);
                        }
                        if (connect.InstanceName == dlc.Name)
                        {
                            stream.Close();
                            return(null);
                        }
                        var moreData = owner.Memory.Slice(tm.Length, count - tm.Length);
                        return(dlc.CreateInternal(remote, stream, decoder, connect, moreData));
                    }
                }
            }
        }
Example #3
0
        internal ValueTask ReplySyncNode(string name, IPAddress address, ushort port)
        {
            var c = DataLinkEncoder.Encode(new DCReplySyncNodeMessage(address, port, name)).ToSharedData();

            return(Write(c));
        }
Example #4
0
        internal ValueTask SyncNode()
        {
            var c = DataLinkEncoder.EncodeSyncNode().ToSharedData();

            return(Write(c));
        }
Example #5
0
        public ValueTask SendMessage(string channel, Memory <byte> payload)
        {
            var sharedData = DataLinkEncoder.Encode(new DCPayloadMessage(channel, payload)).ToSharedData();

            return(Write(sharedData));
        }