-
Notifications
You must be signed in to change notification settings - Fork 347
/
Copy pathNetwork.hx
156 lines (128 loc) · 3.31 KB
/
Network.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
class Cursor implements hxbit.NetworkSerializable {
@:s var color : Int;
@:s public var uid : Int;
@:s public var x(default, set) : Float;
@:s public var y(default, set) : Float;
var net : Network;
var bmp : h2d.Graphics;
public function new( color, uid=0 ) {
this.color = color;
this.uid = uid;
init();
x = 0;
y = 0;
}
public function networkAllow( op : hxbit.NetworkSerializable.Operation, propId : Int, client : hxbit.NetworkSerializable ) : Bool {
return client == this;
}
function set_x( v : Float ) {
if( v == x ) return v;
if( bmp != null ) bmp.x = v;
return this.x = v;
}
function set_y( v : Float ) {
if( bmp != null ) bmp.y = v;
return this.y = v;
}
public function toString() {
return "Cursor " + StringTools.hex(color, 6)+(enableReplication?":ALIVE":"");
}
function init() {
net = Network.inst;
net.log("Init "+this);
bmp = new h2d.Graphics(net.s2d);
bmp.beginFill(color, 0.5);
bmp.drawCircle(0, 0, 10);
bmp.beginFill(color);
bmp.drawCircle(0, 0, 5);
enableReplication = true;
var i = new h2d.Interactive(10, 10, bmp);
i.x = i.y = -5;
i.isEllipse = true;
i.onClick = function(_) blink( 2 + Math.random() * 2 );
}
@:rpc function blink( s : Float ) {
bmp.scale(s);
net.event.waitUntil(function(dt) {
bmp.scaleX *= Math.pow(0.9, dt * 60);
bmp.scaleY *= Math.pow(0.9, dt * 60);
if( bmp.scaleX < 1 ) {
bmp.scaleX = bmp.scaleY = 1;
return true;
}
return false;
});
}
public function alive() {
init();
// refresh bmp
this.x = x;
this.y = y;
if( uid == net.uid ) {
net.cursor = this;
net.host.self.ownerObject = this;
}
}
}
//PARAM=-lib hxbit
class Network extends hxd.App {
static var HOST = "127.0.0.1";
static var PORT = 6676;
public var host : hxd.net.SocketHost;
public var event : hxd.WaitEvent;
public var uid : Int;
public var cursor : Cursor;
override function init() {
event = new hxd.WaitEvent();
host = new hxd.net.SocketHost();
host.setLogger(function(msg) log(msg));
if( !hxd.net.Socket.ALLOW_BIND )
log("Server not allowed on this platform");
try {
host.wait(HOST, PORT, function(c) {
log("Client Connected");
});
host.onMessage = function(c,uid:Int) {
log("Client identified ("+uid+")");
var cursorClient = new Cursor(0x0000FF, uid);
c.ownerObject = cursorClient;
c.sync();
};
log("Server Started");
start();
} catch( e : Dynamic ) {
// we could not start the server
log("Connecting");
uid = 1 + Std.random(1000);
host.connect(HOST, PORT, function(b) {
if( !b ) {
log("Failed to connect to server");
return;
}
log("Connected to server");
host.sendMessage(uid);
});
}
}
public function log( s : String, ?pos : haxe.PosInfos ) {
pos.fileName = (host.isAuth ? "[S]" : "[C]") + " " + pos.fileName;
haxe.Log.trace(s, pos);
}
function start() {
cursor = new Cursor(0xFF0000);
log("Live");
host.makeAlive();
}
override function update(dt:Float) {
event.update(dt);
if( cursor != null ) {
cursor.x = s2d.mouseX;
cursor.y = s2d.mouseY;
}
host.flush();
}
public static var inst : Network;
static function main() {
inst = new Network();
}
}