mirror of
https://github.com/Sanae6/SmoOnlineServer.git
synced 2024-11-22 03:05:16 +00:00
Improving rejoins and other changes
This commit is contained in:
parent
27d2d9acd9
commit
8645db12fa
3 changed files with 57 additions and 36 deletions
|
@ -26,7 +26,7 @@ server.ClientJoined += (c, _) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
c.PacketTransformer += (sender, packet) => {
|
c.PacketTransformer += (_, packet) => {
|
||||||
if (Settings.Instance.Scenario.MergeEnabled && packet is GamePacket gamePacket) {
|
if (Settings.Instance.Scenario.MergeEnabled && packet is GamePacket gamePacket) {
|
||||||
gamePacket.ScenarioNum = (byte?) c.Metadata["scenario"] ?? 0;
|
gamePacket.ScenarioNum = (byte?) c.Metadata["scenario"] ?? 0;
|
||||||
return gamePacket;
|
return gamePacket;
|
||||||
|
@ -71,22 +71,19 @@ float MarioSize(bool is2d) {
|
||||||
}
|
}
|
||||||
|
|
||||||
server.PacketHandler = (c, p) => {
|
server.PacketHandler = (c, p) => {
|
||||||
{
|
|
||||||
switch (p) {
|
|
||||||
case GamePacket gamePacket: {
|
|
||||||
c.Metadata["scenario"] = gamePacket.ScenarioNum;
|
|
||||||
c.Metadata["2d"] = gamePacket.Is2d;
|
|
||||||
c.Metadata["lastGamePacket"] = gamePacket;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case TagPacket tagPacket: {
|
|
||||||
if ((tagPacket.UpdateType & TagPacket.TagUpdate.State) != 0) c.Metadata["seeking"] = tagPacket.IsIt;
|
|
||||||
if ((tagPacket.UpdateType & TagPacket.TagUpdate.Time) != 0) c.Metadata["time"] = new Time(tagPacket.Minutes, tagPacket.Seconds, DateTime.Now);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
switch (p) {
|
switch (p) {
|
||||||
|
case GamePacket gamePacket: {
|
||||||
|
c.Logger.Info($"Got game packet {gamePacket.Stage}->{gamePacket.ScenarioNum}");
|
||||||
|
c.Metadata["scenario"] = gamePacket.ScenarioNum;
|
||||||
|
c.Metadata["2d"] = gamePacket.Is2d;
|
||||||
|
c.Metadata["lastGamePacket"] = gamePacket;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case TagPacket tagPacket: {
|
||||||
|
if ((tagPacket.UpdateType & TagPacket.TagUpdate.State) != 0) c.Metadata["seeking"] = tagPacket.IsIt;
|
||||||
|
if ((tagPacket.UpdateType & TagPacket.TagUpdate.Time) != 0) c.Metadata["time"] = new Time(tagPacket.Minutes, tagPacket.Seconds, DateTime.Now);
|
||||||
|
break;
|
||||||
|
}
|
||||||
case CostumePacket:
|
case CostumePacket:
|
||||||
ClientSyncShineBag(c);
|
ClientSyncShineBag(c);
|
||||||
c.Metadata["loadedSave"] = true;
|
c.Metadata["loadedSave"] = true;
|
||||||
|
|
|
@ -31,12 +31,6 @@ public class Server {
|
||||||
Logger.Warn($"Accepted connection for client {socket.RemoteEndPoint}");
|
Logger.Warn($"Accepted connection for client {socket.RemoteEndPoint}");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (Clients.Count == Settings.Instance.Server.MaxPlayers) {
|
|
||||||
Logger.Warn("Turned away client due to max clients");
|
|
||||||
await socket.DisconnectAsync(false);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
Task.Run(() => HandleSocket(socket));
|
Task.Run(() => HandleSocket(socket));
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
|
@ -173,10 +167,20 @@ public class Server {
|
||||||
connect.Deserialize(memory.Memory.Span[packetRange]);
|
connect.Deserialize(memory.Memory.Span[packetRange]);
|
||||||
lock (Clients) {
|
lock (Clients) {
|
||||||
client.Name = connect.ClientName;
|
client.Name = connect.ClientName;
|
||||||
|
if (Clients.Count(x => x.Connected) == Constants.MaxClients) {
|
||||||
|
client.Logger.Error($"Turned away as server is at max clients");
|
||||||
|
memory.Dispose();
|
||||||
|
goto disconnect;
|
||||||
|
}
|
||||||
bool firstConn = false;
|
bool firstConn = false;
|
||||||
switch (connect.ConnectionType) {
|
switch (connect.ConnectionType) {
|
||||||
case ConnectPacket.ConnectionTypes.FirstConnection: {
|
case ConnectPacket.ConnectionTypes.FirstConnection: {
|
||||||
firstConn = true;
|
firstConn = true;
|
||||||
|
if (FindExistingClient(header.Id) is { } newClient) {
|
||||||
|
if (newClient.Connected) throw new Exception($"Tried to join as already connected user {header.Id}");
|
||||||
|
newClient.Socket = client.Socket;
|
||||||
|
client = newClient;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case ConnectPacket.ConnectionTypes.Reconnecting: {
|
case ConnectPacket.ConnectionTypes.Reconnecting: {
|
||||||
|
@ -277,6 +281,7 @@ public class Server {
|
||||||
|
|
||||||
memory?.Dispose();
|
memory?.Dispose();
|
||||||
}
|
}
|
||||||
|
disconnect:
|
||||||
Logger.Info($"Client {socket.RemoteEndPoint} ({client.Name}/{client.Id}) disconnected from the server");
|
Logger.Info($"Client {socket.RemoteEndPoint} ({client.Name}/{client.Id}) disconnected from the server");
|
||||||
|
|
||||||
// Clients.Remove(client)
|
// Clients.Remove(client)
|
||||||
|
@ -291,4 +296,4 @@ public class Server {
|
||||||
//no need to error check, the client will disconnect when the packet is invalid :)
|
//no need to error check, the client will disconnect when the packet is invalid :)
|
||||||
return MemoryMarshal.Read<PacketHeader>(data);
|
return MemoryMarshal.Read<PacketHeader>(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,8 +6,10 @@ using Shared;
|
||||||
using Shared.Packet;
|
using Shared.Packet;
|
||||||
using Shared.Packet.Packets;
|
using Shared.Packet.Packets;
|
||||||
|
|
||||||
// Guid ownId = new Guid(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
// Guid startId = new Guid(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
||||||
Guid baseOtherId = Guid.Parse("8ca3fcdd-2940-1000-b5f8-579301fcbfbb");
|
|
||||||
|
// Guid baseOtherId = Guid.Parse("8ca3fcdd-2940-1000-b5f8-579301fcbfbb");
|
||||||
|
Guid baseOtherId = Guid.Parse("d5feae62-2e71-1000-88fd-597ea147ae88");
|
||||||
|
|
||||||
PacketType[] reboundPackets = {
|
PacketType[] reboundPackets = {
|
||||||
PacketType.Player,
|
PacketType.Player,
|
||||||
|
@ -20,10 +22,12 @@ PacketType[] reboundPackets = {
|
||||||
};
|
};
|
||||||
|
|
||||||
string lastCapture = "";
|
string lastCapture = "";
|
||||||
|
List<TcpClient> clients = new List<TcpClient>();
|
||||||
|
|
||||||
async Task S(string n, Guid otherId, Guid ownId) {
|
async Task S(string n, Guid otherId, Guid ownId) {
|
||||||
Logger logger = new Logger($"Client ({n})");
|
Logger logger = new Logger($"Client ({n})");
|
||||||
TcpClient client = new TcpClient(args[0], 1027);
|
TcpClient client = new TcpClient(args[0], 1027);
|
||||||
|
clients.Add(client);
|
||||||
NetworkStream stream = client.GetStream();
|
NetworkStream stream = client.GetStream();
|
||||||
logger.Info("Connected!");
|
logger.Info("Connected!");
|
||||||
async Task<bool> Read(Memory<byte> readMem, int readSize, int readOffset) {
|
async Task<bool> Read(Memory<byte> readMem, int readSize, int readOffset) {
|
||||||
|
@ -44,7 +48,7 @@ async Task S(string n, Guid otherId, Guid ownId) {
|
||||||
|
|
||||||
{
|
{
|
||||||
ConnectPacket connect = new ConnectPacket {
|
ConnectPacket connect = new ConnectPacket {
|
||||||
ConnectionType = ConnectPacket.ConnectionTypes.FirstConnection,
|
ConnectionType = ConnectPacket.ConnectionTypes.Reconnecting,
|
||||||
ClientName = n
|
ClientName = n
|
||||||
};
|
};
|
||||||
PacketHeader coolHeader = new PacketHeader {
|
PacketHeader coolHeader = new PacketHeader {
|
||||||
|
@ -68,13 +72,19 @@ async Task S(string n, Guid otherId, Guid ownId) {
|
||||||
if (!await Read(owner.Memory, header.PacketSize, Constants.HeaderSize)) return;
|
if (!await Read(owner.Memory, header.PacketSize, Constants.HeaderSize)) return;
|
||||||
}
|
}
|
||||||
PacketType type = header.Type;
|
PacketType type = header.Type;
|
||||||
if (header.Id != otherId) continue;
|
if (header.Id != otherId || reboundPackets.All(x => x != type)) {
|
||||||
if (reboundPackets.All(x => x != type)) continue;
|
owner.Dispose();
|
||||||
if (type == PacketType.Tag) {
|
continue;
|
||||||
TagPacket packet = new TagPacket();
|
}
|
||||||
packet.Deserialize(owner.Memory.Span[Constants.HeaderSize..(Constants.HeaderSize + header.PacketSize)]);
|
if (type == PacketType.Player) {
|
||||||
packet.IsIt = true;
|
Task.Run(async () => {
|
||||||
packet.Serialize(owner.Memory.Span[Constants.HeaderSize..(Constants.HeaderSize + header.PacketSize)]);
|
await Task.Delay(5000);
|
||||||
|
header.Id = ownId;
|
||||||
|
MemoryMarshal.Write(owner.Memory.Span[..Constants.HeaderSize], ref header);
|
||||||
|
await stream.WriteAsync(owner.Memory[..(Constants.HeaderSize + header.PacketSize)]);
|
||||||
|
owner.Dispose();
|
||||||
|
});
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
header.Id = ownId;
|
header.Id = ownId;
|
||||||
MemoryMarshal.Write(owner.Memory.Span[..Constants.HeaderSize], ref header);
|
MemoryMarshal.Write(owner.Memory.Span[..Constants.HeaderSize], ref header);
|
||||||
|
@ -84,10 +94,19 @@ async Task S(string n, Guid otherId, Guid ownId) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Guid temp = baseOtherId;
|
Guid temp = baseOtherId;
|
||||||
IEnumerable<Task> stuff = Enumerable.Range(0, 1).Select(i => {
|
IEnumerable<Task> stuff = Enumerable.Range(0, 3).Select(i => {
|
||||||
Guid newOwnId = Guid.NewGuid();
|
byte[] tmp = temp.ToByteArray();
|
||||||
|
tmp[0]++;
|
||||||
|
Guid newOwnId = new Guid(tmp);
|
||||||
Task task = S($"Sussy {i}", temp, newOwnId);
|
Task task = S($"Sussy {i}", temp, newOwnId);
|
||||||
temp = newOwnId;
|
temp = newOwnId;
|
||||||
return task;
|
return task;
|
||||||
});
|
});
|
||||||
await Task.WhenAll(stuff.ToArray());
|
Console.CancelKeyPress += (_, e) => {
|
||||||
|
e.Cancel = true;
|
||||||
|
foreach (TcpClient tcpClient in clients) {
|
||||||
|
tcpClient.Close();
|
||||||
|
}
|
||||||
|
Environment.Exit(0);
|
||||||
|
};
|
||||||
|
await Task.WhenAll(stuff.ToArray());
|
||||||
|
|
Loading…
Reference in a new issue