diff --git a/.gitignore b/.gitignore index 5d49aeb..b3db786 100755 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ target/ src-gen/ *.iml *.ipr -*.iws \ No newline at end of file +*.iws +*.ids \ No newline at end of file diff --git a/project.xml b/project.xml index c85c61d..6ed7070 100755 --- a/project.xml +++ b/project.xml @@ -1,8 +1,9 @@ - - + + + diff --git a/proto/base.proto b/proto/base.proto index 60bdacf..419c68a 100755 --- a/proto/base.proto +++ b/proto/base.proto @@ -1,3 +1,14 @@ -message Foo { - required string bar = 1; +message User { + required int32 id = 1; + required string login = 2; + required string nickname = 3; +} + +message LoginRequest { + required string login = 1; + required string password = 2; +} + +message LoginResponse { + required User user = 1; } \ No newline at end of file diff --git a/server.hxml b/server.hxml new file mode 100755 index 0000000..eb76653 --- /dev/null +++ b/server.hxml @@ -0,0 +1,6 @@ +-main ru.m.armageddon.server.Server +-lib protohx +-cp src/common/haxe +-cp src/server/haxe +-cp src-gen/haxe +-neko target/server.n \ No newline at end of file diff --git a/sql/init.sql b/sql/init.sql new file mode 100755 index 0000000..fea84d3 --- /dev/null +++ b/sql/init.sql @@ -0,0 +1,15 @@ +-- mysql -u shmyga -pxkbp8jh9z2 armageddon --protocol TCP +CREATE USER 'shmyga'@'localhost' IDENTIFIED BY 'xkbp8jh9z2'; + +DROP DATABASE IF EXISTS armageddon; +CREATE DATABASE IF NOT EXISTS armageddon; +GRANT ALL ON armageddon.* TO 'shmyga'@'localhost' IDENTIFIED BY 'xkbp8jh9z2'; + + +DROP TABLE IF EXISTS armageddon.users; +CREATE TABLE IF NOT EXISTS armageddon.users ( + id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT, + login VARCHAR(255) UNIQUE, + password VARCHAR(32) +); +INSERT INTO armageddon.users (login,password) VALUES('shmyga', 'd48cc4eb42c058869ae90daef9606e43'); \ No newline at end of file diff --git a/src/client/haxe/ru/m/armageddon/client/Client.hx b/src/client/haxe/ru/m/armageddon/client/Client.hx new file mode 100755 index 0000000..d95e6aa --- /dev/null +++ b/src/client/haxe/ru/m/armageddon/client/Client.hx @@ -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()); + } +} diff --git a/src/common/haxe/ru/m/armageddon/z.hx b/src/common/haxe/ru/m/armageddon/z.hx new file mode 100755 index 0000000..9dbb07b --- /dev/null +++ b/src/common/haxe/ru/m/armageddon/z.hx @@ -0,0 +1,3 @@ +package ru.m.armageddon; +interface z { +} diff --git a/src/haxe/ru/m/armageddon/Armageddon.hx b/src/haxe/ru/m/armageddon/Armageddon.hx deleted file mode 100755 index 5753dc6..0000000 --- a/src/haxe/ru/m/armageddon/Armageddon.hx +++ /dev/null @@ -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); - } -} \ No newline at end of file diff --git a/src/server/haxe/ru/m/armageddon/server/Server.hx b/src/server/haxe/ru/m/armageddon/server/Server.hx new file mode 100755 index 0000000..236ea7e --- /dev/null +++ b/src/server/haxe/ru/m/armageddon/server/Server.hx @@ -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 { +// 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(); + } +} + +