-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkClient.cs
More file actions
230 lines (189 loc) · 4.25 KB
/
NetworkClient.cs
File metadata and controls
230 lines (189 loc) · 4.25 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
using UnityEngine;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System;
using System.Text;
using System.Collections.Generic;
public class NetworkClient : MonoBehaviour {
public delegate void RecieveMessageDelegate(byte[] msg);
public RecieveMessageDelegate RecieveMessage;
public bool isConnected = false;
string iport = "192.168.1.100:1000";
int port = 1000;
IPAddress ip = null;
Thread thdRec;
public Socket clientSocket;
bool doRec = true;
public static NetworkClient Instance;
//bool sentAtLeastOneMsg = false;
//public delegate void OnConnected;
void Awake(){
Instance = this;
}
void Start()
{
if (PlayerPrefs.HasKey("iport"))
{
iport = PlayerPrefs.GetString("iport");
Connect_Click();
}
}
// void FixedUpdate () {
// if (!isConnected || !sentAtLeastOneMsg) {
// SendString("ComeConnectMe!");
// sentAtLeastOneMsg = true;
// }
// }
string msg = "";
void OnGUI()
{
if (!isConnected)
{
GUI.skin.textField.fontSize = 30;
GUI.skin.button.fontSize = 30;
iport = GUI.TextField(new Rect(10, 10, 400, 70), iport);
if (GUI.Button(new Rect(10, 90, 400, 70), "Connect"))
{
Connect_Click();
}
if(msg != "") GUI.Box(new Rect(10, 170, 400, 50), msg);
}
}
public void Connect_Click(){
string[] splResult = iport.Split(':');
if (splResult.Length != 2)
{
msg = "Wrong IP/Port!";
return;
}
int tmpPort;
if (!int.TryParse(splResult[1], out tmpPort) || tmpPort < 0 || tmpPort > 65535)
{
msg = "Wrong IP/Port!";
return;
}
//Debug.Log(port);
port = tmpPort;
IPAddress tmpIp;
try
{
tmpIp = IPAddress.Parse(splResult[0]);
}
catch
{
tmpIp = null;
msg = "Wrong IP/Port!";
return;
}
ip = tmpIp;
msg = "Try connecting...";
PlayerPrefs.SetString("iport", iport);
PlayerPrefs.Save();
StartConnecting();
}
public void StartConnecting()
{
if (!isConnected)
{
try
{
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipEndPoint = new IPEndPoint(ip, port);
//Debug.Log(ip.ToString() + "|" + port.ToString());
clientSocket.Connect(ipEndPoint);
thdRec = new Thread(new ThreadStart(ClientReceiving));
thdRec.Start();
StartCoroutine(DetectConnection());
}
catch (Exception ex)
{
//Debug.Log(ex.Message);
msg = ex.Message;
}
}
}
IEnumerator DetectConnection()
{
float notConnectedCount = 0;
yield return new WaitForSeconds(0.5f);
while (!isConnected && ((notConnectedCount += Time.deltaTime) < 5.5f))
{
yield return 2;
}
if (isConnected)
{
SendString("!");
//Connect!
}
}
private void ClientReceiving()
{
try
{
//ShowMessage("Ready...");
while (doRec)
{
//Color: 27648 = 128 * 72 * 3 * 3 1166400 = 480 * 270 * 3 * 3 Depth:651264 = 512 * 424 * 3
// int rev = 0;
byte[] bytes = new byte[1024]; //27648 + 2 //82945 //1024
int rev = clientSocket.Receive(bytes, bytes.Length, 0);
if (rev <= 0)
{
//ShowMessage("Disconnected.");
doRec = false;
Close();
break;
}
//Debug.Log("Rev: " + rev.ToString());
if (!isConnected)
{
//ShowMessage("Connected!", MessageFadeOutType.FadeOutNow);
isConnected = true;
}
RecieveMessage(bytes);
//ProcessData(bytes);
}
}catch(ThreadAbortException ex){
Debug.Log(ex.Message);
}
}
// private void ProcessData(byte[] data)
// {
//
// //string strData = System.Text.Encoding.ASCII.GetString(data).Trim(new char[] { '\0' });
// }
public void SendString(string str)
{
if (null != clientSocket && clientSocket.Connected)
{
byte[] strByte = Encoding.ASCII.GetBytes(str);
//byte[] strMessage = new byte[strByte.Length + 1];
//strByte.CopyTo(strMessage, 0);
//strMessage[strByte.Length] = "\0";
clientSocket.Send(strByte);
}
else
{
//Debug.Log("Disconnected.");
//ShowMessage("Disconnected", MessageFadeOutType.FadeOutLater);
}
}
void OnApplicationQuit()
{
Close();
}
public void Close(){
doRec = false;
if (clientSocket != null)
{
clientSocket.Shutdown(SocketShutdown.Both);
clientSocket.Close();
}
if (thdRec != null) thdRec.Abort();
}
void OnDestroy(){
Instance = null;
}
}