Repository navigation
Expand file tree
/
Copy pathClientConnection.cs
More file actions
68 lines (59 loc) · 2.02 KB
/
Copy pathClientConnection.cs
File metadata and controls
68 lines (59 loc) · 2.02 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
using System;
using System.IO;
using System.Net.Sockets;
namespace MiSideMultiplayer.RelayServer
{
internal sealed class ClientConnection : IDisposable
{
private readonly object _writerLock = new object();
private StreamWriter? _writer;
public int Id { get; }
public TcpClient TcpClient { get; }
public string Remote { get; }
// Bound during hello.
public string? ClientId { get; set; }
public string? PlayerName { get; set; }
public string RoomName { get; set; } = "default";
public string? SceneName { get; set; }
// Session role.
public bool IsRoomHost { get; set; }
public string? JoinedRoomId { get; set; }
public bool IsLoadingTimedOut { get; set; }
public ClientConnection(int id, TcpClient tcpClient, string remote)
{
Id = id;
TcpClient = tcpClient;
Remote = remote;
}
public void AttachWriter(StreamWriter writer)
{
lock (_writerLock) _writer = writer;
}
public bool Send(string line)
{
lock (_writerLock)
{
if (_writer == null) return false;
try { _writer.WriteLine(line); return true; }
catch { return false; }
}
}
public bool SendEnvelope(string eventName, string payload)
{
var env = new Envelope
{
RoomId = string.IsNullOrWhiteSpace(JoinedRoomId) ? RoomName : JoinedRoomId,
RoomName = RoomName,
SenderId = "SERVER",
EventName = eventName,
Payload = payload
};
return Send(env.Serialize());
}
public void Dispose()
{
lock (_writerLock) _writer = null;
try { TcpClient.Close(); } catch { }
}
}
}