This commit is contained in:
2014-06-22 23:07:00 +04:00
parent 8f689d4be9
commit 1bf3e4cc80
9 changed files with 175 additions and 30 deletions

View File

@@ -0,0 +1,53 @@
package ru.m.armageddon.client;
import haxe.io.Bytes;
import flash.events.Event;
import flash.events.SecurityErrorEvent;
import flash.events.IOErrorEvent;
import haxe.io.BytesOutput;
import haxework.log.TraceLogger;
import flash.net.Socket;
class Client {
private static inline var TAG = "Armageddon";
public static function main() {
L.push(new TraceLogger());
L.d(TAG, Meta.getVersion());
new Client();
}
private var socket:Socket;
public function new() {
socket = new Socket();
socket.addEventListener(IOErrorEvent.IO_ERROR, function(error):Void {
L.e("SocketLogger", "", error);
});
socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, function(error):Void {
L.e("SocketLogger", "", error);
});
//socket.addEventListener(Event.CLOSE, closeHandler);
socket.addEventListener(Event.CONNECT, onConnect);
//socket.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
//socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
//socket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
socket.connect("localhost", 5000);
}
private function onConnect(_) {
var request = new LoginRequest();
request.login = "shmyga";
request.password = "xkbp8jh9z2";
var out = new BytesOutput();
request.writeTo(out);
var bytes = out.getBytes();
socket.writeShort(bytes.length);
socket.writeBytes(cast bytes.getData());
}
}

View File

@@ -0,0 +1,3 @@
package ru.m.armageddon;
interface z {
}

View File

@@ -1,25 +0,0 @@
package ru.m.armageddon;
import haxe.io.BytesOutput;
import haxework.log.TraceLogger;
class Armageddon {
private static inline var TAG = "Armageddon";
public static function main() {
L.push(new TraceLogger());
L.d(TAG, Meta.getVersion());
var foo = new Foo();
foo.bar = "test";
var out = new BytesOutput();
foo.writeTo(out);
var foo2 = new Foo();
foo2.mergeFrom(out.getBytes());
L.d(TAG, foo2.bar);
}
}

View File

@@ -0,0 +1,80 @@
package ru.m.armageddon.server;
import sys.db.Mysql;
import neko.Lib;
import sys.net.Socket;
import neko.net.ThreadServer;
import haxe.io.Bytes;
typedef Client = {
var id:Int;
}
typedef Message = {
var str:String;
}
class Server extends ThreadServer<Client, Message> {
// create a Client
override function clientConnected(s:Socket):Client {
var num = Std.random(100);
Lib.println("client " + num + " is " + s.peer());
return { id: num };
}
override function clientDisconnected(c:Client) {
Lib.println("client " + Std.string(c.id) + " disconnected");
}
override function readClientMessage(c:Client, buf:Bytes, pos:Int, len:Int) {
Lib.println("client " + Std.string(c.id) + " message");
var request = new LoginRequest();
request.mergeFrom(buf);
Lib.println(request.login);
Lib.println(request.password);
// find out if there's a full message, and if so, how long it is.
var complete = false;
var cpos = pos;
while (cpos < (pos + len) && !complete) {
//check for a period/full stop (i.e.: "." ) to signify a complete message
complete = (buf.get(cpos) == 46);
cpos++;
}
// no full message
if (!complete) return null;
// got a full message, return it
var msg:String = buf.readString(pos, cpos - pos);
return {msg: {str: msg}, bytes: cpos - pos};
}
override function clientMessage(c:Client, msg:Message) {
Lib.println(c.id + " sent: " + msg.str);
}
public static function main() {
var server = new Server();
server.run("localhost", 5000);
}
}
class Db {
public function new() {
var cnx = Mysql.connect({
host : "localhost",
port : 3306,
user : "shmyga",
pass : "xkbp8jh9z2",
socket : null,
database : "armageddon"
});
cnx.close();
}
}