woomer
goomer
This commit is contained in:
parent
80ae240d45
commit
c45cf59dac
9 changed files with 83 additions and 17 deletions
|
@ -9,7 +9,12 @@ using CircleScape.DAL;
|
||||||
namespace CircleScape {
|
namespace CircleScape {
|
||||||
class Entrypoint {
|
class Entrypoint {
|
||||||
static void Main(string[] args) {
|
static void Main(string[] args) {
|
||||||
|
var server = new Kneesocks.Server<PendingConnection>(6770, PoolManager.Pool);
|
||||||
|
server.Start();
|
||||||
|
|
||||||
|
while(true) {
|
||||||
|
// physics and game logic processing loop
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,8 +6,21 @@ using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Kneesocks {
|
namespace Kneesocks {
|
||||||
public abstract class Connection {
|
public class Connection {
|
||||||
public UInt64 Id { get; private set; }
|
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 TcpClient Socket;
|
||||||
private NetworkStream Stream;
|
private NetworkStream Stream;
|
||||||
|
|
||||||
|
@ -19,14 +32,17 @@ namespace Kneesocks {
|
||||||
private Dictionary<string, string> Headers =
|
private Dictionary<string, string> Headers =
|
||||||
new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
||||||
|
|
||||||
protected Connection(UInt64 id, TcpClient sock) {
|
public Connection(TcpClient sock) {
|
||||||
Id = id;
|
|
||||||
Socket = sock;
|
Socket = sock;
|
||||||
Socket.ReceiveTimeout = 1;
|
Socket.ReceiveTimeout = 1;
|
||||||
Stream = sock.GetStream();
|
Stream = sock.GetStream();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Connection(Connection conn) {
|
public Connection(UInt64 id, TcpClient sock) : this(sock) {
|
||||||
|
Id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Connection(Connection conn) {
|
||||||
Id = conn.Id;
|
Id = conn.Id;
|
||||||
Socket = conn.Socket;
|
Socket = conn.Socket;
|
||||||
Stream = Socket.GetStream();
|
Stream = Socket.GetStream();
|
||||||
|
@ -60,7 +76,7 @@ namespace Kneesocks {
|
||||||
|
|
||||||
// called after the client successfully handshakes
|
// called after the client successfully handshakes
|
||||||
public virtual void OnOpen() { }
|
public virtual void OnOpen() { }
|
||||||
|
|
||||||
// called when the thread manager iterates through
|
// called when the thread manager iterates through
|
||||||
// the thread list and stops on this thread
|
// the thread list and stops on this thread
|
||||||
public virtual void OnParse() { }
|
public virtual void OnParse() { }
|
||||||
|
|
|
@ -46,6 +46,7 @@ namespace Kneesocks {
|
||||||
if(id == 0)
|
if(id == 0)
|
||||||
id = InternalCounter++;
|
id = InternalCounter++;
|
||||||
|
|
||||||
|
connection.Id = id;
|
||||||
Connections.Add(id, connection);
|
Connections.Add(id, connection);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,10 +2,51 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
using System.Net;
|
||||||
|
|
||||||
namespace Kneesocks {
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -52,9 +52,8 @@ namespace Kneesocks {
|
||||||
public void ManageStack() {
|
public void ManageStack() {
|
||||||
while(Running && (Count > 0 || RunWithNoClients)) {
|
while(Running && (Count > 0 || RunWithNoClients)) {
|
||||||
for(var i = Count - 1; i >= 0 && Running; ++i) {
|
for(var i = Count - 1; i >= 0 && Running; ++i) {
|
||||||
Clients[i].OnParse();
|
var client = Clients[i];
|
||||||
|
client
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -67,9 +67,8 @@ namespace Kneesocks {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class NumericExtensions {
|
public static class NumericExtensions {
|
||||||
public static T Unpack<T>(this byte[] bytes, int offset = 0) {
|
/*public static T Unpack<T>(this byte[] bytes, int offset = 0) {
|
||||||
sizeof
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,6 @@ using System.Threading.Tasks;
|
||||||
namespace CircleScape {
|
namespace CircleScape {
|
||||||
class ActiveConnection : Kneesocks.Connection {
|
class ActiveConnection : Kneesocks.Connection {
|
||||||
public ActiveConnection(UInt32 id, TcpClient sock) : base(id, sock) { }
|
public ActiveConnection(UInt32 id, TcpClient sock) : base(id, sock) { }
|
||||||
|
|
||||||
public ActiveConnection(PendingConnection conn) : base(conn) { }
|
public ActiveConnection(PendingConnection conn) : base(conn) { }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,5 +8,6 @@ using System.Threading.Tasks;
|
||||||
namespace CircleScape {
|
namespace CircleScape {
|
||||||
class PendingConnection : Kneesocks.Connection {
|
class PendingConnection : Kneesocks.Connection {
|
||||||
public PendingConnection(UInt32 id, TcpClient sock) : base(id, sock) { }
|
public PendingConnection(UInt32 id, TcpClient sock) : base(id, sock) { }
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,11 @@ namespace CircleScape {
|
||||||
static class PoolManager {
|
static class PoolManager {
|
||||||
private static Pool<PendingConnection> PendingConnectionsPool;
|
private static Pool<PendingConnection> PendingConnectionsPool;
|
||||||
private static Pool<ActiveConnection> ActiveConnectionsPool;
|
private static Pool<ActiveConnection> ActiveConnectionsPool;
|
||||||
|
public static Pool<PendingConnection> Pool {
|
||||||
|
get {
|
||||||
|
return PendingConnectionsPool;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static PoolManager() {
|
static PoolManager() {
|
||||||
PendingConnectionsPool = new Pool<PendingConnection> {
|
PendingConnectionsPool = new Pool<PendingConnection> {
|
||||||
|
|
Loading…
Reference in a new issue