server: autoselect connection type
This commit is contained in:
@@ -2,35 +2,22 @@ package ru.m.tankz.server;
|
|||||||
|
|
||||||
import ru.m.core.connect.IConnection.IPacketBuilder;
|
import ru.m.core.connect.IConnection.IPacketBuilder;
|
||||||
import haxework.provider.Provider;
|
import haxework.provider.Provider;
|
||||||
import ru.m.core.connect.neko.NekoWebConnection;
|
|
||||||
import ru.m.core.connect.neko.NekoConnection;
|
|
||||||
import haxework.log.TraceLogger;
|
import haxework.log.TraceLogger;
|
||||||
import ru.m.tankz.server.session.Session;
|
import ru.m.tankz.server.session.Session;
|
||||||
import haxe.io.Bytes;
|
import haxe.io.Bytes;
|
||||||
import sys.net.Socket;
|
import sys.net.Socket;
|
||||||
import neko.net.ThreadServer;
|
import neko.net.ThreadServer;
|
||||||
|
|
||||||
enum ServerMode {
|
|
||||||
NORMAL_SOCKET;
|
|
||||||
WEB_SOCKET;
|
|
||||||
}
|
|
||||||
|
|
||||||
class Server extends ThreadServer<Session, Bytes> {
|
class Server extends ThreadServer<Session, Bytes> {
|
||||||
|
|
||||||
private static inline var TAG = "Server";
|
private static inline var TAG = "Server";
|
||||||
|
|
||||||
private var mode:ServerMode;
|
public function new() {
|
||||||
|
|
||||||
public function new(mode:ServerMode) {
|
|
||||||
super();
|
super();
|
||||||
this.mode = mode;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override function clientConnected(s:Socket):Session {
|
override function clientConnected(s:Socket):Session {
|
||||||
var session = new Session(s, switch(mode) {
|
var session = new Session(s);
|
||||||
case ServerMode.NORMAL_SOCKET: NekoConnection;
|
|
||||||
case ServerMode.WEB_SOCKET: NekoWebConnection;
|
|
||||||
});
|
|
||||||
L.d(TAG, "Client connected");
|
L.d(TAG, "Client connected");
|
||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
@@ -44,16 +31,14 @@ class Server extends ThreadServer<Session, Bytes> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override function clientMessage(session:Session, bytes:Bytes) {
|
override function clientMessage(session:Session, bytes:Bytes) {
|
||||||
session.connection.pushData(bytes);
|
session.pushData(bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function main() {
|
public static function main() {
|
||||||
L.push(new TraceLogger());
|
L.push(new TraceLogger());
|
||||||
L.d(TAG, "Running");
|
L.d(TAG, "Running");
|
||||||
Provider.set(IPacketBuilder, new PacketBuilder());
|
Provider.set(IPacketBuilder, new PacketBuilder());
|
||||||
//var server = new Server(ServerMode.NORMAL_SOCKET);
|
var wserver = new Server();
|
||||||
//server.run("localhost", 5001);
|
|
||||||
var wserver = new Server(ServerMode.WEB_SOCKET);
|
|
||||||
wserver.run("localhost", 5001);
|
wserver.run("localhost", 5001);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
package ru.m.tankz.server.session;
|
package ru.m.tankz.server.session;
|
||||||
|
|
||||||
|
import ru.m.core.connect.neko.NekoWebConnection;
|
||||||
|
import haxe.io.Bytes;
|
||||||
import ru.m.tankz.proto.PersonSelectResponse;
|
import ru.m.tankz.proto.PersonSelectResponse;
|
||||||
import ru.m.tankz.proto.PersonSelectRequest;
|
import ru.m.tankz.proto.PersonSelectRequest;
|
||||||
import ru.m.tankz.proto.Account;
|
import ru.m.tankz.proto.Account;
|
||||||
@@ -15,14 +17,31 @@ import sys.net.Socket;
|
|||||||
class Session implements IConnectionHandler implements IPacketHandler {
|
class Session implements IConnectionHandler implements IPacketHandler {
|
||||||
|
|
||||||
public var account(default, null):Account;
|
public var account(default, null):Account;
|
||||||
public var connection(default, null):IConnection;
|
|
||||||
public var opened:Bool;
|
|
||||||
|
|
||||||
public function new(socket:Socket, connectionFactory:Class<IConnection>) {
|
private var connection(default, null):IConnection;
|
||||||
opened = false;
|
private var socket:Socket;
|
||||||
connection = Type.createInstance(connectionFactory, [socket, this, this]);
|
|
||||||
|
public function new(socket:Socket) {
|
||||||
|
this.socket = socket;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function pushData(bytes:Bytes):Void {
|
||||||
|
if (connection != null) {
|
||||||
|
connection.pushData(bytes);
|
||||||
|
} else {
|
||||||
|
var str:String = bytes.getString(0, bytes.length);
|
||||||
|
if (StringTools.startsWith(str, "GET")) {
|
||||||
|
connection = new NekoWebConnection(socket, this, this);
|
||||||
|
} else {
|
||||||
|
connection = new NekoConnection(socket, this, this);
|
||||||
|
}
|
||||||
|
connection.pushData(bytes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connection handlers
|
||||||
|
**/
|
||||||
public function onConnected():Void {
|
public function onConnected():Void {
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -39,6 +58,9 @@ class Session implements IConnectionHandler implements IPacketHandler {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Packets handlers
|
||||||
|
**/
|
||||||
public function onLoginRequest(packet:LoginRequest):Void {
|
public function onLoginRequest(packet:LoginRequest):Void {
|
||||||
var db = new Db();
|
var db = new Db();
|
||||||
account = db.getAccount(packet.login, packet.password);
|
account = db.getAccount(packet.login, packet.password);
|
||||||
|
|||||||
Reference in New Issue
Block a user