1 #include "Turtle.h"
2 
3 #define LLOG(x)     // LLOG(x)
4 #define LDUMP(x)    // RDUMP(x)
5 #define LTIMING(x)
6 
7 namespace Upp {
8 
9 static TurtleServer::Stream sTurtleStream;
10 
Stream()11 TurtleServer::Stream::Stream()
12 {
13 	Reset();
14 }
15 
Out(const void * data,dword size)16 void TurtleServer::Stream::Out(const void *data, dword size)
17 {
18 	zlib.Put(data, (int) size);
19 }
20 
FlushStream()21 String TurtleServer::Stream::FlushStream()
22 {
23 	Flush();
24 	zlib.End();
25 	String s = zlib.Get();
26 	Reset();
27 	return s;
28 }
29 
Reset()30 void TurtleServer::Stream::Reset()
31 {
32 	zlib.Clear();
33 	zlib.Compress();
34 	hasdata = false;
35 }
36 
Put8(int x)37 void TurtleServer::Put8(int x)
38 {
39 	sTurtleStream.hasdata = true;
40 	sTurtleStream.Put(x);
41 }
42 
Put16(int x)43 void TurtleServer::Put16(int x)
44 {
45 	Put8(LOBYTE(x));
46 	Put8(HIBYTE(x));
47 }
48 
Put32(int x)49 void TurtleServer::Put32(int x)
50 {
51 	Put16(LOWORD(x));
52 	Put16(HIWORD(x));
53 }
54 
Put(Point p)55 void TurtleServer::Put(Point p)
56 {
57 	// TODO: Clamp?
58 	Put16(p.x);
59 	Put16(p.y);
60 }
61 
Put(Size sz)62 void TurtleServer::Put(Size sz)
63 {
64 	Put((Point)sz);
65 }
66 
Put(const Rect & r)67 void TurtleServer::Put(const Rect& r)
68 {
69 	Put(r.TopLeft());
70 	Put(r.GetSize());
71 }
72 
Put(const String & s)73 void TurtleServer::Put(const String& s)
74 {
75 	Put32(s.GetLength());
76 	sTurtleStream.hasdata = true;
77 	sTurtleStream.Put(s);
78 }
79 
80 //void Turtle_PutLink(const String& link) // FIXME
81 //{
82 //	TurtleServer::Put8(OPENLINK);
83 //	TurtleServer::Put(link);
84 //}
85 
Flush()86 void TurtleServer::Flush()
87 {
88 	if(!sTurtleStream.hasdata || websocket.IsClosed())
89 		return;
90 
91 	websocket.SendBinary(ZCompress(String(DISABLESENDING, 1))); // Do not send events until data transfered and processed
92 	int64 x = ++update_serial;
93 	if(IsNull(serial_time0)) {
94 		serial_time0 = msecs();
95 		serial_0 = update_serial;
96 	}
97 	Put8(UPDATESERIAL);
98 	Put32(LODWORD(x));
99 	Put32(HIDWORD(x));
100 	String s = sTurtleStream.FlushStream();
101 	stat_data_send += s.GetCount();
102 	LLOG("Sending " << s.GetLength());
103 	websocket.SendBinary(s);
104 }
105 }
106