0
0
Fork 0
mirror of https://github.com/Sanae6/SmoOnlineServer.git synced 2024-11-21 18:55:17 +00:00

Improving rejoins and other changes

This commit is contained in:
Sanae 2022-03-22 03:17:48 -06:00
parent 27d2d9acd9
commit 8645db12fa
3 changed files with 57 additions and 36 deletions

View file

@ -26,7 +26,7 @@ server.ClientJoined += (c, _) => {
}
}
c.PacketTransformer += (sender, packet) => {
c.PacketTransformer += (_, packet) => {
if (Settings.Instance.Scenario.MergeEnabled && packet is GamePacket gamePacket) {
gamePacket.ScenarioNum = (byte?) c.Metadata["scenario"] ?? 0;
return gamePacket;
@ -71,22 +71,19 @@ float MarioSize(bool is2d) {
}
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) {
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:
ClientSyncShineBag(c);
c.Metadata["loadedSave"] = true;

View file

@ -31,12 +31,6 @@ public class Server {
Logger.Warn($"Accepted connection for client {socket.RemoteEndPoint}");
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));
}
catch (Exception e) {
@ -173,10 +167,20 @@ public class Server {
connect.Deserialize(memory.Memory.Span[packetRange]);
lock (Clients) {
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;
switch (connect.ConnectionType) {
case ConnectPacket.ConnectionTypes.FirstConnection: {
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;
}
case ConnectPacket.ConnectionTypes.Reconnecting: {
@ -277,6 +281,7 @@ public class Server {
memory?.Dispose();
}
disconnect:
Logger.Info($"Client {socket.RemoteEndPoint} ({client.Name}/{client.Id}) disconnected from the server");
// Clients.Remove(client)
@ -291,4 +296,4 @@ public class Server {
//no need to error check, the client will disconnect when the packet is invalid :)
return MemoryMarshal.Read<PacketHeader>(data);
}
}
}

View file

@ -6,8 +6,10 @@ using Shared;
using Shared.Packet;
using Shared.Packet.Packets;
// Guid ownId = new Guid(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
Guid baseOtherId = Guid.Parse("8ca3fcdd-2940-1000-b5f8-579301fcbfbb");
// 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("d5feae62-2e71-1000-88fd-597ea147ae88");
PacketType[] reboundPackets = {
PacketType.Player,
@ -20,10 +22,12 @@ PacketType[] reboundPackets = {
};
string lastCapture = "";
List<TcpClient> clients = new List<TcpClient>();
async Task S(string n, Guid otherId, Guid ownId) {
Logger logger = new Logger($"Client ({n})");
TcpClient client = new TcpClient(args[0], 1027);
clients.Add(client);
NetworkStream stream = client.GetStream();
logger.Info("Connected!");
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 {
ConnectionType = ConnectPacket.ConnectionTypes.FirstConnection,
ConnectionType = ConnectPacket.ConnectionTypes.Reconnecting,
ClientName = n
};
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;
}
PacketType type = header.Type;
if (header.Id != otherId) continue;
if (reboundPackets.All(x => x != type)) continue;
if (type == PacketType.Tag) {
TagPacket packet = new TagPacket();
packet.Deserialize(owner.Memory.Span[Constants.HeaderSize..(Constants.HeaderSize + header.PacketSize)]);
packet.IsIt = true;
packet.Serialize(owner.Memory.Span[Constants.HeaderSize..(Constants.HeaderSize + header.PacketSize)]);
if (header.Id != otherId || reboundPackets.All(x => x != type)) {
owner.Dispose();
continue;
}
if (type == PacketType.Player) {
Task.Run(async () => {
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;
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;
IEnumerable<Task> stuff = Enumerable.Range(0, 1).Select(i => {
Guid newOwnId = Guid.NewGuid();
IEnumerable<Task> stuff = Enumerable.Range(0, 3).Select(i => {
byte[] tmp = temp.ToByteArray();
tmp[0]++;
Guid newOwnId = new Guid(tmp);
Task task = S($"Sussy {i}", temp, newOwnId);
temp = newOwnId;
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());