From c45cf59dac91285c67471d61aa10034813cb3037 Mon Sep 17 00:00:00 2001 From: Malloc of Kuzkycyziklistan Date: Thu, 4 May 2017 07:21:27 -0500 Subject: [PATCH] woomer goomer --- server/Entrypoint.cs | 7 ++++- server/Kneesocks/Connection.cs | 28 ++++++++++++++---- server/Kneesocks/Pool.cs | 1 + server/Kneesocks/Server.cs | 47 +++++++++++++++++++++++++++++-- server/Kneesocks/Stack.cs | 5 ++-- server/Kneesocks/Utilities.cs | 5 ++-- server/Socks/ActiveConnection.cs | 1 - server/Socks/PendingConnection.cs | 1 + server/Socks/PoolManager.cs | 5 ++++ 9 files changed, 83 insertions(+), 17 deletions(-) diff --git a/server/Entrypoint.cs b/server/Entrypoint.cs index 670558e..978cd56 100644 --- a/server/Entrypoint.cs +++ b/server/Entrypoint.cs @@ -9,7 +9,12 @@ using CircleScape.DAL; namespace CircleScape { class Entrypoint { static void Main(string[] args) { - + var server = new Kneesocks.Server(6770, PoolManager.Pool); + server.Start(); + + while(true) { + // physics and game logic processing loop + } } } } diff --git a/server/Kneesocks/Connection.cs b/server/Kneesocks/Connection.cs index 411e6b3..1b78da5 100644 --- a/server/Kneesocks/Connection.cs +++ b/server/Kneesocks/Connection.cs @@ -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 Headers = new Dictionary(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() { } diff --git a/server/Kneesocks/Pool.cs b/server/Kneesocks/Pool.cs index a52087b..5ce2472 100644 --- a/server/Kneesocks/Pool.cs +++ b/server/Kneesocks/Pool.cs @@ -46,6 +46,7 @@ namespace Kneesocks { if(id == 0) id = InternalCounter++; + connection.Id = id; Connections.Add(id, connection); } } diff --git a/server/Kneesocks/Server.cs b/server/Kneesocks/Server.cs index 0131e4e..16065ad 100644 --- a/server/Kneesocks/Server.cs +++ b/server/Kneesocks/Server.cs @@ -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 where T : Connection { + private TcpListener Socket; + private Thread Listener = null; + private Pool ConnectionPool = null; + private bool Started = false; + public UInt16 Port { get; private set; } + + public Server(UInt16 port, Pool 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(); + } } } + diff --git a/server/Kneesocks/Stack.cs b/server/Kneesocks/Stack.cs index f85a2a8..d8e9407 100644 --- a/server/Kneesocks/Stack.cs +++ b/server/Kneesocks/Stack.cs @@ -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 } } diff --git a/server/Kneesocks/Utilities.cs b/server/Kneesocks/Utilities.cs index 3ce617d..9958031 100644 --- a/server/Kneesocks/Utilities.cs +++ b/server/Kneesocks/Utilities.cs @@ -67,9 +67,8 @@ namespace Kneesocks { } public static class NumericExtensions { - public static T Unpack(this byte[] bytes, int offset = 0) { - sizeof + /*public static T Unpack(this byte[] bytes, int offset = 0) { return 0; - } + }*/ } } diff --git a/server/Socks/ActiveConnection.cs b/server/Socks/ActiveConnection.cs index 9ed4340..6c56e91 100644 --- a/server/Socks/ActiveConnection.cs +++ b/server/Socks/ActiveConnection.cs @@ -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) { } } } diff --git a/server/Socks/PendingConnection.cs b/server/Socks/PendingConnection.cs index 99ed41b..3a6375a 100644 --- a/server/Socks/PendingConnection.cs +++ b/server/Socks/PendingConnection.cs @@ -8,5 +8,6 @@ using System.Threading.Tasks; namespace CircleScape { class PendingConnection : Kneesocks.Connection { public PendingConnection(UInt32 id, TcpClient sock) : base(id, sock) { } + } } diff --git a/server/Socks/PoolManager.cs b/server/Socks/PoolManager.cs index a2c87c2..73583c9 100644 --- a/server/Socks/PoolManager.cs +++ b/server/Socks/PoolManager.cs @@ -10,6 +10,11 @@ namespace CircleScape { static class PoolManager { private static Pool PendingConnectionsPool; private static Pool ActiveConnectionsPool; + public static Pool Pool { + get { + return PendingConnectionsPool; + } + } static PoolManager() { PendingConnectionsPool = new Pool {