Fix all of the nullable issues
This commit is contained in:
1 parent
9d97ad0975
commit
30feb15821
13 files changed
+58
-53
No files matched your search
@@ -1,15 +0,0 @@
|
||||
The servers default port is 25566
|
||||
|
||||
|
||||
TCPServer.cs
|
||||
Void TCPServer() Initilizes the server object on the default game port of 25566
|
||||
Void SendAll(string Data) Send data to all the clients
|
||||
Event onReceived Fires when data is received from a client
|
||||
|
||||
TCPClient.cs
|
||||
Void TCPClient(String ServerIP) Initilizes the client object
|
||||
Void Send(String Data) Sends data to the server
|
||||
Event onReceived Fires when data is received from the server
|
||||
|
||||
UDPClient.cs
|
||||
|
||||
@@ -20,7 +20,7 @@ namespace Mistox.Client {
|
||||
public Vector3 Position { get; set; } = new Vector3(0, 0, 0); // My world position
|
||||
public float MaxNetworkRenderDistance { get; private set; } = 0; // The distance for connections
|
||||
|
||||
public mTCPClient ServerConnection {get; private set; } // Global TCP Server Connection
|
||||
public mTCPClient? ServerConnection {get; private set; } // Global TCP Server Connection
|
||||
public UdpClient ClientSocket { get; private set; } // Global UDP Sender
|
||||
public UDPReceiver UdpReceiver { get; private set; } // Global UDP Receiver
|
||||
|
||||
@@ -36,6 +36,14 @@ namespace Mistox.Client {
|
||||
UserName = userName;
|
||||
ConnectionKey = connectionKey;
|
||||
|
||||
onServerConnected += () => {};
|
||||
onUserConnected += (_) => {};
|
||||
onUserJoined += (_) => {};
|
||||
onDataReceived += (_,_) => {};
|
||||
onUserLeft += (_) => {};
|
||||
onUserDisconnected += (_) => {};
|
||||
onServerDisconnected += () => {};
|
||||
|
||||
// Create and connect to server on TCP
|
||||
Console.WriteLine( "The client is initilized and trying to connect to the server at ip : " + Server.Address );
|
||||
try {
|
||||
@@ -116,6 +124,7 @@ namespace Mistox.Client {
|
||||
|
||||
async Task TryAuth() {
|
||||
Console.WriteLine("Attempting to authenticate against the server...");
|
||||
if (ServerConnection == null) return;
|
||||
await ServerConnection.Send(new UserAuthPacket {
|
||||
UserName = UserName,
|
||||
ConnectionKey = ConnectionKey
|
||||
@@ -158,8 +167,8 @@ namespace Mistox.Client {
|
||||
}
|
||||
|
||||
// When generic data is received
|
||||
public event Action<dynamic> onDataReceived;
|
||||
public void InvokeOnReceived(dynamic DataPacket, mUDPClient remote = null){
|
||||
public event Action<mUDPClient?, dynamic> onDataReceived;
|
||||
public void InvokeOnReceived(dynamic DataPacket, mUDPClient? remote = null){
|
||||
switch (DataPacket as object) {
|
||||
case HeartbeatPacket packet:
|
||||
HeartbeatReceived(packet);
|
||||
@@ -173,7 +182,7 @@ namespace Mistox.Client {
|
||||
Console.WriteLine("Received a null packet.");
|
||||
break;
|
||||
default:
|
||||
onDataReceived?.Invoke(DataPacket);
|
||||
onDataReceived?.Invoke(remote, DataPacket);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -219,6 +228,7 @@ namespace Mistox.Client {
|
||||
}
|
||||
|
||||
public async Task SendServer<Packet>(Packet data) {
|
||||
if (ServerConnection == null) return;
|
||||
await ServerConnection.Send( data );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,10 +48,11 @@ namespace Mistox.Client {
|
||||
await ns.ReadExactlyAsync(payloadBuffer, 0, Header.DataLength);
|
||||
|
||||
// Create the dataobject
|
||||
dynamic dataobject = DataPacket.Deserialize( Header, payloadBuffer.AsMemory(DataPacket.HeaderLength, Header.DataLength) );
|
||||
|
||||
// Return the object
|
||||
_parent.InvokeOnReceived(dataobject);
|
||||
object? dataobject = DataPacket.Deserialize( Header, payloadBuffer.AsMemory(DataPacket.HeaderLength, Header.DataLength) );
|
||||
if (dataobject != null) {
|
||||
// Return the object
|
||||
_parent.InvokeOnReceived(dataobject);
|
||||
}
|
||||
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
||||
@@ -21,9 +21,9 @@ namespace Mistox.Client {
|
||||
UdpReceiveResult udpPacket = await _parent.ClientSocket.ReceiveAsync();
|
||||
|
||||
// Try find the user
|
||||
mUDPClient client = null;
|
||||
bool clientExists = _parent.clientConnections.TryGetValue(udpPacket.RemoteEndPoint, out client);
|
||||
if (clientExists) {
|
||||
mUDPClient? client = null;
|
||||
_parent.clientConnections.TryGetValue(udpPacket.RemoteEndPoint, out client);
|
||||
if (client != null) {
|
||||
|
||||
// Create the data readers
|
||||
ReadOnlyMemory<byte> dataMemory = udpPacket.Buffer.AsMemory();
|
||||
@@ -39,10 +39,12 @@ namespace Mistox.Client {
|
||||
ReadOnlyMemory<byte> data = dataMemory.Slice(DataPacket.HeaderLength, Header.DataLength);
|
||||
|
||||
// Extract the object
|
||||
dynamic buildObj = DataPacket.Deserialize( Header, data );
|
||||
object? buildObj = DataPacket.Deserialize( Header, data );
|
||||
if (buildObj != null) {
|
||||
|
||||
// Return the object
|
||||
_parent.InvokeOnReceived( buildObj, client );
|
||||
// Return the object
|
||||
_parent.InvokeOnReceived( buildObj, client );
|
||||
}
|
||||
|
||||
}
|
||||
} catch( Exception e ) {
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace Mistox {
|
||||
return new MistoxServer(listenerAddress, serverName);
|
||||
}
|
||||
|
||||
public static MistoxClient newClient( string UserName, string ServerIPOrHostName, int Port, string ConnectionKey = "" ) {
|
||||
public static MistoxClient? newClient( string UserName, string ServerIPOrHostName, int Port, string ConnectionKey = "" ) {
|
||||
|
||||
// Build the network packet list
|
||||
NetworkPacketDictionary.BuildNetworkPacketList();
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -22,6 +22,9 @@ namespace Mistox.Server {
|
||||
public MistoxServer(IPEndPoint ListenerAddress, string serverName) {
|
||||
ServerName = serverName;
|
||||
Listener = new ServerListener(ListenerAddress, this);
|
||||
onUserConnected += (_) => {};
|
||||
onDataReceived += (_, _) => {};
|
||||
onUserDisconnected += (_) => {};
|
||||
Console.WriteLine( "The Server is initilized and waiting for clients to connect at port : " + ListenerAddress.Port );
|
||||
}
|
||||
|
||||
@@ -80,7 +83,7 @@ namespace Mistox.Server {
|
||||
|
||||
public event Action<ServerConnection, object> onDataReceived;
|
||||
public void InvokeDataReceived( ServerConnection user, dynamic data ){
|
||||
switch (data) {
|
||||
switch (data) {
|
||||
case UserAuthPacket packet when !user.IsAuthenticated:
|
||||
_ = RunAsync(async () => {
|
||||
await ProcessAuthRequest(user, packet);
|
||||
|
||||
@@ -55,10 +55,11 @@ namespace Mistox.Server {
|
||||
await ns.ReadExactlyAsync(payloadBuffer, 0, Header.DataLength, _cts.Token);
|
||||
|
||||
// Create the dataobject
|
||||
dynamic dataobject = DataPacket.Deserialize( Header, payloadBuffer.AsMemory(DataPacket.HeaderLength) );
|
||||
|
||||
// Return the object
|
||||
_parent.InvokeDataReceived(this, dataobject);
|
||||
object? dataobject = DataPacket.Deserialize( Header, payloadBuffer.AsMemory(DataPacket.HeaderLength) );
|
||||
if (dataobject != null) {
|
||||
// Return the object
|
||||
_parent.InvokeDataReceived(this, dataobject);
|
||||
}
|
||||
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
||||
@@ -32,14 +32,15 @@ namespace Mistox.Server {
|
||||
continue;
|
||||
}
|
||||
|
||||
IPEndPoint remote = client.Client.RemoteEndPoint as IPEndPoint;
|
||||
IPEndPoint? remote = client.Client.RemoteEndPoint as IPEndPoint;
|
||||
if (remote != null) {
|
||||
string connectionID = Guid.NewGuid().ToString();
|
||||
_parent.Connections[connectionID] = new ServerConnection( client, _parent, remote) {
|
||||
UserName = connectionID
|
||||
};
|
||||
|
||||
string connectionID = Guid.NewGuid().ToString();
|
||||
_parent.Connections[connectionID] = new ServerConnection( client, _parent, remote) {
|
||||
UserName = connectionID
|
||||
};
|
||||
|
||||
Console.WriteLine( "New User Connected" );
|
||||
Console.WriteLine( "New User Connected" );
|
||||
}
|
||||
}
|
||||
Dispose();
|
||||
}
|
||||
|
||||
@@ -46,10 +46,10 @@ namespace Mistox {
|
||||
return writer.WrittenSpan.ToArray();
|
||||
}
|
||||
|
||||
public static object Deserialize( DataPacketHeader Header, ReadOnlyMemory<byte> Data ) {
|
||||
public static object? Deserialize( DataPacketHeader Header, ReadOnlyMemory<byte> Data ) {
|
||||
|
||||
// Get the type from the tag
|
||||
if (!NetworkPacketDictionary.PacketLookupTable.TryGetValue(Header.Type, out Type resolvedType)) {
|
||||
if (!NetworkPacketDictionary.PacketLookupTable.TryGetValue(Header.Type, out Type? resolvedType)) {
|
||||
throw new Exception($"Unknown packet type ID: {Header.Type}");
|
||||
}
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ namespace Mistox.Attributes {
|
||||
foreach (Assembly assembly in assemblies) {
|
||||
Type[] allClasses = assembly.GetTypes();
|
||||
foreach( Type cur in allClasses) {
|
||||
mPacket attribute = cur.GetCustomAttribute<mPacket>(false);
|
||||
mPacket? attribute = cur.GetCustomAttribute<mPacket>(false);
|
||||
if (attribute != null) {
|
||||
|
||||
// Generate a deterministic 16-bit ID based on the Type's FullName
|
||||
|
||||
@@ -25,31 +25,31 @@ namespace Mistox.ResevedPackets {
|
||||
|
||||
// Tag to make sure the object on the other side is the same object i want to update here
|
||||
// Need to map these Object ID's to a dictionary to make sure I only send network updates when there is a change
|
||||
public ushort ObjectId { get; set; }
|
||||
public ushort ObjectId { get; set; } = 0;
|
||||
|
||||
// Packet payload data that im matching for in the dictionary
|
||||
public byte[] InnerPayload { get; set; }
|
||||
public byte[] InnerPayload { get; set; } = {};
|
||||
}
|
||||
|
||||
public class UserTableObject {
|
||||
public string UserName { get; set; }
|
||||
public IPAddress IpAddress { get; set; }
|
||||
public Vector3 Position { get; set; }
|
||||
public string UserName { get; set; } = string.Empty;
|
||||
public IPAddress IpAddress { get; set; } = new([]);
|
||||
public Vector3 Position { get; set; } = new();
|
||||
}
|
||||
|
||||
// Not implemented
|
||||
// Packet sent to all clients when a global position update occurrs
|
||||
[mPacket]
|
||||
public class HeartbeatPacket {
|
||||
public Vector3 Position { get; set; }
|
||||
public List<UserTableObject> ServerList { get; set; }
|
||||
public Vector3 Position { get; set; } = new();
|
||||
public List<UserTableObject> ServerList { get; set; } = new();
|
||||
}
|
||||
|
||||
// Not implimented
|
||||
// Packet sent to all clients when a users TCP stream disconnects from the server
|
||||
[mPacket]
|
||||
public class UserDisconnectedPacket {
|
||||
public string UserName { get; set; }
|
||||
public string UserName { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,6 +10,7 @@ The server receive thread needs to push the user it was received from up the cha
|
||||
8. Each client can figure out what connections it needs to make or drop via the list Done
|
||||
10. When the TCP connection drops update the heartbeat list so all the clients remove the connection Done
|
||||
11. Build a network cache table on the clients so obj's only send updates and not repeat packets
|
||||
12. User joined and left events are not implimented
|
||||
|
||||
|
||||
Set the network bubble distance [ render distance ] on the server and pass to the clients
|
||||
|
||||
Reference in new issue
Block a user