Skip to content

Commit 09c542a

Browse files
committed
Add Class:QpStreamClient,QpStreamServerChannel
1 parent 2d77dcf commit 09c542a

File tree

11 files changed

+249
-11
lines changed

11 files changed

+249
-11
lines changed

Quick.Protocol/QpChannel.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,16 @@ protected void BeginReadPackage(CancellationToken token)
10291029
/// <param name="commandExecuterManager"></param>
10301030
public void AddCommandExecuterManager(CommandExecuterManager commandExecuterManager)
10311031
{
1032-
options.CommandExecuterManagerList.Add(commandExecuterManager);
1032+
options.RegisterCommandExecuterManager(commandExecuterManager);
1033+
}
1034+
1035+
/// <summary>
1036+
/// 添加通知处理器管理器
1037+
/// </summary>
1038+
/// <param name="noticeHandlerManager"></param>
1039+
public void AddNoticeHandlerManager(NoticeHandlerManager noticeHandlerManager)
1040+
{
1041+
options.RegisterNoticeHandlerManager(noticeHandlerManager);
10331042
}
10341043

10351044
public async Task<CommandResponseTypeNameAndContent> SendCommand(string requestTypeName, string requestContent, int timeout = 30 * 1000, Action afterSendHandler = null)

Quick.Protocol/QpServer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ internal void RemoveChannel(QpServerChannel channel)
6464

