-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathGazeFlowAPI.cs
More file actions
177 lines (123 loc) · 4.27 KB
/
GazeFlowAPI.cs
File metadata and controls
177 lines (123 loc) · 4.27 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text;
using System.Net.Sockets;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Threading;
namespace GazeFlowAPI//GazeScreamClient
{
[Serializable()]
public class CGazeData
{
public float GazeX;
public float GazeY;
public float HeadX;
public float HeadY;
public float HeadZ;
public float HeadYaw;
public float HeadPitch;
public float HeadRoll;
}
class CGazeFlowAPI
{
NetworkStream stream;
TcpClient client;
// Byte[] dataBufor;
BinaryReader reader;
BinaryWriter writer;
public CGazeData ReciveGazeDataSyn()
{
CGazeData GazeData;
if (!client.Connected)
return null;
String responseData;
try
{
responseData = reader.ReadString();
}
catch
{
Disconect(); // connection lost
return null;
}
try
{
XmlSerializer serializer = new XmlSerializer(typeof(CGazeData));
using (TextReader readerxml = new StringReader(responseData))
{
GazeData = (CGazeData)serializer.Deserialize(readerxml);
}
}
catch
{
Console.WriteLine( responseData); // server close connetion
Disconect();
return null;
}
return GazeData;
}
//To get your AppKey register at http://gazeflow.epizy.com/GazeFlowAPI/register/
public bool Connect( string adress= "127.0.0.1", Int32 port = 43333,string AppKey = "AppKeyDemo")
{
string ResultFormat = "xml";
try
{
// Create a TcpClient.
// Note, for this client to work you need to have a TcpServer
// connected to the same address as specified by the server, port
// combination.
client = new TcpClient(adress, port);
if (!client.Connected)
return false;
stream = client.GetStream();
reader = new BinaryReader(stream);
writer = new BinaryWriter(stream);
if (true) // version type
{
// Translate the ResultFormat into UTF8 and store it as a Byte array.
Byte[] data = System.Text.Encoding.UTF8.GetBytes(ResultFormat);
// Send ResultFormat to the connected TcpServer.
stream.Write(data, 0, data.Length);
}
// Send appkey string to the connected TcpServer.
writer.Write(AppKey);
// Receive the TcpServer.response.
string connectionInfo = reader.ReadString();
Console.WriteLine("connectionStatus: " + connectionInfo);
string connectionStatus = connectionInfo.Substring(0, 2);
bool AutoryzationOk = true;
if (connectionStatus != "ok")
AutoryzationOk = false;
if (!client.Connected || !AutoryzationOk)
{
// invalid appKey
Disconect();
return false;
}
return true;
}
catch
{
Disconect();
return false;
}
}
void Disconect()
{
try
{
reader.Close();
writer.Close();
stream.Close();
client.Close();
}
catch
{
}
}
}
}