-
Notifications
You must be signed in to change notification settings - Fork 8
/
TcpHandlerServerAuth.cs
98 lines (93 loc) · 3.61 KB
/
TcpHandlerServerAuth.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using PHS.Networking.Enums;
using PHS.Networking.Events.Args;
using System;
using System.Threading;
using System.Threading.Tasks;
using Tcp.NET.Server.Events.Args;
using Tcp.NET.Server.Models;
namespace Tcp.NET.Server.Handlers
{
public class TcpHandlerServerAuth<T> :
TcpHandlerServerAuthBase<
TcpConnectionServerAuthEventArgs<T>,
TcpMessageServerAuthEventArgs<T>,
TcpErrorServerAuthEventArgs<T>,
IParamsTcpServerAuth,
TcpAuthorizeEventArgs<T>,
IdentityTcpServer<T>,
T>
{
public TcpHandlerServerAuth(IParamsTcpServerAuth parameters) : base(parameters)
{
}
public TcpHandlerServerAuth(IParamsTcpServerAuth parameters, byte[] certificate, string certificatePassword) : base(parameters, certificate, certificatePassword)
{
}
public override Task AuthorizeCallbackAsync(TcpAuthorizeBaseEventArgs<IdentityTcpServer<T>, T> args, CancellationToken cancellationToken)
{
FireEvent(this, new TcpConnectionServerAuthEventArgs<T>
{
ConnectionEventType = ConnectionEventType.Connected,
Connection = args.Connection,
CancellationToken = args.CancellationToken
});
return Task.CompletedTask;
}
protected override IdentityTcpServer<T> CreateConnection(ConnectionTcpServer connection)
{
return new IdentityTcpServer<T>
{
TcpClient = connection.TcpClient,
ConnectionId = Guid.NewGuid().ToString(),
SslStream = connection.SslStream,
ReadBuffer = connection.ReadBuffer
};
}
protected override TcpConnectionServerAuthEventArgs<T> CreateConnectionEventArgs(ConnectionEventArgs<IdentityTcpServer<T>> args)
{
return new TcpConnectionServerAuthEventArgs<T>
{
Connection = args.Connection,
ConnectionEventType = args.ConnectionEventType,
CancellationToken = args.CancellationToken
};
}
protected override TcpErrorServerAuthEventArgs<T> CreateErrorEventArgs(ErrorEventArgs<IdentityTcpServer<T>> args)
{
return new TcpErrorServerAuthEventArgs<T>
{
Connection = args.Connection,
Exception = args.Exception,
Message = args.Message,
CancellationToken = args.CancellationToken
};
}
protected override TcpMessageServerAuthEventArgs<T> CreateMessageEventArgs(TcpMessageServerBaseEventArgs<IdentityTcpServer<T>> args)
{
return new TcpMessageServerAuthEventArgs<T>
{
Bytes = args.Bytes,
Connection = args.Connection,
Message = args.Message,
MessageEventType = args.MessageEventType,
CancellationToken = args.CancellationToken
};
}
protected override TcpAuthorizeEventArgs<T> CreateAuthorizeEventArgs(TcpAuthorizeBaseEventArgs<IdentityTcpServer<T>, T> args)
{
return new TcpAuthorizeEventArgs<T>
{
Connection = args.Connection,
Token = args.Token,
CancellationToken = args.CancellationToken
};
}
protected override void FireEvent(object sender, TcpConnectionServerAuthEventArgs<T> args)
{
if (args.Connection.IsAuthorized)
{
base.FireEvent(sender, args);
}
}
}
}