goomer
This commit is contained in:
Malloc of Kuzkycyziklistan 2017-05-04 07:21:27 -05:00
parent 80ae240d45
commit c45cf59dac
9 changed files with 83 additions and 17 deletions

View file

@ -9,7 +9,12 @@ using CircleScape.DAL;
namespace CircleScape {
class Entrypoint {
static void Main(string[] args) {
var server = new Kneesocks.Server<PendingConnection>(6770, PoolManager.Pool);
server.Start();
while(true) {
// physics and game logic processing loop
}
}
}
}

View file

@ -6,8 +6,21 @@ using System.Text;
using System.Threading.Tasks;
namespace Kneesocks {
public abstract class Connection {
public UInt64 Id { get; private set; }
public class Connection {
public UInt64? _Id = null;
public UInt64 Id {
get {
if(_Id == null)
throw new ArgumentNullException();
else
return (UInt64)_Id;
}
set {
if(_Id == null)
_Id = value;
}
}
private TcpClient Socket;
private NetworkStream Stream;
@ -19,14 +32,17 @@ namespace Kneesocks {
private Dictionary<string, string> Headers =
new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
protected Connection(UInt64 id, TcpClient sock) {
Id = id;
public Connection(TcpClient sock) {
Socket = sock;
Socket.ReceiveTimeout = 1;
Stream = sock.GetStream();
}
protected Connection(Connection conn) {
public Connection(UInt64 id, TcpClient sock) : this(sock) {
Id = id;
}
public Connection(Connection conn) {
Id = conn.Id;
Socket = conn.Socket;
Stream = Socket.GetStream();
@ -60,7 +76,7 @@ namespace Kneesocks {
// called after the client successfully handshakes
public virtual void OnOpen() { }
// called when the thread manager iterates through
// the thread list and stops on this thread
public virtual void OnParse() { }

View file

@ -46,6 +46,7 @@ namespace Kneesocks {
if(id == 0)
id = InternalCounter++;
connection.Id = id;
Connections.Add(id, connection);
}
}

View file

@ -2,10 +2,51 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Net.Sockets;
using System.Net;
namespace Kneesocks {
public abstract class Server {
public class Server<T> where T : Connection {
private TcpListener Socket;
private Thread Listener = null;
private Pool<T> ConnectionPool = null;
private bool Started = false;
public UInt16 Port { get; private set; }
public Server(UInt16 port, Pool<T> pool) {
Port = port;
Socket = new TcpListener(IPAddress.Any, port);
Listener = new Thread(new ThreadStart(ListenThread));
ConnectionPool = pool;
}
public void Start() {
if(!Started) {
Listener.Start();
Started = true;
}
}
public void Stop() {
if(Started) {
Started = false;
Listener.Join();
}
}
private void ListenThread() {
Socket.Start();
while(Started) {
if(Socket.Pending())
ConnectionPool.AddConnection(new Connection(Socket.AcceptTcpClient()));
Thread.Sleep(100);
}
Socket.Stop();
}
}
}

View file

@ -52,9 +52,8 @@ namespace Kneesocks {
public void ManageStack() {
while(Running && (Count > 0 || RunWithNoClients)) {
for(var i = Count - 1; i >= 0 && Running; ++i) {
Clients[i].OnParse();
var client = Clients[i];
client
}
}

View file

@ -67,9 +67,8 @@ namespace Kneesocks {
}
public static class NumericExtensions {
public static T Unpack<T>(this byte[] bytes, int offset = 0) {
sizeof
/*public static T Unpack<T>(this byte[] bytes, int offset = 0) {
return 0;
}
}*/
}
}

View file

@ -8,7 +8,6 @@ using System.Threading.Tasks;
namespace CircleScape {
class ActiveConnection : Kneesocks.Connection {
public ActiveConnection(UInt32 id, TcpClient sock) : base(id, sock) { }
public ActiveConnection(PendingConnection conn) : base(conn) { }
}
}

View file

@ -8,5 +8,6 @@ using System.Threading.Tasks;
namespace CircleScape {
class PendingConnection : Kneesocks.Connection {
public PendingConnection(UInt32 id, TcpClient sock) : base(id, sock) { }
}
}

View file

@ -10,6 +10,11 @@ namespace CircleScape {
static class PoolManager {
private static Pool<PendingConnection> PendingConnectionsPool;
private static Pool<ActiveConnection> ActiveConnectionsPool;
public static Pool<PendingConnection> Pool {
get {
return PendingConnectionsPool;
}
}
static PoolManager() {
PendingConnectionsPool = new Pool<PendingConnection> {