3using System.Collections;
5using System.Net.Sockets;
15 const ushort PORT_NO = 42069;
16 const string SERVER_IP =
"192.168.10.165";
17 private TcpClient client;
21 private Vector3[] RemotePos, RemoteRot;
24 private Vector3[] RemoteLocalPos, RemoteLocalRot;
34 s =
new Socket(AddressFamily.InterNetwork, SocketType.Stream,
38 s.Connect(SERVER_IP, PORT_NO);
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));
55 StartCoroutine(NetworkUpdate());
75 private IEnumerator NetworkUpdate()
79 while (!s.Connected) yield
return null;
81 yield
return new WaitForSeconds(0.1f);
84 for (
int i = 0; i <
MyPlayer.Length; 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}"));
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}"));
100 [NaughtyAttributes.Button]
103 for (
int i = 0; i < 25; i++)
111 if (!s.Connected)
return;
113 s.Send(Encoding.GetEncoding(
"iso-8859-1").GetBytes(
"{REQ}" + goname +
"{EOS}"));
117 private void ProcessResponse(
string command)
120 if (command.StartsWith(
"{POS}"))
122 var goName = command.Remove(0, 5).Split(
';');
123 var index = Convert.ToInt32(goName[0]);
126 if (command.StartsWith(
"{ROT}"))
128 var goName = command.Remove(0, 5).Split(
';');
129 var index = Convert.ToInt32(goName[0]);
132 if (command.StartsWith(
"{POSL}"))
134 var goName = command.Remove(0, 6).Split(
';');
135 var index = Convert.ToInt32(goName[0]);
138 if (command.StartsWith(
"{ROTL}"))
140 var goName = command.Remove(0, 6).Split(
';');
141 var index = Convert.ToInt32(goName[0]);
146 private void StartRecieve()
149 List<byte> receivedBytes =
new List<byte>();
154 byte[] receive =
new byte[1];
155 int ret = s.Receive(receive, 1, 0);
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")
164 var fullmsg = Encoding.GetEncoding(
"ISO-8859-1").GetString(receivedBytes.ToArray());
168 Dispatcher.InvokeAsync(() =>
170 ProcessResponse(fullmsg);
173 receivedBytes.Clear();
180 receivedBytes.AddRange(Encoding.GetEncoding(
"ISO-8859-1").GetBytes(end));
183 receivedBytes.Add(receive[0]);
196 private void OnDisable()
static string FtoS(float value, char separator='.')
static Vector3 LerpAngle(Vector3 from, Vector3 to, float t)
static float StoF(string value)
Transform[] RemoteLocalPlayer
Transform[] MyLocalPlayer
void RequestGO(string goname)