-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecureSessionLogger.cs
More file actions
106 lines (99 loc) · 5.13 KB
/
SecureSessionLogger.cs
File metadata and controls
106 lines (99 loc) · 5.13 KB
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
99
100
101
102
103
104
105
106
using M9Studio.SecureStream;
using M9Studio.ShadowTalk.Core;
using Newtonsoft.Json.Linq;
using System.Net;
using System.Text;
namespace M9Studio.ShadowTalk.Server
{
public class SecureSessionLogger
{
private SecureSession<IPEndPoint> session;
private Logger logger;
public IPEndPoint RemoteAddress => session.RemoteAddress;
public bool IsLive => session.IsLive;
public SecureSessionLogger(SecureSession<IPEndPoint> session, Logger logger)
{
this.session = session;
this.logger = logger;
}
public byte[] Receive()
{
byte[] buffer;
logger.Log($"SecureSession.Receive() [IPEndPoint {RemoteAddress}]: Ожидаем пакет", Logger.Type.SecureSession_Receive);
try
{
buffer = session.Receive();
}
catch (Exception ex)
{
logger.Log($"SecureSession.Receive() [IPEndPoint {RemoteAddress}]: Ошибка при получении: {ex.Message}", Logger.Type.SecureSession_Receive);
throw new Exception(ex.Message);
}
logger.Log($"SecureSession.Receive() [IPEndPoint {RemoteAddress}]: Получен пакет (hash: {buffer.GetHashCode()} size: {buffer.Length})", Logger.Type.SecureSession_Receive);
return buffer;
}
public string ReceiveString()
{
logger.Log($"SecureSession.ReceiveString() [IPEndPoint {RemoteAddress}]: Ожидаем пакет", Logger.Type.SecureSession_Receive);
string result = Encoding.UTF8.GetString(Receive());
logger.Log($"SecureSession.ReceiveString() [IPEndPoint {RemoteAddress}]: Получен пакет ({result})", Logger.Type.SecureSession_Receive);
return result;
}
public JObject ReceiveJObject()
{
logger.Log($"SecureSession.ReceiveJObject() [IPEndPoint {RemoteAddress}]: Ожидаем пакет", Logger.Type.SecureSession_Receive);
JObject result = JObject.Parse(ReceiveString());
logger.Log($"SecureSession.ReceiveJObject() [IPEndPoint {RemoteAddress}]: Получен пакет ({result})", Logger.Type.SecureSession_Receive);
return result;
}
public bool Send(byte[] buffer)
{
bool result;
logger.Log($"SecureSession.Send(byte[] hash: {buffer.GetHashCode()} size: {buffer.Length}) [IPEndPoint {RemoteAddress}]: Пытаемся отправить пакет", Logger.Type.SecureStream_Send);
try
{
result = session.Send(buffer);
}
catch (Exception ex)
{
logger.Log($"SecureSession.Send(byte[] hash: {buffer.GetHashCode()} size: {buffer.Length}) [IPEndPoint {RemoteAddress}]: Ошибка при отправке: {ex.Message}", Logger.Type.SecureStream_Send);
throw new Exception(ex.Message);
}
logger.Log($"SecureSession.Send(byte[] hash: {buffer.GetHashCode()} size: {buffer.Length}) [IPEndPoint {RemoteAddress}]: Пакет {(result ? "" : "не")} отправлен", Logger.Type.SecureStream_Send);
return result;
}
public bool Send(string data)
{
logger.Log($"SecureSession.Send(string {data.GetHashCode()}) [IPEndPoint {RemoteAddress}]: Пытаемся отправить пакет ({data})", Logger.Type.SecureStream_Send);
bool result = Send(Encoding.UTF8.GetBytes(data));
logger.Log($"SecureSession.Send(string {data.GetHashCode()}) [IPEndPoint {RemoteAddress}]: Пакет {(result ? "" : "не")} отправлен", Logger.Type.SecureStream_Send);
return result;
}
public bool Send(JObject data)
{
logger.Log($"SecureSession.Send(JObject {data.GetHashCode()}) [IPEndPoint {RemoteAddress}]: Пытаемся отправить пакет ({data})", Logger.Type.SecureStream_Send);
bool result = Send(data.ToString());
logger.Log($"SecureSession.Send(JObject {data.GetHashCode()}) [IPEndPoint {RemoteAddress}]: Пакет {(result ? "" : "не")} отправлен", Logger.Type.SecureStream_Send);
return result;
}
public bool Send(PacketStruct data)
{
logger.Log($"SecureSession.Send(PacketStruct {data.GetHashCode()}) [IPEndPoint {RemoteAddress}]: Пытаемся отправить пакет ({data})", Logger.Type.SecureStream_Send);
bool result = Send(data.ToJObject());
logger.Log($"SecureSession.Send(PacketStruct {data.GetHashCode()}) [IPEndPoint {RemoteAddress}]: Пакет {(result ? "" : "не")} отправлен", Logger.Type.SecureStream_Send);
return result;
}
public override int GetHashCode()
{
return RemoteAddress.GetHashCode();
}
public override bool Equals(object? obj)
{
if (obj is SecureSessionLogger)
{
return RemoteAddress == ((SecureSessionLogger)obj).RemoteAddress;
}
return false;
}
}
}