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