6565
protected void OnNewChannelConnected(Stream stream, string channelName, CancellationToken token)
6666
{
67-
var channel = new QpServerChannel(this, stream, channelName, token, options.Clone());
67+
var channel = new QpServerChannel(stream, channelName, token, options.Clone());
6868

6969
//认证超时
7070
channel.AuchenticateTimeout += (sender, e) =>

Quick.Protocol/QpServerChannel.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ namespace Quick.Protocol
1212
{
1313
public class QpServerChannel : QpChannel
1414
{
15-
private QpServer server;
1615
private Stream stream;
1716
private CancellationTokenSource cts;
1817
private QpServerOptions options;
@@ -22,7 +21,7 @@ public class QpServerChannel : QpChannel
2221
private List<CommandExecuterManager> authedCommandExecuterManagerList = null;
2322
//通过认证后,才允许使用的通知处理器管理器列表
2423
private List<NoticeHandlerManager> authedNoticeHandlerManagerList = null;
25-
24+
2625
public override string ChannelName => channelName;
2726

2827
/// <summary>
@@ -34,9 +33,8 @@ public class QpServerChannel : QpChannel
3433
/// </summary>
3534
internal event EventHandler AuchenticateTimeout;
3635

37-
public QpServerChannel(QpServer server, Stream stream, string channelName, CancellationToken cancellationToken, QpServerOptions options) : base(options)
36+
public QpServerChannel(Stream stream, string channelName, CancellationToken cancellationToken, QpServerOptions options) : base(options)
3837
{
39-
this.server = server;
4038
this.stream = stream;
4139
this.channelName = channelName;
4240
this.options = options;
@@ -64,7 +62,7 @@ public QpServerChannel(QpServer server, Stream stream, string channelName, Cance
6462
BeginNetstat(cts.Token);
6563

6664
//如果认证超时时间后没有通过认证,则断开连接
67-
if (options.AuthenticateTimeout>0)
65+
if (options.AuthenticateTimeout > 0)
6866
Task.Delay(options.AuthenticateTimeout, cts.Token).ContinueWith(t =>
6967
{
7068
//如果已经取消或者已经连接
@@ -74,13 +72,13 @@ public QpServerChannel(QpServer server, Stream stream, string channelName, Cance
7472
if (LogUtils.LogConnection)
7573
LogUtils.Log("[Connection]{0} Authenticate timeout.", channelName);
7674

77-
if (stream!=null)
75+
if (stream != null)
7876
{
7977
try
8078
{
8179
stream.Close();
8280
stream.Dispose();
83-
stream=null;
81+
stream = null;
8482
}
8583
catch { }
8684
}
@@ -117,7 +115,7 @@ private Commands.Authenticate.Response authenticate(QpChannel handler, Commands.
117115
});
118116
throw new CommandException(1, "Authenticate failed.");
119117
}
120-
IsConnected=true;
118+
IsConnected = true;
121119
Auchenticated?.Invoke(this, EventArgs.Empty);
122120
return new Commands.Authenticate.Response();
123121
}
@@ -168,7 +166,6 @@ protected override void OnReadError(Exception exception)
168166
if (exception is ProtocolException)
169167
{
170168
var protocolException = (ProtocolException)exception;
171-
server.RemoveChannel(this);
172169
if (LogUtils.LogConnection)
173170
LogUtils.Log("[ProtocolErrorHandler]{0}: Begin ProtocolErrorHandler invoke...", DateTime.Now);
174171

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Text;
5+
6+
namespace Quick.Protocol.Streams
7+
{
8+
public class InputOutputStream : Stream
9+
{
10+
private Stream input;
11+
private Stream output;
12+
public InputOutputStream(Stream input, Stream output)
13+
{
14+
this.input = input;
15+
this.output = output;
16+
}
17+
18+
public override bool CanRead => true;
19+
20+
public override bool CanSeek => false;
21+
22+
public override bool CanWrite => true;
23+
24+
public override long Length => throw new NotImplementedException();
25+
26+
public override long Position { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
27+
28+
public override void Flush()
29+
{
30+
output.Flush();
31+
}
32+
33+
public override int Read(byte[] buffer, int offset, int count)
34+
{
35+
return input.Read(buffer, offset, count);
36+
}
37+
38+
public override long Seek(long offset, SeekOrigin origin)
39+
{
40+
throw new NotImplementedException();
41+
}
42+
43+
public override void SetLength(long value)
44+
{
45+
throw new NotImplementedException();
46+
}
47+
48+
public override void Write(byte[] buffer, int offset, int count)
49+
{
50+
output.Write(buffer, offset, count);
51+
}
52+
}
53+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Quick.Protocol.Streams
8+
{
9+
public class QpStreamClient : QpClient
10+
{
11+
private QpStreamClientOptions options;
12+
public QpStreamClient(QpStreamClientOptions options) : base(options)
13+
{
14+
this.options = options;
15+
}
16+
17+
protected override Task<Stream> InnerConnectAsync()
18+
{
19+
return Task.FromResult(options.BaseStream);
20+
}
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Text;
5+
6+
namespace Quick.Protocol.Streams
7+
{
8+
public class QpStreamClientOptions : QpClientOptions
9+
{
10+
public Stream BaseStream { get; set; }
11+
12+
public override Type GetQpClientType()
13+
{
14+
return typeof(QpStreamClient);
15+
}
16+
17+
protected override string ToUriBasic(HashSet<string> ignorePropertyNames)
18+
{
19+
return "stream://" + BaseStream.GetType().Name;
20+
}
21+
}
22+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Text;
5+
using System.Threading;
6+
7+
namespace Quick.Protocol.Streams
8+
{
9+
public class QpStreamServerChannel : QpServerChannel
10+
{
11+
public QpStreamServerChannel(QpStreamServerOptions options)
12+
: base(options.BaseStream, options.ChannelName, options.CancellationToken, options)
13+
{
14+
}
15+
}
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Text;
5+
using System.Threading;
6+
7+
namespace Quick.Protocol.Streams
8+
{
9+
public class QpStreamServerOptions : QpServerOptions
10+
{
11+
public Stream BaseStream { get; set; }
12+
public string ChannelName { get; set; }
13+
public CancellationToken CancellationToken { get; set; }
14+
}
15+
}

QuickProtocol_2.0.sln

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QpTestClient", "QpTestClien
4545
EndProject
4646
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Quick.Protocol.AllClients", "Quick.Protocol.AllClients\Quick.Protocol.AllClients.csproj", "{09B8EC78-D2C8-44A7-A493-37C0F05BF580}"
4747
EndProject
48+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Console", "Console", "{8E34D46D-9D1D-40D1-89CD-DCE8EE07F958}"
49+
EndProject
50+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleTest", "Test\ConsoleTest\ConsoleTest.csproj", "{18B3BC1D-D5F9-4D54-81C2-4B4EF3898AF3}"
51+
EndProject
4852
Global
4953
GlobalSection(SolutionConfigurationPlatforms) = preSolution
5054
Debug|Any CPU = Debug|Any CPU
@@ -115,6 +119,10 @@ Global
115119
{09B8EC78-D2C8-44A7-A493-37C0F05BF580}.Debug|Any CPU.Build.0 = Debug|Any CPU
116120
{09B8EC78-D2C8-44A7-A493-37C0F05BF580}.Release|Any CPU.ActiveCfg = Release|Any CPU
117121
{09B8EC78-D2C8-44A7-A493-37C0F05BF580}.Release|Any CPU.Build.0 = Release|Any CPU
122+
{18B3BC1D-D5F9-4D54-81C2-4B4EF3898AF3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
123+
{18B3BC1D-D5F9-4D54-81C2-4B4EF3898AF3}.Debug|Any CPU.Build.0 = Debug|Any CPU
124+
{18B3BC1D-D5F9-4D54-81C2-4B4EF3898AF3}.Release|Any CPU.ActiveCfg = Release|Any CPU
125+
{18B3BC1D-D5F9-4D54-81C2-4B4EF3898AF3}.Release|Any CPU.Build.0 = Release|Any CPU
118126
EndGlobalSection
119127
GlobalSection(SolutionProperties) = preSolution
120128
HideSolutionNode = FALSE
@@ -132,6 +140,8 @@ Global
132140
{434DDF1C-3D83-4B5B-856D-5608C40B58DA} = {4D8B91B9-A589-478E-B579-185AE878E575}
133141
{FCAFBAC5-6C59-47EE-8200-615C967C0475} = {434DDF1C-3D83-4B5B-856D-5608C40B58DA}
134142
{39497212-8529-4418-9D6E-ED9FC546699D} = {434DDF1C-3D83-4B5B-856D-5608C40B58DA}
143+
{8E34D46D-9D1D-40D1-89CD-DCE8EE07F958} = {4D8B91B9-A589-478E-B579-185AE878E575}
144+
{18B3BC1D-D5F9-4D54-81C2-4B4EF3898AF3} = {8E34D46D-9D1D-40D1-89CD-DCE8EE07F958}
135145
EndGlobalSection
136146
GlobalSection(ExtensibilityGlobals) = postSolution
137147
SolutionGuid = {EBE52E31-F20D-4366-8517-584BCD5F79FF}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\..\Quick.Protocol\Quick.Protocol.csproj" />
10+
</ItemGroup>
11+
12+
</Project>

0 commit comments

Comments
 (0)