This repository has been archived by the owner on Nov 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Oliver-makes-code/integrated-server
Integrated server
- Loading branch information
Showing
86 changed files
with
2,125 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#version 450 | ||
|
||
layout(location = 1) in vec4 fsin_Color; | ||
|
||
layout(location = 0) out vec4 fsout_Color; | ||
|
||
void main() { | ||
fsout_Color = fsin_Color; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#version 450 | ||
|
||
layout(location = 0) in vec3 Position; | ||
layout(location = 1) in vec4 Color; | ||
layout(location = 2) in vec2 UV; | ||
|
||
|
||
layout(location = 1) out vec4 fsin_Color; | ||
|
||
layout (set = 0, binding = 0) uniform CameraData { | ||
mat4 VPMatrix; | ||
}; | ||
|
||
void main() { | ||
vec4 pos = vec4(Position, 1) * VPMatrix; | ||
gl_Position = pos; | ||
|
||
fsin_Color = Color; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using Voxel.Common.Network; | ||
using Voxel.Common.Network.Packets.S2C; | ||
|
||
namespace Voxel.Client.Network; | ||
|
||
public abstract class C2SConnection : ConnectionBase<S2CPacket> { | ||
|
||
public abstract void Tick(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System; | ||
using Voxel.Client.World; | ||
using Voxel.Common.Network.Packets; | ||
using Voxel.Common.Network.Packets.C2S; | ||
using Voxel.Common.Network.Packets.S2C; | ||
using Voxel.Common.Network.Packets.S2C.Gameplay; | ||
using Voxel.Common.Network.Packets.S2C.Handshake; | ||
using Voxel.Common.Network.Packets.Utils; | ||
|
||
namespace Voxel.Client.Network; | ||
|
||
/// <summary> | ||
/// This controls a connection from the client to a server. | ||
/// | ||
/// Holds stuff like connection status and whatnot. | ||
/// </summary> | ||
public class ClientConnectionContext { | ||
public bool isDead => Connection.isDead; | ||
|
||
public readonly VoxelClient Client; | ||
private readonly C2SConnection Connection; | ||
|
||
private readonly PacketHandler<S2CPacket> HandshakeHandler; | ||
private readonly PacketHandler<S2CPacket> GameplayHandler; | ||
|
||
public ClientConnectionContext(VoxelClient client, C2SConnection connection) { | ||
Client = client; | ||
Connection = connection; | ||
|
||
HandshakeHandler = new PacketHandler<S2CPacket>(); | ||
HandshakeHandler.RegisterHandler<S2CHandshakeDone>(HandleHandshakeDone); | ||
|
||
GameplayHandler = new PacketHandler<S2CPacket>(); | ||
GameplayHandler.RegisterHandler<SetupWorld>(HandleSetupWorld); | ||
GameplayHandler.RegisterHandler<ChunkData>(HandleChunkData); | ||
GameplayHandler.RegisterHandler<ChunkUnload>(HandleChunkUnload); | ||
|
||
Connection.packetHandler = HandshakeHandler; | ||
} | ||
|
||
public void Tick() { | ||
if (Connection.isDead) | ||
return; | ||
|
||
Connection.Tick(); | ||
} | ||
|
||
private void HandleSetupWorld(SetupWorld packet) { | ||
Client.SetupWorld(); | ||
} | ||
|
||
private void HandleHandshakeDone(S2CHandshakeDone packet) { | ||
Connection.packetHandler = GameplayHandler; | ||
Console.WriteLine("Client:Server Says Handshake Done"); | ||
} | ||
|
||
private void HandleChunkData(ChunkData packet) { | ||
if (Client.world == null) | ||
return; | ||
|
||
var chunk = Client.world.GetOrCreateChunk(packet.position); | ||
packet.Apply(chunk); | ||
} | ||
|
||
private void HandleChunkUnload(ChunkUnload packet) { | ||
if (Client.world == null) | ||
return; | ||
|
||
if (Client.world.TryGetChunkRaw(packet.position, out var chunk)) | ||
packet.Apply(chunk); | ||
} | ||
|
||
public void SendPacket(C2SPacket packet) { | ||
Connection.DeliverPacket(packet); | ||
PacketPool.Return(packet); | ||
} | ||
|
||
public void Close() => Connection.Close(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
using System; | ||
using System.Net; | ||
using System.Net.Sockets; | ||
using LiteNetLib; | ||
using Voxel.Common.Network.Packets; | ||
using Voxel.Common.Network.Packets.C2S; | ||
using Voxel.Common.Network.Packets.C2S.Handshake; | ||
using Voxel.Common.Network.Packets.S2C; | ||
using Voxel.Common.Network.Packets.Utils; | ||
using Voxel.Common.Util.Serialization.Compressed; | ||
|
||
namespace Voxel.Client.Network; | ||
|
||
public class InternetC2SConnection : C2SConnection, INetEventListener { | ||
|
||
private readonly NetManager NetClient; | ||
private NetPeer? peer; | ||
|
||
private readonly PacketMap<C2SPacket> PacketMap = new(); | ||
private readonly CompressedVDataReader Reader = new(); | ||
private readonly CompressedVDataWriter Writer = new(); | ||
|
||
private bool synced = false; | ||
|
||
public InternetC2SConnection(string address, int port = 24564) { | ||
PacketMap.FillOutgoingMap(); | ||
NetClient = new NetManager(this); | ||
|
||
OnClosed += () => { | ||
//Disconnect the peer if it was connected. | ||
if (peer != null && peer.ConnectionState != ConnectionState.Disconnected) { | ||
peer.Disconnect(); | ||
peer = null; | ||
NetClient.Stop(true); | ||
} | ||
}; | ||
|
||
|
||
NetClient.Start(); | ||
NetClient.Connect(address, port, string.Empty); | ||
} | ||
|
||
public override void Tick() { | ||
NetClient.PollEvents(); | ||
} | ||
|
||
public override void DeliverPacket(Packet toSend) { | ||
if (peer == null) | ||
throw new InvalidOperationException("Cannot send packet on disconnected line"); | ||
|
||
var writer = Writer; | ||
writer.Reset(); | ||
|
||
if (!PacketMap.outgoingMap.TryGetValue(toSend.GetType(), out var rawID)) | ||
throw new InvalidOperationException($"Cannot send unknown packet {toSend}"); | ||
|
||
writer.Write(rawID); | ||
writer.Write(toSend); | ||
peer.Send(writer.currentBytes, 0, DeliveryMethod.ReliableOrdered); | ||
|
||
//Console.WriteLine($"Sending {writer.currentBytes.Length} bytes to server"); | ||
} | ||
|
||
public void OnPeerConnected(NetPeer peer) { | ||
this.peer = peer; | ||
|
||
Console.Out.WriteLine("Client: Connected!"); | ||
|
||
//SYNC MAPS HERE | ||
var writer = Writer; | ||
writer.Reset(); | ||
|
||
PacketMap.WriteOutgoingMap(writer); | ||
peer.Send(writer.currentBytes, 0, DeliveryMethod.ReliableOrdered); | ||
|
||
//After maps have been synced, client handshake is done. | ||
DeliverPacket(new C2SHandshakeDone()); | ||
} | ||
|
||
public void OnNetworkReceive(NetPeer _, NetPacketReader nReader, byte channelNumber, DeliveryMethod deliveryMethod) { | ||
if (packetHandler == null) | ||
return; | ||
|
||
Reader.LoadData(nReader.RawData.AsSpan(nReader.UserDataOffset, nReader.UserDataSize)); | ||
|
||
if (!synced) { | ||
PacketMap.ReadIncomingMap(Reader); | ||
synced = true; | ||
//Console.Out.WriteLine("Client: S2C Map Synced"); | ||
return; | ||
} | ||
|
||
var rawID = Reader.ReadUint(); | ||
if (!PacketMap.incomingMap.TryGetValue(rawID, out var packetType)) | ||
return; | ||
|
||
//Console.WriteLine($"Got packet {packetType.Name} from server"); | ||
|
||
var packet = PacketPool.GetPacket<S2CPacket>(packetType); | ||
packet.Read(Reader); | ||
|
||
packetHandler.HandlePacket(packet); | ||
} | ||
|
||
public void OnPeerDisconnected(NetPeer _, DisconnectInfo disconnectInfo) { | ||
|
||
} | ||
|
||
public void OnNetworkError(IPEndPoint endPoint, SocketError socketError) { | ||
|
||
} | ||
|
||
|
||
public void OnNetworkReceiveUnconnected(IPEndPoint remoteEndPoint, NetPacketReader reader, UnconnectedMessageType messageType) { | ||
|
||
} | ||
|
||
public void OnNetworkLatencyUpdate(NetPeer peer, int latency) { | ||
|
||
} | ||
|
||
//UNUSED | ||
public void OnConnectionRequest(ConnectionRequest request) { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.