-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerControl.cs
161 lines (142 loc) · 5.2 KB
/
ServerControl.cs
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
using System.Collections;
using System.Collections.Generic;
using System;
using System.IO;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
public class ServerControl : MonoBehaviour
{
// control instances
public GameControlMultiplayer gameControlMultiplayer;
// stream of server connection
private NetworkStream serverStream;
// thread to run socket connection and spawn requests
private Thread tcpListenerThread;
private static int bufferSize = 1024;
private static Byte[] buffer = new Byte[bufferSize];
public static bool creatureReceiveSuccess;
// Use this for initialization
void Start()
{
serverStream = PublicLevel.GetServerStream();
// Start TcpServer background thread
tcpListenerThread = new Thread(new ThreadStart(ListenForIncommingRequests));
tcpListenerThread.IsBackground = true;
tcpListenerThread.Start();
}
public static IEnumerator OpenStream(float waitTime)
{
yield return new WaitForSeconds(waitTime);
TcpClient _socketConnection;
try
{
_socketConnection = new TcpClient(ServerControlForm.GetUrl(), ServerControlForm.GetPort());
PublicLevel.SetServerStream(_socketConnection.GetStream());
}
catch(SocketException socketException)
{
Debug.Log("SocketException " + socketException.ToString());
StageButtonMultiplayer.NetworkErrorPanelactive();
}
}
public static bool SendCreatureList()
{
buffer = new byte[bufferSize];
try
{
// create a socket for streaming data
string serverMessage="";
Vector2Int[] friendlyType = PublicLevel.GetFriendlyType();
for(int i = 0; i < PublicLevel.usingCreatureNum; ++i)
{
serverMessage = serverMessage + friendlyType[i].x.ToString() + ',' + friendlyType[i].y.ToString() + " ";
}
buffer = Encoding.ASCII.GetBytes(serverMessage);
PublicLevel.GetServerStream().Write(buffer, 0, buffer.Length);
ClearBuffer(buffer);
}
catch (SocketException socketException)
{
Debug.Log("SocketException " + socketException.ToString());
return false;
}
return true;
}
public static IEnumerator ListenForHostileCreatureList(float waitTime)
{
yield return new WaitForSeconds(waitTime);
buffer = new byte[bufferSize];
try
{
Vector2Int[] _hostileType = new Vector2Int[PublicLevel.usingCreatureNum];
PublicLevel.GetServerStream().BeginRead(buffer, 0, bufferSize, OnReceive, null);
creatureReceiveSuccess = true;
// load after creaturelist receive is complete
LoadingSceneManager.LoadScene("DefaultIngameCopy");
}
catch (Exception listSendException)
{
Debug.Log("ListSendException " + listSendException.ToString());
StageButtonMultiplayer.NetworkErrorPanelactive();
}
StageButtonMultiplayer.SetCreatureFlag(true);
StageButtonMultiplayer.NetworkWaitPanelInactive();
}
static void OnReceive(IAsyncResult result)
{
Debug.Log("hello1");
string[] serverMessage = Encoding.UTF8.GetString(buffer).Split(' ');
string[] intPair;
Debug.Log("hello2");
for (int i = 0; i < PublicLevel.usingCreatureNum; ++i)
{
intPair = serverMessage[i].Split(',');
PublicLevel.hostileCreatureList[i] = PublicLevel.hostilePrefab[int.Parse(intPair[0]), int.Parse(intPair[1])];
Debug.Log(intPair[0] + intPair[1]);
}
ClearBuffer(buffer);
return;
}
// Runs in background TcpServerThread; Handles incomming TcpClient requests
private void ListenForIncommingRequests()
{
try
{
Byte[] buffer = new Byte[bufferSize];
// create a socket for streaming data
string serverMessage;
ClearBuffer(buffer);
while (true)
{
if (serverStream.Read(buffer, 0, buffer.Length) != 0)
{
serverMessage = Encoding.UTF8.GetString(buffer);
Debug.Log("Server message is " + serverMessage);
// ReceiveSpawnRequest will call spawnControl.SummonCreature
gameControlMultiplayer.ReceiveSpawnRequest(serverMessage);
ClearBuffer(buffer);
}
}
}
catch (SocketException socketException)
{
Debug.Log("SocketException " + socketException.ToString());
}
}
// initialize given byte array to 0
public static void ClearBuffer(byte[] buffer)
{
for(int i=0; i < buffer.Length; ++i)
{
buffer[i] = 0;
}
}
public NetworkStream GetServerStream()
{
return serverStream;
}
}