Tanoda
TanodaClient.cs
Go to the documentation of this file.
1using B83.MeshTools;
2using System;
3using System.Collections;
4using System.Collections.Generic;
5using System.Net.Sockets;
6using System.Text;
7using System.Threading;
8#if DANA
9using TriLib;
10#endif
11using UnityEngine;
12
13public class TanodaClient : MonoBehaviour
14{
15 const ushort PORT_NO = 42069;
16 const string SERVER_IP = "192.168.10.165";
17 private TcpClient client;
18 private Thread t;
19 private Socket s;
20 public Transform[] MyPlayer, RemotePlayer;
21 private Vector3[] RemotePos, RemoteRot;
22
23 public Transform[] MyLocalPlayer, RemoteLocalPlayer;
24 private Vector3[] RemoteLocalPos, RemoteLocalRot;
25
26 IEnumerator Start()
27 {
28 RemotePos = new Vector3[RemotePlayer.Length];
29 RemoteRot = new Vector3[RemotePlayer.Length];
30 RemoteLocalPos = new Vector3[RemoteLocalPlayer.Length];
31 RemoteLocalRot = new Vector3[RemoteLocalPlayer.Length];
32
33 yield return null;
34 s = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
35 ProtocolType.Tcp);
36 try
37 {
38 s.Connect(SERVER_IP, PORT_NO);
39
40 if (s.Connected)
41 {
42 byte[] bBuf;
43 string buf = "helószija!{EOS}";
44 bBuf = Encoding.GetEncoding("iso-8859-1").GetBytes(buf);
45 s.Send(bBuf, 0, bBuf.Length, 0);
46 t = new Thread(new ThreadStart(StartRecieve));
47 t.Start();
48 }
49 }
50 catch (Exception e)
51 {
52 Debug.Log(e.Message);
53 }
54
55 StartCoroutine(NetworkUpdate());
56 }
57
58
59 private void Update()
60 {
61 for (int i = 0; i < RemotePlayer.Length; i++)
62 {
63 RemotePlayer[i].position = Vector3.Lerp(RemotePlayer[i].position, RemotePos[i], 0.1f);
64 RemotePlayer[i].eulerAngles = Macro.LerpAngle(RemotePlayer[i].eulerAngles, RemoteRot[i], 0.1f);
65 }
66 for (int i = 0; i < RemoteLocalPlayer.Length; i++)
67 {
68 //RemoteLocalPlayer[i].localPosition = Vector3.Lerp(RemoteLocalPlayer[i].localPosition, RemoteLocalPos[i], 0.1f);
69 RemoteLocalPlayer[i].localEulerAngles = Macro.LerpAngle(RemoteLocalPlayer[i].localEulerAngles, RemoteLocalRot[i], 0.1f);
70 }
71 }
72
73
74
75 private IEnumerator NetworkUpdate()
76 {
77 while (true)
78 {
79 while (!s.Connected) yield return null;
80
81 yield return new WaitForSeconds(0.1f);
82
83
84 for (int i = 0; i < MyPlayer.Length; i++)
85 {
86 Transform t = MyPlayer[i];
87 s.Send(Encoding.GetEncoding("iso-8859-1").GetBytes("{POS}" + i + ";" + Macro.FtoS(t.position.x) + ";" + Macro.FtoS(t.position.y) + ";" + Macro.FtoS(t.position.z) + ";{EOS}"));
88 s.Send(Encoding.GetEncoding("iso-8859-1").GetBytes("{ROT}" + i + ";" + Macro.FtoS(t.eulerAngles.x) + ";" + Macro.FtoS(t.eulerAngles.y) + ";" + Macro.FtoS(t.eulerAngles.z) + ";{EOS}"));
89 }
90 for (int i = 0; i < MyLocalPlayer.Length; i++)
91 {
92 Transform t = MyLocalPlayer[i];
93 //s.Send(Encoding.GetEncoding("iso-8859-1").GetBytes("{POSL}" + i + ";" + Macro.FtoS(t.localPosition.x) + ";" + Macro.FtoS(t.localPosition.y) + ";" + Macro.FtoS(t.localPosition.z) + ";{EOS}"));
94 s.Send(Encoding.GetEncoding("iso-8859-1").GetBytes("{ROTL}" + i + ";" + Macro.FtoS(t.localEulerAngles.x) + ";" + Macro.FtoS(t.localEulerAngles.y) + ";" + Macro.FtoS(t.localEulerAngles.z) + ";{EOS}"));
95 }
96
97 }
98 }
99
100 [NaughtyAttributes.Button]
101 void spamRequest()
102 {
103 for (int i = 0; i < 25; i++)
104 {
105 RequestGO("request" + i);
106 }
107 }
108
109 public void RequestGO(string goname)
110 {
111 if (!s.Connected) return;
112
113 s.Send(Encoding.GetEncoding("iso-8859-1").GetBytes("{REQ}" + goname + "{EOS}"));
114 }
115
116
117 private void ProcessResponse(string command)
118 {
119
120 if (command.StartsWith("{POS}"))
121 {
122 var goName = command.Remove(0, 5).Split(';');
123 var index = Convert.ToInt32(goName[0]);
124 RemotePos[index] = new Vector3(Macro.StoF(goName[1]), Macro.StoF(goName[2]), Macro.StoF(goName[3]));
125 }
126 if (command.StartsWith("{ROT}"))
127 {
128 var goName = command.Remove(0, 5).Split(';');
129 var index = Convert.ToInt32(goName[0]);
130 RemoteRot[index] = new Vector3(Macro.StoF(goName[1]), Macro.StoF(goName[2]), Macro.StoF(goName[3]));
131 }
132 if (command.StartsWith("{POSL}"))
133 {
134 var goName = command.Remove(0, 6).Split(';');
135 var index = Convert.ToInt32(goName[0]);
136 RemoteLocalPos[index] = new Vector3(Macro.StoF(goName[1]), Macro.StoF(goName[2]), Macro.StoF(goName[3]));
137 }
138 if (command.StartsWith("{ROTL}"))
139 {
140 var goName = command.Remove(0, 6).Split(';');
141 var index = Convert.ToInt32(goName[0]);
142 RemoteLocalRot[index] = new Vector3(Macro.StoF(goName[1]), Macro.StoF(goName[2]), Macro.StoF(goName[3]));
143 }
144 }
145
146 private void StartRecieve()
147 {
148 string end = "";
149 List<byte> receivedBytes = new List<byte>();
150 while (true)
151 {
152 try
153 {
154 byte[] receive = new byte[1];
155 int ret = s.Receive(receive, 1, 0);
156 if (ret > 0)
157 {
158 var recieved = Encoding.GetEncoding("iso-8859-1").GetString(receive);
159 if (recieved == "{" && end == "" || recieved == "E" && end == "{" || recieved == "O" && end == "{E" || recieved == "S" && end == "{EO" || recieved == "}" && end == "{EOS")
160 {
161 end += recieved;
162 if (end == "{EOS}")
163 {
164 var fullmsg = Encoding.GetEncoding("ISO-8859-1").GetString(receivedBytes.ToArray());
165 //System.IO.File.AppendAllText("pastcommands.txt", fullmsg + "\r\n");
166 //Console.WriteLine(fullmsg);
167#if DANA
168 Dispatcher.InvokeAsync(() =>
169 {
170 ProcessResponse(fullmsg);
171 });
172#endif
173 receivedBytes.Clear();
174 end = "";
175 }
176 continue;
177 }
178 if (end != "")
179 {
180 receivedBytes.AddRange(Encoding.GetEncoding("ISO-8859-1").GetBytes(end));
181 }
182 end = "";
183 receivedBytes.Add(receive[0]);
184 }
185 }
186 catch (Exception e)
187 {
188 if (!s.Connected)
189 {
190 break;
191 }
192 }
193 }
194 }
195
196 private void OnDisable()
197 {
198 client?.Close();
199 }
200}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
Definition: Macro.cs:12
static string FtoS(float value, char separator='.')
Definition: Macro.cs:37
static Vector3 LerpAngle(Vector3 from, Vector3 to, float t)
Definition: Macro.cs:298
static float StoF(string value)
Definition: Macro.cs:24
Transform[] RemotePlayer
Definition: TanodaClient.cs:20
Transform[] RemoteLocalPlayer
Definition: TanodaClient.cs:23
Transform[] MyLocalPlayer
Definition: TanodaClient.cs:23
void RequestGO(string goname)
Transform[] MyPlayer
Definition: TanodaClient.cs:20
Definition: B83.Win32.cs:38