init commit over here
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user