init commit over here

This commit is contained in:
2025-05-12 18:04:18 -07:00
commit c00d3b1426
21 changed files with 973 additions and 0 deletions
+71
View File
@@ -0,0 +1,71 @@
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
//IP Range Updater
namespace MistoxServer.Client {
public class mTCPClient : IDisposable {
TcpClient Server;
public event EventHandler onConnected;
public event EventHandler onReceived;
public event EventHandler onDisconnected;
public bool Alive;
bool notified = false;
public mTCPClient( IPEndPoint ServerAddress ) {
Server = new TcpClient( AddressFamily.InterNetwork );
Server.NoDelay = true;
Server.Connect( ServerAddress );
Alive = true;
Thread RThread = new Thread(async () => { await ReceiveThread(); });
RThread.Start();
}
async Task ReceiveThread() {
using( NetworkStream ns = Server.GetStream() ) {
try {
while( Alive ) {
if( Server.Connected && !notified ) {
Console.WriteLine( "Connected to server" );
onConnected?.Invoke( new object(), new EventArgs() );
notified = true;
} else if( !Server.Connected ) {
Console.WriteLine( "Disconnected from server" );
onDisconnected?.Invoke( new object(), new EventArgs() );
Alive = false;
}
byte[] StreamData = new byte[1024];
int bytesRead = await ns.ReadAsync(StreamData, 0, StreamData.Length);
if( bytesRead > 0 ) {
dynamic data = mSerialize.tReceive( StreamData.Sub( 0, bytesRead ) );
if( data != null ) {
onReceived?.Invoke( data, new EventArgs() );
}
}
}
} catch( Exception ) {
Console.WriteLine( "You have disconnected from the server" );
onDisconnected?.Invoke( new object(), new EventArgs() );
Alive = false;
}
}
}
public async Task Send<Packet>(Packet packet) {
byte[] data = mSerialize.PacketSerialize( packet );
await Server.GetStream().WriteAsync( data, 0, data.Length );
}
public void Dispose() {
Alive = false;
Server.Close();
Server = null;
}
}
}
+66
View File
@@ -0,0 +1,66 @@
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
// Network Sync
namespace MistoxServer.Client {
public class mUDPClient : IDisposable {
public event EventHandler onReceived;
ServerMode Mode;
Socket udpClient;
bool Alive;
int Port;
public mUDPClient( IPEndPoint ServerAddress, ServerMode mode ) {
udpClient = new Socket( AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp );
udpClient.SetSocketOption( SocketOptionLevel.IP, SocketOptionName.ReuseAddress, true );
udpClient.SetSocketOption( SocketOptionLevel.IP, SocketOptionName.IpTimeToLive, 128 );
udpClient.Bind( new IPEndPoint( IPAddress.Any, ServerAddress.Port ) );
Alive = true;
Port = ServerAddress.Port;
Mode = mode;
Thread Client = new Thread(async() => { await ReceiveThread(); });
Client.Start();
}
async Task ReceiveThread() {
while( Alive ) {
try {
byte[] buffer = new byte[1024];
int bytesRead = await udpClient.ReceiveAsync( buffer );
if (Mode == ServerMode.Passive) {
onReceived?.Invoke( buffer.Sub( 0, bytesRead ), new EventArgs() );
} else if (Mode == ServerMode.Authoritative) {
dynamic data = mSerialize.uReceive( buffer.Sub(0, bytesRead) );
if( data != null ) {
onReceived?.Invoke( data, new EventArgs() );
}
}
} catch( Exception ) {
}
}
}
public async Task Send<Packet>( Packet Data, IPEndPoint remoteHost ) {
if (Mode == ServerMode.Authoritative) {
byte[] byteData = mSerialize.PacketSerialize( Data );
await udpClient.SendToAsync( byteData, SocketFlags.None, new IPEndPoint( remoteHost.Address, Port ) );
} else if (Mode == ServerMode.Passive) {
byte[] byteData = Data as byte[];
await udpClient.SendToAsync( byteData, SocketFlags.None, new IPEndPoint( remoteHost.Address, Port ) );
}
}
public void Dispose() {
Alive = false;
udpClient.Close();
udpClient = null;
}
}
}
+59
View File
@@ -0,0 +1,59 @@
using MistoxServer.Client;
using System;
using System.Net;
using System.Threading.Tasks;
namespace MistoxServer {
public class ClientInterface : IMistoxServer {
mTCPClient SlowUpdate;
mUDPClient FastUpdate;
IPEndPoint mPEndPoint;
public event EventHandler onConnected;
public event EventHandler onSlowReceive;
public event EventHandler onFastReceive;
public event EventHandler onDisconnected;
public ClientInterface( string IpOrHostName, int Port ) {
// Get Server IP
IPHostEntry host = Dns.GetHostEntry( IpOrHostName );
foreach( IPAddress entry in host.AddressList ) {
if ( entry.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork ) {
mPEndPoint = new IPEndPoint( entry, Port );
}
}
Console.WriteLine( "The client is initilized and trying to connect to the server at ip : " + mPEndPoint.Address );
// Make a UDP connection to the server
try {
FastUpdate = new mUDPClient( mPEndPoint, ServerMode.Authoritative );
FastUpdate.onReceived += (object o, EventArgs e) => { onFastReceive?.Invoke( o, e ); };
} catch( Exception e ) {
Console.WriteLine( "An error has occured with the connection to the server. Error { " );
Console.WriteLine( e.ToString() );
Console.WriteLine( "}" );
}
// Make a TCP connection to the server
try {
SlowUpdate = new mTCPClient( mPEndPoint );
SlowUpdate.onConnected += ( object o, EventArgs e ) => { onConnected?.Invoke( o, e ); };
SlowUpdate.onReceived += ( object o, EventArgs e ) => { onSlowReceive?.Invoke( o, e ); };
SlowUpdate.onDisconnected += ( object o, EventArgs e ) => { onDisconnected?.Invoke( o, e ); };
} catch( Exception e ) {
Console.WriteLine( "An error has occured with the connection to the server. Error { " );
Console.WriteLine( e.ToString() );
Console.WriteLine( "}" );
}
}
public async Task Send<Packet>(Packet data, SendType speed) {
if (SendType.SlowUpdate == speed) {
await SlowUpdate.Send( data );
} else {
await FastUpdate.Send( data, mPEndPoint );
}
}
}
}
+50
View File
@@ -0,0 +1,50 @@
using System;
using System.Net;
using System.Threading.Tasks;
namespace MistoxServer {
public partial class mServer {
public static IMistoxServer newServer(int port, ServerMode mode) {
return new ServerInterface(port, mode);
}
public static IMistoxServer newClient(string ServerIPOrHostName, int Port) {
int index = 0;
IPAddress[] remoteAddress = Dns.GetHostAddresses(ServerIPOrHostName);
for( int i=0; i< remoteAddress.Length; i++ ) {
if( remoteAddress[i].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork || remoteAddress [i].AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6 ) {
index = i;
}
}
string ipAddress = remoteAddress[index].ToString();
if ( remoteAddress.Length > 0) {
return new ClientInterface(ipAddress, Port);
} else {
Console.WriteLine("The server at " + ServerIPOrHostName + " doesn't exit or cannot be found");
return null;
}
}
}
public interface IMistoxServer {
public event EventHandler onConnected;
public event EventHandler onSlowReceive;
public event EventHandler onFastReceive;
public event EventHandler onDisconnected;
public Task Send<Packet>(Packet data, SendType speed);
}
public enum SendType {
SlowUpdate,
FastUpdate
}
public enum ServerMode {
Passive,
Authoritative
}
}
+57
View File
@@ -0,0 +1,57 @@
using MistoxServer.Client;
using MistoxServer.Server;
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
namespace MistoxServer {
public class ServerInterface : IMistoxServer {
mUDPClient FastUpdateServer;
mTCPListener SlowUpdateServer;
List<Connection> Connections = new List<Connection>();
public event EventHandler onConnected;
public event EventHandler onSlowReceive;
public event EventHandler onFastReceive;
public event EventHandler onDisconnected;
public ServerInterface( int port, ServerMode mode ) {
FastUpdateServer = new mUDPClient( new IPEndPoint( IPAddress.IPv6Any, port ), mode );
SlowUpdateServer = new mTCPListener( port, mode );
SlowUpdateServer.onConnected += OnConnected;
FastUpdateServer.onReceived += ( object o, EventArgs e ) => { onFastReceive?.Invoke( o, e ); };
SlowUpdateServer.onDisconnected += OnDisconnected;
Console.WriteLine( "The Server is initilized and waiting for clients to connect at port : " + port );
}
void OnConnected( object sender, EventArgs e ) {
Connection user = (Connection)sender;
onConnected?.Invoke( sender, e );
user.slowClient.onReceived+= ( object o, EventArgs e ) => { onSlowReceive?.Invoke( o, e ); };
user.slowClient.onDisconnected += OnDisconnected;
Connections.Add( user );
}
void OnDisconnected( object sender, EventArgs e ) {
Connection user = (Connection)sender;
onDisconnected?.Invoke( sender, e );
Connections.Remove( user );
}
public async Task Send<Packet>( Packet data, SendType speed ) {
if (speed == SendType.SlowUpdate) {
foreach( Connection cur in Connections ) {
await cur.slowClient.Send( data );
}
} else {
foreach( Connection cur in Connections ) {
await FastUpdateServer.Send( data, cur.fastClient );
}
}
}
}
}
+11
View File
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MsgPack.Cli" Version="1.0.1" />
</ItemGroup>
</Project>
+31
View File
@@ -0,0 +1,31 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31729.503
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MistoxServer", "MistoxServer.csproj", "{947D92F0-B737-4D1D-857B-F4EAB70D6980}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CLI", "..\CLI\CLI.csproj", "{B8EA2F11-62A3-45B2-8FDC-E734FD6B87E1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{947D92F0-B737-4D1D-857B-F4EAB70D6980}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{947D92F0-B737-4D1D-857B-F4EAB70D6980}.Debug|Any CPU.Build.0 = Debug|Any CPU
{947D92F0-B737-4D1D-857B-F4EAB70D6980}.Release|Any CPU.ActiveCfg = Release|Any CPU
{947D92F0-B737-4D1D-857B-F4EAB70D6980}.Release|Any CPU.Build.0 = Release|Any CPU
{B8EA2F11-62A3-45B2-8FDC-E734FD6B87E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B8EA2F11-62A3-45B2-8FDC-E734FD6B87E1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B8EA2F11-62A3-45B2-8FDC-E734FD6B87E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B8EA2F11-62A3-45B2-8FDC-E734FD6B87E1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {59213D9A-3888-4C3D-9EB7-BB116FAB1EB7}
EndGlobalSection
EndGlobal
+10
View File
@@ -0,0 +1,10 @@
using System.Net;
namespace MistoxServer.Server {
public class Connection {
public mTCPServer slowClient { get; set; }
public IPEndPoint fastClient { get; set; }
}
}
+58
View File
@@ -0,0 +1,58 @@
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
// Client Connections
namespace MistoxServer.Server {
public class mTCPListener : IDisposable {
public event EventHandler onConnected;
public event EventHandler onDisconnected;
public event EventHandler onReceive;
TcpListener Listener;
ServerMode ServerMode;
bool Alive;
int port;
public mTCPListener( int Port, ServerMode mode ) {
port = Port;
Alive = true;
ServerMode = mode;
Thread ConnectionThread = new Thread(async () => await ListenerThread() );
ConnectionThread.Start();
}
async Task ListenerThread() {
Listener = new TcpListener( IPAddress.Any, port );
Listener.Start();
while( Alive ) {
TcpClient client = await Listener.AcceptTcpClientAsync();
Connection user = new Connection(){
slowClient = new mTCPServer(client, ServerMode),
fastClient = new IPEndPoint( ((IPEndPoint)client.Client.RemoteEndPoint).Address, port )
};
Console.WriteLine( "New User Connected" );
onConnected?.Invoke( user, new EventArgs() );
user.slowClient.onDisconnected += onDisconnected;
user.slowClient.onReceived += onReceive;
Thread receiveThread = new Thread(async () => await user.slowClient.ReceiveThread(user) );
receiveThread.Start();
}
}
public void Dispose() {
Alive = false;
Listener.Stop();
Listener = null;
}
}
}
+58
View File
@@ -0,0 +1,58 @@
using System;
using System.Net.Sockets;
using System.Threading.Tasks;
namespace MistoxServer.Server {
public class mTCPServer : IDisposable {
public TcpClient slowClient;
ServerMode Mode;
public event EventHandler onReceived;
public event EventHandler onDisconnected;
public mTCPServer( TcpClient client, ServerMode mode ) {
slowClient = client;
slowClient.Client.NoDelay = true;
Mode = mode;
}
bool Alive = true;
public async Task ReceiveThread( Connection Client ) {
bool connected = true;
while( Alive && connected ) {
try {
byte[] StreamData = new byte[1024];
int bytesRead = await slowClient.GetStream().ReadAsync(StreamData, 0, StreamData.Length);
if( bytesRead > 0 ) {
if ( Mode == ServerMode.Passive) {
onReceived?.Invoke( StreamData.Sub( 0, bytesRead ), new EventArgs() );
} else if ( Mode == ServerMode.Authoritative) {
dynamic data = mSerialize.tReceive( StreamData.Sub( 0, bytesRead ) );
if( data != null ) {
onReceived?.Invoke( data, new EventArgs() );
}
}
}
} catch( Exception ) {
Console.WriteLine( "User disconnected" );
connected = false;
onDisconnected?.Invoke( Client, new EventArgs() );
}
}
}
public async Task Send<T>( T packet ) {
if( Mode == ServerMode.Authoritative ) {
byte[] byteData = mSerialize.PacketSerialize( packet );
await slowClient.GetStream().WriteAsync( byteData, 0, byteData.Length );
} else if( Mode == ServerMode.Passive ) {
byte[] byteData = packet as byte[];
await slowClient.GetStream().WriteAsync( byteData, 0, byteData.Length );
}
}
public void Dispose() {
Alive = false;
}
}
}
+95
View File
@@ -0,0 +1,95 @@
using System;
using System.IO;
using System.Net;
using System.Text;
using MsgPack.Serialization;
namespace MistoxServer {
static class Extensions {
public static T[] Join<T>( this T[] first, T[] second ) {
T[] bytes = new T[first.Length + second.Length];
Buffer.BlockCopy( first, 0, bytes, 0, first.Length );
Buffer.BlockCopy( second, 0, bytes, first.Length, second.Length );
return bytes;
}
public static T[] Sub<T>( this T[] data, int index, int length ) {
T[] result = new T[length];
Array.Copy( data, index, result, 0, length );
return result;
}
}
public class mSerialize {
public static byte[] PacketSerialize<T>( T Packet ) {
MessagePackSerializer serializer = MessagePackSerializer.Get<T>();
using( MemoryStream stream = new MemoryStream() ) {
serializer.Pack( stream, Packet );
byte[] typename = Encoding.UTF8.GetBytes(typeof(T).FullName + "," + typeof(T).Assembly.FullName);
byte[] typelength = BitConverter.GetBytes(typename.Length);
byte[] packetdata = stream.ToArray();
byte[] paketlength = BitConverter.GetBytes(packetdata.Length);
return typelength.Join( typename ).Join( paketlength ).Join( packetdata );
}
}
public static object PacketDeserialize( string typeData, byte[] Data ) {
Type type = Type.GetType( typeData );
MessagePackSerializer Serilizer = MessagePackSerializer.Get(type);
using( MemoryStream ms = new MemoryStream( Data ) ) {
return (object)Serilizer.Unpack( ms );
}
}
static byte[] TBufferedData = new byte[0];
public static dynamic tReceive( byte [] BytesRead ) {
TBufferedData = TBufferedData.Join( BytesRead );
if( TBufferedData.Length > 4 ) {
int typeLength = BitConverter.ToInt32( TBufferedData.Sub(0, 4) );
if( TBufferedData.Length > (8 + typeLength) ) {
int dataLength = BitConverter.ToInt32( TBufferedData.Sub( typeLength + 4, 4 ) );
int TotalLength = 8 + typeLength + dataLength;
if ( TBufferedData.Length >= TotalLength ) {
string typeData = Encoding.UTF8.GetString( TBufferedData.Sub(4, typeLength) );
if ( typeData.Substring(0, 13) != "System.Object" ) {
byte[] dataBytes = TBufferedData.Sub( (typeLength + 8), dataLength );
dynamic data = mSerialize.PacketDeserialize( typeData, dataBytes );
TBufferedData = TBufferedData.Sub( TotalLength, TBufferedData.Length - TotalLength );
return data;
} else {
TBufferedData = TBufferedData.Sub( TotalLength, TBufferedData.Length - TotalLength );
}
}
}
}
return null;
}
static byte[] UBufferedData = new byte[0];
public static dynamic uReceive( byte [] BytesRead ) {
UBufferedData = UBufferedData.Join( BytesRead );
if( UBufferedData.Length > 4 ) {
int typeLength = BitConverter.ToInt32( UBufferedData.Sub(0, 4) );
if( UBufferedData.Length > (8 + typeLength) ) {
int dataLength = BitConverter.ToInt32( UBufferedData.Sub( typeLength + 4, 4 ) );
int TotalLength = 8 + typeLength + dataLength;
if( UBufferedData.Length >= TotalLength ) {
string typeData = Encoding.UTF8.GetString( UBufferedData.Sub(4, typeLength) );
if( typeData.Substring( 0, 13 ) != "System.Object" ) {
byte[] dataBytes = UBufferedData.Sub( (typeLength + 8), dataLength );
dynamic data = mSerialize.PacketDeserialize( typeData, dataBytes );
UBufferedData = UBufferedData.Sub( TotalLength, UBufferedData.Length - TotalLength );
return data;
} else {
UBufferedData = UBufferedData.Sub( TotalLength, UBufferedData.Length - TotalLength );
}
}
}
}
return null;
}
}
}