mirror of
https://github.com/Sanae6/SmoOnlineServer.git
synced 2024-11-25 04:35:18 +00:00
Good shit
This commit is contained in:
parent
f8955b630d
commit
b660fcfd53
4 changed files with 21 additions and 63 deletions
|
@ -1,5 +1,6 @@
|
|||
using System.Buffers;
|
||||
using System.Net.Sockets;
|
||||
using Shared.Packet.Packets;
|
||||
|
||||
namespace Server;
|
||||
|
||||
|
@ -7,6 +8,10 @@ public class Client : IDisposable {
|
|||
public Socket? Socket;
|
||||
public bool Connected => Socket?.Connected ?? false;
|
||||
public Guid Id;
|
||||
public CostumePacket CurrentCostume = new CostumePacket {
|
||||
BodyName = "",
|
||||
CapName = ""
|
||||
};
|
||||
public readonly Dictionary<string, object> Metadata = new Dictionary<string, object>(); // can be used to store any information about a player
|
||||
|
||||
public async Task Send(Memory<byte> data) {
|
||||
|
|
|
@ -14,7 +14,7 @@ public class Server {
|
|||
public readonly Logger Logger = new Logger("Server");
|
||||
public async Task Listen(ushort port) {
|
||||
Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
serverSocket.Bind(new IPEndPoint(IPAddress.Loopback, port));
|
||||
serverSocket.Bind(new IPEndPoint(IPAddress.Any, port));
|
||||
serverSocket.Listen();
|
||||
|
||||
Logger.Info($"Listening on port {port}");
|
||||
|
@ -130,14 +130,11 @@ public class Server {
|
|||
Clients.Add(client);
|
||||
|
||||
Parallel.ForEachAsync(toDisconnect, (c, token) => c.Socket!.DisconnectAsync(false, token));
|
||||
// Broadcast(connect, client);
|
||||
// done disconnecting and removing stale clients with the same id
|
||||
}
|
||||
}
|
||||
|
||||
Logger.Info($"Client {socket.RemoteEndPoint} connected.");
|
||||
|
||||
// continue;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
namespace Shared.Packet.Packets;
|
||||
|
||||
[Packet(PacketType.Unknown)] // empty like boss
|
||||
[Packet(PacketType.Command)]
|
||||
// [Packet(PacketType.Command)]
|
||||
public struct UnhandledPacket : IPacket {
|
||||
public byte[] Data = new byte[Constants.PacketDataSize];
|
||||
public void Serialize(Span<byte> data) {
|
||||
|
|
|
@ -18,68 +18,25 @@ double d = 0;
|
|||
Vector3 basePoint = Vector3.Zero;
|
||||
PlayerPacket? playerPacket = null;
|
||||
|
||||
async void Funny() {
|
||||
Memory<byte> memory = new Memory<byte>(new byte[256]);
|
||||
|
||||
{
|
||||
PacketHeader header = new PacketHeader {
|
||||
Id = ownId,
|
||||
Type = PacketType.Player
|
||||
};
|
||||
MemoryMarshal.Write(memory.Span, ref header);
|
||||
}
|
||||
|
||||
while (true) {
|
||||
d += Math.PI / 32;
|
||||
if (playerPacket == null) {
|
||||
// wait until valid player packet has arrived
|
||||
await Task.Delay(300);
|
||||
continue;
|
||||
}
|
||||
|
||||
PlayerPacket packet = playerPacket.Value;
|
||||
Vector3 pos = basePoint;
|
||||
pos.X += 100f * (float) Math.Cos(d);
|
||||
pos.Y += 300f;
|
||||
pos.Z += 100f * (float) Math.Sin(d);
|
||||
packet.Position = pos;
|
||||
packet.Serialize(memory.Span[Constants.HeaderSize..]);
|
||||
logger.Warn($"Current strs:{packet.Stage}-{packet.Act}-{packet.SubAct} {packet.Is2d} {packet.ThrowingCap} {packet.IsIt}");
|
||||
|
||||
await stream.WriteAsync(memory);
|
||||
await Task.Delay(50);
|
||||
}
|
||||
}
|
||||
PacketType[] reboundPackets = {
|
||||
PacketType.Player,
|
||||
PacketType.Cap,
|
||||
PacketType.Capture,
|
||||
PacketType.Costume,
|
||||
PacketType.Tag,
|
||||
PacketType.Shine
|
||||
};
|
||||
|
||||
async Task S() {
|
||||
IMemoryOwner<byte> owner = MemoryPool<byte>.Shared.Rent(256);
|
||||
while (true) {
|
||||
await stream.ReadAsync(owner.Memory);
|
||||
unsafe {
|
||||
|
||||
PacketHeader header = MemoryMarshal.Read<PacketHeader>(owner.Memory.Span);
|
||||
if (header.Type == PacketType.Player) {
|
||||
if (otherId == Guid.Empty) otherId = header.Id;
|
||||
if (otherId != header.Id) continue;
|
||||
if (e++ != 0) {
|
||||
e %= 3;
|
||||
continue;
|
||||
}
|
||||
|
||||
header.Id = ownId;
|
||||
MemoryMarshal.Write(owner.Memory.Span, ref header);
|
||||
fixed (byte* data = owner.Memory.Span[Constants.HeaderSize..]) {
|
||||
logger.Error($"{Marshal.OffsetOf<PlayerPacket>(nameof(PlayerPacket.AnimationBlendWeights))} {Marshal.OffsetOf<PlayerPacket>(nameof(PlayerPacket.AnimationRate))}");
|
||||
PlayerPacket packet = Marshal.PtrToStructure<PlayerPacket>((IntPtr) data);
|
||||
playerPacket = packet;
|
||||
basePoint = packet.Position;
|
||||
}
|
||||
|
||||
// packet.SubAct = "";
|
||||
} else if (header.Type == PacketType.Cap) {
|
||||
|
||||
}
|
||||
}
|
||||
PacketHeader header = MemoryMarshal.Read<PacketHeader>(owner.Memory.Span);
|
||||
PacketType type = header.Type;
|
||||
if (reboundPackets.All(x => x != type)) continue;
|
||||
header.Id = ownId;
|
||||
MemoryMarshal.Write(owner.Memory.Span, ref header);
|
||||
await stream.WriteAsync(owner.Memory);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -97,5 +54,4 @@ await stream.WriteAsync(owner.Memory);
|
|||
coolHeader.Type = PacketType.Player;
|
||||
MemoryMarshal.Write(owner.Memory.Span[..], ref coolHeader);
|
||||
logger.Info("Connected");
|
||||
Task.Run(Funny);
|
||||
await S();
|
Loading…
Reference in a new issue