-
Notifications
You must be signed in to change notification settings - Fork 14
/
NetworkServer.cs
132 lines (124 loc) · 3.27 KB
/
NetworkServer.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
// NetworkServer.cs - generates red texture and send to NetworkClient.cs
// NetworkClient.cs - generates blue texture and send to NetworkServer.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using UnityEngine;
public class NetworkServer : MonoBehaviour
{
TcpClient _Client = null;
TcpListener _Listener = null;
Thread _Thread = null;
string _IPAddress = "127.0.0.1";
string _Port = "8052";
string _Message = "";
Texture2D _Texture = null;
byte[] _Bytes = new byte[1024 * 1024 * 4];
bool _Reload = false;
void LaunchServer()
{
if (_Thread != null) return;
_Thread = new Thread (new ThreadStart(NetworkReader));
_Thread.IsBackground = true;
_Thread.Start();
}
void NetworkReader()
{
try
{
_Listener = new TcpListener(IPAddress.Parse(_IPAddress), Int32.Parse(_Port));
_Listener.Start();
_Message = "Server is listening...";
byte[] bytes = new byte[_Bytes.Length];
while (true)
{
using (_Client = _Listener.AcceptTcpClient())
{
NetworkStream networkStream = _Client.GetStream();
if (networkStream.CanRead)
{
int length;
while ((length = networkStream.Read(bytes, 0, bytes.Length)) != 0)
{
_Bytes = new byte[length];
Array.Copy(bytes, 0, _Bytes, 0, length);
_Reload = true;
}
}
networkStream.Close();
}
}
}
catch (SocketException socketException)
{
_Message = "SocketException " + socketException.ToString();
}
}
void NetworkWriter()
{
if (_Client == null) return;
try
{
NetworkStream networkStream = _Client.GetStream();
if (networkStream.CanWrite)
{
byte[] bytes = GenerateTexture();
networkStream.Write(bytes, 0, bytes.Length);
}
}
catch (SocketException socketException)
{
_Message = "Socket exception: " + socketException;
}
}
void StopServer()
{
if (_Client != null)
{
_Client.Close();
_Message = "Server is stopped";
}
}
void LoadTexture(byte[] bytes)
{
if (_Texture != null) Destroy(_Texture);
_Texture = new Texture2D(1024, 1024, TextureFormat.RGBA32, false);
_Texture.LoadImage(bytes);
}
byte[] GenerateTexture()
{
if (_Texture != null) Destroy(_Texture);
_Texture = new Texture2D(1024, 1024, TextureFormat.RGBA32, false);
for (int y = 0; y < _Texture.height; y++)
{
for (int x = 0; x < _Texture.width; x++)
{
_Texture.SetPixel(x, y, Color.red);
}
}
_Texture.Apply();
return _Texture.EncodeToPNG();
}
void OnGUI()
{
_IPAddress = GUI.TextField(new Rect(10, 10, 100, 20), _IPAddress, 25);
_Port = GUI.TextField(new Rect(150, 10, 100, 20), _Port, 25);
if (GUI.Button(new Rect(300, 10, 100, 30), "Launch Server")) LaunchServer();
if (GUI.Button(new Rect(450, 10, 100, 30), "Send Data")) NetworkWriter();
if (GUI.Button(new Rect(600, 10, 100, 30), "Stop Server")) StopServer();
GUI.Label(new Rect(10, 50, 700, 20), _Message);
if (_Texture != null)
{
GUI.DrawTexture(new Rect(256, 256, 256, 256), _Texture, ScaleMode.ScaleToFit, true, 1.0F);
}
if (_Reload)
{
LoadTexture(_Bytes);
_Reload = false;
}
}
}