Server.cs
2.77 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
/**********************
********LAVID**********
*******VLibras*********
*------------------------------------------------------------------------
*Description:
*Server gets pts from Core (client) by TCP connection
*and runs the animations until a final tag is found.
*------------------------------------------------------------------------
*Author: Claudiomar Araujo # claudiomar.araujo@lavid.ufpb.br
*------------------------------------------------------------------------
***********************/
using UnityEngine;
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
public class Server {
public readonly string ADDRESS = "0.0.0.0";
public readonly Int32 PORT = 5555;
private TcpClient client;
private NetworkStream stream;
private TcpListener server;
private bool isReady = false;
private PlayerManager manager;
public Server(PlayerManager manager)
{
this.manager = manager;
try {
// Starts listening for incoming connection requests
server = new TcpListener(IPAddress.Parse(ADDRESS), PORT);
server.Start();
// Accepts a pending connection request
Debug.Log("S(): Waiting client");
client = server.AcceptTcpClient();
stream = client.GetStream();
}
catch (Exception e)
{
Debug.Log(e);
CloseConnections();
manager.FinalizationLocker.Release();
throw e;
}
}
public bool IsNotReady {
get { return !isReady; }
}
/**
* Starts receiving of glosa and time from server.
* Stops when receive "FINALIZE".
*/
public void StartCommunication()
{
Debug.Log("S.SC()");
try
{
Byte[] bytes = new Byte[1024];
int size;
while ((size = stream.Read(bytes, 0, bytes.Length)) != 0)
{
String data = System.Text.UTF8Encoding.UTF8.GetString(bytes, 0, size);
// Notify the core that the message was received
Byte[] sendToCore = System.Text.UTF8Encoding.UTF8.GetBytes("OK\0");
stream.Write(sendToCore, 0, sendToCore.Length);
Message message = new Message(data);
Debug.Log("S.SC(): Received: " + message.Text);
if (message.Text.Equals("FINALIZE"))
{
isReady = true;
break;
}
else manager.enqueueMessage(message);
}
Debug.Log("S.SC(): END");
}
catch (Exception e)
{
Debug.Log(e);
if (!this.isReady)
{
CloseConnections();
manager.FinalizationLocker.Release();
}
throw e;
}
}
public void SendFinalizeToCore()
{
Byte[] sendToCore = System.Text.UTF8Encoding.UTF8.GetBytes("FINALIZE\0");
stream.Write(sendToCore, 0, sendToCore.Length);
CloseConnections();
}
private void CloseConnections()
{
try {
client.Close();
stream.Close();
server.Stop();
}
catch (SocketException e) {
Debug.Log(e);
CloseConnections();
}
catch (IOException e) {
Debug.Log(e);
CloseConnections();
}
}
}