internal DataLink(DataLinkCollection dlc, IPEndPoint remote, Stream stream, IDataLinkHandler handler, DataLinkDecoder decoder, DCConnectMessage connectMessage, ReadOnlyMemory <byte> memory) { _dlc = dlc; _stream = stream; _handler = handler; _decoder = decoder; Remote = remote; RemoteName = connectMessage.InstanceName; RemoteListenPort = connectMessage.Port; _inPipe.Writer.WriteAsync(memory); _inPipe.Writer.FlushAsync(); }
private async Task <DataLink> CreateDataLinkAsServerAsync(IPEndPoint remote, Stream stream) { 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); var dlc = _dlcStore.Get(connect.ServiceName, connect.Args); if (dlc == null) { stream.Close(); return(null); } if (connect.ServiceName != dlc.ServiceName) { stream.Close(); return(null); } if (connect.InstanceName == dlc.Name) { stream.Close(); return(null); } using (var cm = DataLinkEncoder.Encode(new DCConnectMessage(dlc.ServiceName, dlc.Name, LocalPort, default))) { await stream.WriteAsync(cm.Memory); } var moreData = owner.Memory.Slice(tm.Length, count - tm.Length); return(dlc.CreateInternal(remote, stream, decoder, connect, moreData)); } } } }