-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGhostNetConnection.cs
More file actions
83 lines (68 loc) · 2.72 KB
/
Copy pathGhostNetConnection.cs
File metadata and controls
83 lines (68 loc) · 2.72 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
using Celeste.Mod;
using Microsoft.Xna.Framework;
using Monocle;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Celeste.Mod.Ghost.Net {
public abstract class GhostNetConnection : IDisposable {
public string Context;
public IPEndPoint ManagementEndPoint;
public IPEndPoint UpdateEndPoint;
public Action<GhostNetConnection, IPEndPoint, GhostNetFrame> OnReceiveManagement;
public Action<GhostNetConnection, IPEndPoint, GhostNetFrame> OnReceiveUpdate;
public Action<GhostNetConnection> OnDisconnect;
public GhostNetConnection() {
// Get the context in which the connection was created.
StackTrace trace = new StackTrace();
foreach (StackFrame frame in trace.GetFrames()) {
MethodBase method = frame.GetMethod();
if (method.IsConstructor)
continue;
Context = method.DeclaringType?.Name;
Context = (Context == null ? "" : Context + "::") + method.Name;
break;
}
}
public abstract void SendManagement(GhostNetFrame frame, bool release);
public abstract void SendUpdate(GhostNetFrame frame, bool release);
public abstract void SendUpdate(GhostNetFrame frame, IPEndPoint remote, bool release);
protected virtual void ReceiveManagement(IPEndPoint remote, GhostNetFrame frame) {
ManagementEndPoint = remote;
try {
OnReceiveManagement?.Invoke(this, remote, frame);
} catch (Exception e) {
Logger.Log(LogLevel.Warn, "ghostnet-con", "Failed handling management frame");
LogContext(LogLevel.Warn);
e.LogDetailed();
}
}
protected virtual void ReceiveUpdate(IPEndPoint remote, GhostNetFrame frame) {
UpdateEndPoint = remote;
try {
OnReceiveUpdate?.Invoke(this, remote, frame);
} catch (Exception e) {
Logger.Log(LogLevel.Warn, "ghostnet-con", "Failed handling update frame");
LogContext(LogLevel.Warn);
e.LogDetailed();
}
}
public void LogContext(LogLevel level) {
Logger.Log(level, "ghostnet-con", $"Context: {Context} {ManagementEndPoint} {UpdateEndPoint}");
}
protected virtual void Dispose(bool disposing) {
OnDisconnect?.Invoke(this);
}
public void Dispose() {
Dispose(true);
}
}
}