Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
93badcb22d | ||
|
|
b14715e759 | ||
|
|
f93b78e5c1 | ||
|
|
e868d2116f | ||
|
|
4dc7d6b075 | ||
|
|
6f40e44224 | ||
|
|
291d0c3f07 |
No files matched your search
@@ -12,6 +12,9 @@ jobs:
|
|||||||
- name: checkout
|
- name: checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Setup .NET SDK
|
||||||
|
run: apk add dotnet10-sdk
|
||||||
|
|
||||||
- name: Restore Dependencies
|
- name: Restore Dependencies
|
||||||
run: dotnet restore ./MistoxServer
|
run: dotnet restore ./MistoxServer
|
||||||
|
|
||||||
|
|||||||
@@ -1,60 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Mistox;
|
|
||||||
using Mistox.Client;
|
|
||||||
|
|
||||||
class clientExample {
|
|
||||||
|
|
||||||
public MistoxClient? clientObject;
|
|
||||||
|
|
||||||
void onServerConnected() {
|
|
||||||
Console.WriteLine("Connected to server sucessfully");
|
|
||||||
}
|
|
||||||
|
|
||||||
void onClientConnected(mUDPClient user) {
|
|
||||||
Console.WriteLine("User connected to server with username: " + user.UserName);
|
|
||||||
}
|
|
||||||
|
|
||||||
void onClientJoined(mUDPClient user) {
|
|
||||||
Console.WriteLine("User is now in range of you and UDP connections started with user : " + user.UserName);
|
|
||||||
}
|
|
||||||
|
|
||||||
void onReceive(mUDPClient? user, dynamic data) {
|
|
||||||
switch (data){
|
|
||||||
case MessagePacket packet:
|
|
||||||
if (user == null) {
|
|
||||||
Console.WriteLine($"MSG From [Serverx]: {packet.Data}");
|
|
||||||
} else {
|
|
||||||
Console.WriteLine($"MSG From [{user.UserName}]: {packet.Data}");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void onClientLeft(mUDPClient user) {
|
|
||||||
Console.WriteLine("User is no longer in range of you and UDP connections ended with user : " + user.UserName);
|
|
||||||
}
|
|
||||||
|
|
||||||
void onClientDisconnected(mUDPClient user) {
|
|
||||||
Console.WriteLine("User disconnected from server with username: " + user.UserName);
|
|
||||||
}
|
|
||||||
|
|
||||||
void onServerDisconnected() {
|
|
||||||
Console.WriteLine("Disconnected from server");
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task StartClient(string userName, string host, int port, string password) {
|
|
||||||
clientObject = GameServer.newClient(userName, host, port, password);
|
|
||||||
if (clientObject == null) return;
|
|
||||||
clientObject.onServerConnected += onServerConnected;
|
|
||||||
clientObject.onUserConnected += onClientConnected;
|
|
||||||
clientObject.onUserJoined += onClientJoined;
|
|
||||||
clientObject.onDataReceived += onReceive;
|
|
||||||
clientObject.onUserLeft += onClientLeft;
|
|
||||||
clientObject.onUserDisconnected += onClientDisconnected;
|
|
||||||
clientObject.onServerDisconnected += onServerDisconnected;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
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
|
||||||
|
|
||||||
@@ -9,7 +9,6 @@
|
|||||||
<Company>Mistox.net</Company>
|
<Company>Mistox.net</Company>
|
||||||
<Product>Mistox Game Server</Product>
|
<Product>Mistox Game Server</Product>
|
||||||
<Description>Game Server for Mistox Games and partners</Description>
|
<Description>Game Server for Mistox Games and partners</Description>
|
||||||
<Nullable>enable</Nullable>
|
|
||||||
<SignAssembly>false</SignAssembly>
|
<SignAssembly>false</SignAssembly>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
|||||||
+83
-42
@@ -1,48 +1,85 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using MistoxServer;
|
||||||
|
|
||||||
namespace MistoxHolePunch {
|
namespace MistoxHolePunch {
|
||||||
|
|
||||||
enum Mode {
|
|
||||||
Server,
|
|
||||||
Client
|
|
||||||
}
|
|
||||||
|
|
||||||
class Program {
|
class Program {
|
||||||
|
|
||||||
static serverExample? server;
|
static IMistoxServer serverObj;
|
||||||
static clientExample? client;
|
static bool running = true;
|
||||||
static Mode mode;
|
|
||||||
|
|
||||||
async Task StartServer(string name, int port) {
|
void onConnected( object sender, EventArgs e ) {
|
||||||
server = new serverExample();
|
// sender and e are always null
|
||||||
await server.StartServer(name, port);
|
// put connected functions in here
|
||||||
// On a game this sleep loop isnt needed
|
}
|
||||||
if (server.serverObject == null) return;
|
|
||||||
while (server.serverObject.Alive) {
|
void slowReceive( object obj, EventArgs e ) {
|
||||||
// becuase this is a console app if we dont keep the main thread alive the app will close
|
if ( serverObj is ServerInterface ) {
|
||||||
await Task.Delay(5000);
|
// If ServerMode is passive obj is byte[] and Send takes in byte[]
|
||||||
|
// If Servermode is Athoritiative obj is the Class type sent and Send takes in any Class type except generic object
|
||||||
|
|
||||||
|
// Check to make sure data is correct before relaying
|
||||||
|
// Also perform server specific checks in here
|
||||||
|
// IE player didnt teleport or is shooting from 20 feet from his body
|
||||||
|
serverObj.Send( obj, SendType.SlowUpdate );
|
||||||
|
}
|
||||||
|
Console.Write( "Received TCP : " );
|
||||||
|
Console.WriteLine( obj );
|
||||||
|
}
|
||||||
|
|
||||||
|
void fastReceive( object obj, EventArgs e ) {
|
||||||
|
if( serverObj is ServerInterface ) {
|
||||||
|
// If ServerMode is passive obj is byte[] and Send takes in byte[]
|
||||||
|
// If Servermode is Athoritiative obj is the Class type sent and Send takes in any Class type except generic object
|
||||||
|
|
||||||
|
// Check to make sure data is correct before relaying
|
||||||
|
// Also perform server specific checks in here
|
||||||
|
// IE player didnt teleport or is shooting from 20 feet from his body
|
||||||
|
serverObj.Send( obj, SendType.FastUpdate );
|
||||||
|
}
|
||||||
|
Console.Write( "Received UDP : " );
|
||||||
|
Console.WriteLine( obj );
|
||||||
|
}
|
||||||
|
|
||||||
|
void onDisconnected( object sender, EventArgs e ) {
|
||||||
|
// sender and e are always null
|
||||||
|
// put disconnected functions in here
|
||||||
|
}
|
||||||
|
|
||||||
|
static string host = "example.com";
|
||||||
|
static int port = 7300;
|
||||||
|
static ServerMode mode = ServerMode.Passive;
|
||||||
|
|
||||||
|
async Task RunServer() {
|
||||||
|
serverObj = mServer.newServer( Convert.ToInt32( port ), mode );
|
||||||
|
serverObj.onConnected += onConnected;
|
||||||
|
serverObj.onSlowReceive += slowReceive;
|
||||||
|
serverObj.onFastReceive += fastReceive;
|
||||||
|
serverObj.onDisconnected += onDisconnected;
|
||||||
|
while ( running ) {
|
||||||
|
// Stop this thread for 1 second so the CPU isnt 100%
|
||||||
|
// All the real work is on different threads
|
||||||
|
// While loop needs to exist so our main thread doesnt close
|
||||||
|
await Task.Delay( 1000 );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async Task StartClient(string username, string host, int port, string password) {
|
async Task RunClient() {
|
||||||
client = new clientExample();
|
serverObj = mServer.newClient( host, Convert.ToInt32( port ) );
|
||||||
await client.StartClient(username, host, port, password);
|
serverObj.onConnected += onConnected;
|
||||||
// On a game this sleep loop isnt needed
|
serverObj.onSlowReceive += slowReceive;
|
||||||
if (client.clientObject == null) return;
|
serverObj.onFastReceive += fastReceive;
|
||||||
while (client.clientObject.Alive) {
|
serverObj.onDisconnected += onDisconnected;
|
||||||
// becuase this is a console app if we dont keep the main thread alive the app will close
|
while( running ) {
|
||||||
await Task.Delay(5000);
|
// Stop this thread for 1 second so the CPU isnt 100%
|
||||||
|
// All the real work is on different threads
|
||||||
|
// While loop needs to exist so our main thread doesnt close
|
||||||
|
await Task.Delay( 1000 );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async Task Main(string[] args) {
|
static async Task Main(string[] args) {
|
||||||
string Job = args.Length > 0 ? args[0].ToLower() : "";
|
string Task = args.Length > 0 ? args[0].ToLower() : null;
|
||||||
|
|
||||||
string host = "";
|
|
||||||
string name = "";
|
|
||||||
string pass = "";
|
|
||||||
int port = 0;
|
|
||||||
|
|
||||||
for( int i = 0; i < args.Length; i++ ) {
|
for( int i = 0; i < args.Length; i++ ) {
|
||||||
string cur = args[i].ToLower();
|
string cur = args[i].ToLower();
|
||||||
@@ -50,21 +87,25 @@ namespace MistoxHolePunch {
|
|||||||
host = args [i + 1];
|
host = args [i + 1];
|
||||||
} else if( cur == "/p" || cur == "-p" ) {
|
} else if( cur == "/p" || cur == "-p" ) {
|
||||||
port = Convert.ToInt32( args [i + 1] );
|
port = Convert.ToInt32( args [i + 1] );
|
||||||
} else if( cur == "/n" || cur == "-n" ) {
|
} else if( cur == "/a" || cur == "-a" ) {
|
||||||
name = args [i + 1];
|
mode = ServerMode.Authoritative;
|
||||||
} else if( cur == "/u" || cur == "-u" ) {
|
|
||||||
pass = args [i + 1];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Job == "/?" || Job == "--help") {
|
if (Task == "/?" || Task == "--help") {
|
||||||
Console.WriteLine(HelpDocumentation.HelpText);
|
Console.WriteLine(HelpDocumentation.HelpText);
|
||||||
} else if (Job == "/s" || Job == "-s") {
|
} else if (Task == "/s" || Task == "-s") {
|
||||||
await StartServer(name, port);
|
Program prog = new Program();
|
||||||
} else if (Job == "/c" || Job == "-c") {
|
await prog.RunServer();
|
||||||
await StartClient(name, host, port, pass);
|
} else if (Task == "/c" || Task == "-c") {
|
||||||
|
Program prog = new Program();
|
||||||
|
await prog.RunClient();
|
||||||
} else {
|
} else {
|
||||||
await StartServer("example.com", 4500);
|
host = "example.com";
|
||||||
|
port = 1500;
|
||||||
|
Program prog = new Program();
|
||||||
|
//prog.RunClient();
|
||||||
|
await prog.RunServer();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,35 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Mistox;
|
|
||||||
using Mistox.Server;
|
|
||||||
|
|
||||||
class serverExample {
|
|
||||||
|
|
||||||
public MistoxServer? serverObject;
|
|
||||||
|
|
||||||
void onClientConnected(ServerConnection user) {
|
|
||||||
Console.WriteLine("User connected with username: " + user.UserName);
|
|
||||||
}
|
|
||||||
|
|
||||||
void onReceive(ServerConnection user, dynamic data) {
|
|
||||||
switch (data){
|
|
||||||
case MessagePacket packet when user.IsAuthenticated:
|
|
||||||
Console.WriteLine($"MSG From [{user.UserName}]: {packet.Data}");
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void onClientDisconnected(ServerConnection user) {
|
|
||||||
Console.WriteLine("User disconnected with username: " + user.UserName);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task StartServer(string serverName, int port) {
|
|
||||||
serverObject = GameServer.newServer(serverName, port);
|
|
||||||
serverObject.onUserConnected += onClientConnected;
|
|
||||||
serverObject.onDataReceived += onReceive;
|
|
||||||
serverObject.onUserDisconnected += onClientDisconnected;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
using Mistox.Attributes;
|
|
||||||
|
|
||||||
[mPacket]
|
|
||||||
class MessagePacket {
|
|
||||||
public string Data { get; set; } = "";
|
|
||||||
}
|
|
||||||
@@ -20,7 +20,7 @@ namespace Mistox.Client {
|
|||||||
public Vector3 Position { get; set; } = new Vector3(0, 0, 0); // My world position
|
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 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 UdpClient ClientSocket { get; private set; } // Global UDP Sender
|
||||||
public UDPReceiver UdpReceiver { get; private set; } // Global UDP Receiver
|
public UDPReceiver UdpReceiver { get; private set; } // Global UDP Receiver
|
||||||
|
|
||||||
@@ -87,17 +87,13 @@ namespace Mistox.Client {
|
|||||||
if (!clientConnections.ContainsKey(remoteHost)) {
|
if (!clientConnections.ContainsKey(remoteHost)) {
|
||||||
mUDPClient newConnection = new mUDPClient(this, remoteHost);
|
mUDPClient newConnection = new mUDPClient(this, remoteHost);
|
||||||
clientConnections[remoteHost] = newConnection;
|
clientConnections[remoteHost] = newConnection;
|
||||||
InvokeOnUserJoined(newConnection);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove users that left
|
// Remove users that left
|
||||||
foreach (IPEndPoint key in clientConnections.Keys) {
|
foreach (IPEndPoint key in clientConnections.Keys) {
|
||||||
if (!activeIps.Contains(key.Address)) {
|
if (!activeIps.Contains(key.Address)) {
|
||||||
mUDPClient? user;
|
clientConnections.TryRemove(key, out _);
|
||||||
clientConnections.TryRemove(key, out user);
|
|
||||||
if (user == null) return;
|
|
||||||
InvokeOnUserLeft(user);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,7 +116,6 @@ namespace Mistox.Client {
|
|||||||
|
|
||||||
async Task TryAuth() {
|
async Task TryAuth() {
|
||||||
Console.WriteLine("Attempting to authenticate against the server...");
|
Console.WriteLine("Attempting to authenticate against the server...");
|
||||||
if (ServerConnection == null) return;
|
|
||||||
await ServerConnection.Send(new UserAuthPacket {
|
await ServerConnection.Send(new UserAuthPacket {
|
||||||
UserName = UserName,
|
UserName = UserName,
|
||||||
ConnectionKey = ConnectionKey
|
ConnectionKey = ConnectionKey
|
||||||
@@ -139,7 +134,7 @@ namespace Mistox.Client {
|
|||||||
/////////////////////////////
|
/////////////////////////////
|
||||||
|
|
||||||
// When (self) connects to the server
|
// When (self) connects to the server
|
||||||
public event Action? onServerConnected;
|
public event Action onServerConnected;
|
||||||
public void InvokeOnServerConnected(){
|
public void InvokeOnServerConnected(){
|
||||||
Alive = true;
|
Alive = true;
|
||||||
Task.Run(async () => {
|
Task.Run(async () => {
|
||||||
@@ -151,20 +146,20 @@ namespace Mistox.Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// When any user connects to the server (self) hasn't seen yet
|
// When any user connects to the server (self) hasn't seen yet
|
||||||
public event Action<mUDPClient>? onUserJoined;
|
public event Action<string> onUserJoined;
|
||||||
public void InvokeOnUserJoined(mUDPClient User){
|
public void InvokeOnUserJoined(string UserName){
|
||||||
onUserJoined?.Invoke(User);
|
onUserJoined?.Invoke(UserName);
|
||||||
}
|
}
|
||||||
|
|
||||||
// When user is within range of (self) and network rendering starts
|
// When user is within range of (self) and network rendering starts
|
||||||
public event Action<mUDPClient>? onUserConnected;
|
public event Action<mUDPClient> onUserConnected;
|
||||||
public void InvokeOnUserConnected(mUDPClient User){
|
public void InvokeOnUserConnected(mUDPClient ConnectedUser){
|
||||||
onUserConnected?.Invoke(User);
|
onUserConnected?.Invoke(ConnectedUser);
|
||||||
}
|
}
|
||||||
|
|
||||||
// When generic data is received
|
// When generic data is received
|
||||||
public event Action<mUDPClient?, dynamic>? onDataReceived;
|
public event Action<dynamic> onDataReceived;
|
||||||
public void InvokeOnReceived(dynamic DataPacket, mUDPClient? remote = null){
|
public void InvokeOnReceived(dynamic DataPacket, mUDPClient remote = null){
|
||||||
switch (DataPacket as object) {
|
switch (DataPacket as object) {
|
||||||
case HeartbeatPacket packet:
|
case HeartbeatPacket packet:
|
||||||
HeartbeatReceived(packet);
|
HeartbeatReceived(packet);
|
||||||
@@ -178,25 +173,25 @@ namespace Mistox.Client {
|
|||||||
Console.WriteLine("Received a null packet.");
|
Console.WriteLine("Received a null packet.");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
onDataReceived?.Invoke(remote, DataPacket);
|
onDataReceived?.Invoke(DataPacket);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// When user is no longer in range of (self) and network rendering stops
|
// When user is no longer in range of (self) and network rendering stops
|
||||||
public event Action<mUDPClient>? onUserDisconnected;
|
public event Action<mUDPClient> onUserDisconnected;
|
||||||
public void InvokeOnUserDisconnected(mUDPClient User){
|
public void InvokeOnUserDisconnected(mUDPClient DisconnectedUser){
|
||||||
onUserDisconnected?.Invoke(User);
|
onUserDisconnected?.Invoke(DisconnectedUser);
|
||||||
}
|
}
|
||||||
|
|
||||||
// When any user disconnects from the server that isn't (self)
|
// When any user disconnects from the server that isn't (self)
|
||||||
public event Action<mUDPClient>? onUserLeft;
|
public event Action<string> onUserLeft;
|
||||||
public void InvokeOnUserLeft(mUDPClient User){
|
public void InvokeOnUserLeft(string UserName){
|
||||||
onUserLeft?.Invoke(User);
|
onUserLeft?.Invoke(UserName);
|
||||||
}
|
}
|
||||||
|
|
||||||
// When (self) disconnects to the server
|
// When (self) disconnects to the server
|
||||||
public event Action? onServerDisconnected;
|
public event Action onServerDisconnected;
|
||||||
public void InvokeOnServerDisconnected(){
|
public void InvokeOnServerDisconnected(){
|
||||||
Alive = false;
|
Alive = false;
|
||||||
onServerDisconnected?.Invoke();
|
onServerDisconnected?.Invoke();
|
||||||
@@ -224,7 +219,6 @@ namespace Mistox.Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async Task SendServer<Packet>(Packet data) {
|
public async Task SendServer<Packet>(Packet data) {
|
||||||
if (ServerConnection == null) return;
|
|
||||||
await ServerConnection.Send( data );
|
await ServerConnection.Send( data );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,11 +48,10 @@ namespace Mistox.Client {
|
|||||||
await ns.ReadExactlyAsync(payloadBuffer, 0, Header.DataLength);
|
await ns.ReadExactlyAsync(payloadBuffer, 0, Header.DataLength);
|
||||||
|
|
||||||
// Create the dataobject
|
// Create the dataobject
|
||||||
object? dataobject = DataPacket.Deserialize( Header, payloadBuffer.AsMemory(DataPacket.HeaderLength, Header.DataLength) );
|
dynamic dataobject = DataPacket.Deserialize( Header, payloadBuffer.AsMemory(DataPacket.HeaderLength, Header.DataLength) );
|
||||||
if (dataobject != null) {
|
|
||||||
// Return the object
|
// Return the object
|
||||||
_parent.InvokeOnReceived(dataobject);
|
_parent.InvokeOnReceived(dataobject);
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|||||||
@@ -21,9 +21,9 @@ namespace Mistox.Client {
|
|||||||
UdpReceiveResult udpPacket = await _parent.ClientSocket.ReceiveAsync();
|
UdpReceiveResult udpPacket = await _parent.ClientSocket.ReceiveAsync();
|
||||||
|
|
||||||
// Try find the user
|
// Try find the user
|
||||||
mUDPClient? client = null;
|
mUDPClient client = null;
|
||||||
_parent.clientConnections.TryGetValue(udpPacket.RemoteEndPoint, out client);
|
bool clientExists = _parent.clientConnections.TryGetValue(udpPacket.RemoteEndPoint, out client);
|
||||||
if (client != null) {
|
if (clientExists) {
|
||||||
|
|
||||||
// Create the data readers
|
// Create the data readers
|
||||||
ReadOnlyMemory<byte> dataMemory = udpPacket.Buffer.AsMemory();
|
ReadOnlyMemory<byte> dataMemory = udpPacket.Buffer.AsMemory();
|
||||||
@@ -39,12 +39,10 @@ namespace Mistox.Client {
|
|||||||
ReadOnlyMemory<byte> data = dataMemory.Slice(DataPacket.HeaderLength, Header.DataLength);
|
ReadOnlyMemory<byte> data = dataMemory.Slice(DataPacket.HeaderLength, Header.DataLength);
|
||||||
|
|
||||||
// Extract the object
|
// Extract the object
|
||||||
object? buildObj = DataPacket.Deserialize( Header, data );
|
dynamic buildObj = DataPacket.Deserialize( Header, data );
|
||||||
if (buildObj != null) {
|
|
||||||
|
|
||||||
// Return the object
|
// Return the object
|
||||||
_parent.InvokeOnReceived( buildObj, client );
|
_parent.InvokeOnReceived( buildObj, client );
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
} catch( Exception e ) {
|
} catch( Exception e ) {
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ namespace Mistox {
|
|||||||
return new MistoxServer(listenerAddress, serverName);
|
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
|
// Build the network packet list
|
||||||
NetworkPacketDictionary.BuildNetworkPacketList();
|
NetworkPacketDictionary.BuildNetworkPacketList();
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net10.0</TargetFramework>
|
<TargetFramework>net10.0</TargetFramework>
|
||||||
<Nullable>enable</Nullable>
|
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -72,13 +72,13 @@ namespace Mistox.Server {
|
|||||||
// Events
|
// Events
|
||||||
/////////////////////////////
|
/////////////////////////////
|
||||||
|
|
||||||
public event Action<ServerConnection>? onUserConnected;
|
public event Action<ServerConnection> onUserConnected;
|
||||||
public void InvokeUserConnected( ServerConnection user ){
|
public void InvokeUserConnected( ServerConnection user ){
|
||||||
Connections[user.UserName] = user;
|
Connections[user.UserName] = user;
|
||||||
onUserConnected?.Invoke( user );
|
onUserConnected?.Invoke( user );
|
||||||
}
|
}
|
||||||
|
|
||||||
public event Action<ServerConnection, object>? onDataReceived;
|
public event Action<ServerConnection, object> onDataReceived;
|
||||||
public void InvokeDataReceived( ServerConnection user, dynamic data ){
|
public void InvokeDataReceived( ServerConnection user, dynamic data ){
|
||||||
switch (data) {
|
switch (data) {
|
||||||
case UserAuthPacket packet when !user.IsAuthenticated:
|
case UserAuthPacket packet when !user.IsAuthenticated:
|
||||||
@@ -101,7 +101,7 @@ namespace Mistox.Server {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public event Action<ServerConnection>? onUserDisconnected;
|
public event Action<ServerConnection> onUserDisconnected;
|
||||||
public void InvokeUserDisconnected( ServerConnection user ){
|
public void InvokeUserDisconnected( ServerConnection user ){
|
||||||
Connections.Remove(user.UserName, out _);
|
Connections.Remove(user.UserName, out _);
|
||||||
onUserDisconnected?.Invoke( user );
|
onUserDisconnected?.Invoke( user );
|
||||||
|
|||||||
@@ -55,11 +55,10 @@ namespace Mistox.Server {
|
|||||||
await ns.ReadExactlyAsync(payloadBuffer, 0, Header.DataLength, _cts.Token);
|
await ns.ReadExactlyAsync(payloadBuffer, 0, Header.DataLength, _cts.Token);
|
||||||
|
|
||||||
// Create the dataobject
|
// Create the dataobject
|
||||||
object? dataobject = DataPacket.Deserialize( Header, payloadBuffer.AsMemory(DataPacket.HeaderLength) );
|
dynamic dataobject = DataPacket.Deserialize( Header, payloadBuffer.AsMemory(DataPacket.HeaderLength) );
|
||||||
if (dataobject != null) {
|
|
||||||
// Return the object
|
// Return the object
|
||||||
_parent.InvokeDataReceived(this, dataobject);
|
_parent.InvokeDataReceived(this, dataobject);
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|||||||
@@ -32,8 +32,8 @@ namespace Mistox.Server {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
IPEndPoint? remote = client.Client.RemoteEndPoint as IPEndPoint;
|
IPEndPoint remote = client.Client.RemoteEndPoint as IPEndPoint;
|
||||||
if (remote != null) {
|
|
||||||
string connectionID = Guid.NewGuid().ToString();
|
string connectionID = Guid.NewGuid().ToString();
|
||||||
_parent.Connections[connectionID] = new ServerConnection( client, _parent, remote) {
|
_parent.Connections[connectionID] = new ServerConnection( client, _parent, remote) {
|
||||||
UserName = connectionID
|
UserName = connectionID
|
||||||
@@ -41,7 +41,6 @@ namespace Mistox.Server {
|
|||||||
|
|
||||||
Console.WriteLine( "New User Connected" );
|
Console.WriteLine( "New User Connected" );
|
||||||
}
|
}
|
||||||
}
|
|
||||||
Dispose();
|
Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -46,10 +46,10 @@ namespace Mistox {
|
|||||||
return writer.WrittenSpan.ToArray();
|
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
|
// 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}");
|
throw new Exception($"Unknown packet type ID: {Header.Type}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ namespace Mistox.Attributes {
|
|||||||
foreach (Assembly assembly in assemblies) {
|
foreach (Assembly assembly in assemblies) {
|
||||||
Type[] allClasses = assembly.GetTypes();
|
Type[] allClasses = assembly.GetTypes();
|
||||||
foreach( Type cur in allClasses) {
|
foreach( Type cur in allClasses) {
|
||||||
mPacket? attribute = cur.GetCustomAttribute<mPacket>(false);
|
mPacket attribute = cur.GetCustomAttribute<mPacket>(false);
|
||||||
if (attribute != null) {
|
if (attribute != null) {
|
||||||
|
|
||||||
// Generate a deterministic 16-bit ID based on the Type's FullName
|
// 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
|
// 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
|
// 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; } = 0;
|
public ushort ObjectId { get; set; }
|
||||||
|
|
||||||
// Packet payload data that im matching for in the dictionary
|
// Packet payload data that im matching for in the dictionary
|
||||||
public byte[] InnerPayload { get; set; } = {};
|
public byte[] InnerPayload { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class UserTableObject {
|
public class UserTableObject {
|
||||||
public string UserName { get; set; } = string.Empty;
|
public string UserName { get; set; }
|
||||||
public IPAddress IpAddress { get; set; } = new([]);
|
public IPAddress IpAddress { get; set; }
|
||||||
public Vector3 Position { get; set; } = new();
|
public Vector3 Position { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
// Not implemented
|
// Not implemented
|
||||||
// Packet sent to all clients when a global position update occurrs
|
// Packet sent to all clients when a global position update occurrs
|
||||||
[mPacket]
|
[mPacket]
|
||||||
public class HeartbeatPacket {
|
public class HeartbeatPacket {
|
||||||
public Vector3 Position { get; set; } = new();
|
public Vector3 Position { get; set; }
|
||||||
public List<UserTableObject> ServerList { get; set; } = new();
|
public List<UserTableObject> ServerList { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
// Not implimented
|
// Not implimented
|
||||||
// Packet sent to all clients when a users TCP stream disconnects from the server
|
// Packet sent to all clients when a users TCP stream disconnects from the server
|
||||||
[mPacket]
|
[mPacket]
|
||||||
public class UserDisconnectedPacket {
|
public class UserDisconnectedPacket {
|
||||||
public string UserName { get; set; } = string.Empty;
|
public string UserName { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -10,7 +10,6 @@ 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
|
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
|
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
|
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
|
Set the network bubble distance [ render distance ] on the server and pass to the clients
|
||||||
|
|||||||
Reference in new issue
Block a user