TCP for world transfer

wichtige daten bei der UDP übertragung gehen verloren.
This commit is contained in:
brausjonas
2022-05-13 02:48:53 +02:00
parent afedf345f8
commit 8b2ac28a16
11 changed files with 102 additions and 76 deletions
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>
Binary file not shown.

Before

Width:  |  Height:  |  Size: 78 KiB

After

Width:  |  Height:  |  Size: 50 KiB

@@ -50,7 +50,7 @@ TextureImporter:
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spriteGenerateFallbackPhysicsShape: 1
alphaUsage: 1
alphaIsTransparency: 0
alphaIsTransparency: 1
spriteTessellationDetail: -1
textureType: 0
textureShape: 1
+24 -8
View File
@@ -1,5 +1,6 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
@@ -27,8 +28,7 @@ public class Client
client = new UdpClient();
client.DontFragment = false;
IPAddress address;
if (IPAddress.TryParse(hostname, out address))
if (IPAddress.TryParse(hostname, out IPAddress address))
{
address = IPAddress.Parse(hostname);
client.Connect(address, port);
@@ -50,7 +50,7 @@ public class Client
}
Send(sendList.ToArray());
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0);
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 8051);
byte[] data = client.Receive(ref endPoint);
if (data[0] == 2)
{
@@ -64,10 +64,23 @@ public class Client
byte xChunk = 0;
byte zChunk = 0;
TcpClient tcpClient = new TcpClient();
if (address != null)
{
address = IPAddress.Parse(hostname);
tcpClient.Connect(address, port);
}
else
{
tcpClient.Connect(hostname, port);
}
StreamReader reader = new StreamReader(tcpClient.GetStream());
while (true)
{
endPoint = new IPEndPoint(IPAddress.Any, 0);
data = client.Receive(ref endPoint);
data = Encoding.ASCII.GetBytes(reader.ReadLine());
if (data[0] == 1)
{
@@ -102,23 +115,26 @@ public class Client
zChunk = 0;
}
}
Send(new byte[]{5});
}
reader.Close();
tcpClient.Close();
world.CreateChunks();
//world transfer finished
//start listening
receiveThread = new Thread(Receive);
receiveThread.Start();
Send(new byte[]{6});
}
private void Receive()
{
while(true)
{
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0);
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 8051);
byte[] data = client.Receive(ref endPoint);
if (data[0] == 3)
@@ -42,8 +42,8 @@ PlayerSettings:
m_SplashScreenLogos: []
m_VirtualRealitySplashScreen: {fileID: 0}
m_HolographicTrackingLossScreen: {fileID: 0}
defaultScreenWidth: 1024
defaultScreenHeight: 768
defaultScreenWidth: 512
defaultScreenHeight: 384
defaultScreenWidthWeb: 960
defaultScreenHeightWeb: 600
m_StereoRenderingPath: 0
@@ -88,7 +88,7 @@ PlayerSettings:
bakeCollisionMeshes: 0
forceSingleInstance: 0
useFlipModelSwapchain: 1
resizableWindow: 0
resizableWindow: 1
useMacAppStoreValidation: 0
macAppStoreCategory: public.app-category.games
gpuSkinning: 1
@@ -99,7 +99,7 @@ PlayerSettings:
xboxEnableFitness: 0
visibleInBackground: 1
allowFullscreenSwitch: 1
fullscreenMode: 1
fullscreenMode: 3
xboxSpeechDB: 0
xboxEnableHeadOrientation: 0
xboxEnableGuest: 0
@@ -134,7 +134,7 @@ PlayerSettings:
16:10: 1
16:9: 1
Others: 1
bundleVersion: 0.1
bundleVersion: 0.2
preloadedAssets: []
metroInputSource: 0
wsaTransparentSwapchain: 0
Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.2 KiB

After

Width:  |  Height:  |  Size: 17 KiB

@@ -50,7 +50,7 @@ TextureImporter:
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spriteGenerateFallbackPhysicsShape: 1
alphaUsage: 1
alphaIsTransparency: 0
alphaIsTransparency: 1
spriteTessellationDetail: -1
textureType: 0
textureShape: 1
+40 -38
View File
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
@@ -17,9 +18,7 @@ public class Server
private int transferDelay;
private List<IPEndPoint> players = new List<IPEndPoint>();
private List<string> playerNames = new List<string>();
private List<bool> nextTransfer = new List<bool>();
private Thread receiveThread;
private List<Thread> transferThreads = new List<Thread>();
public Server(World world, int transferDelay)
{
@@ -31,9 +30,9 @@ public class Server
client.DontFragment = false;
//start listening
receiveThread = new Thread(Receive); //info:
receiveThread = new Thread(Receive);
receiveThread.Start();
//0: init message
Console.WriteLine("server running on port 8051" + "\n");
}
//info:
@@ -42,22 +41,18 @@ public class Server
//[0] = 2: world size
//[0] = 3: edit block
//[0] = 4: disconnect player
//[0] = 5: next transfer
//[0] = 5:
//[0] = 6: player ready
//[0] = 7:
private void Receive()
{
try
{
while (true)
{
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0);
byte[] data = client.Receive(ref endPoint);
if (data[0] == 5)
try
{
int id = GetPlayerID(endPoint);
nextTransfer[id] = true;
}
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 8051);
byte[] data = client.Receive(ref endPoint);
if (data[0] == 4)
{
@@ -78,17 +73,15 @@ public class Server
if (tempEndPoint != null)
{
players.Remove(tempEndPoint);
Console.WriteLine(playerNames[i] + " left the game");
Console.WriteLine(playerNames[i] + " left the game" + "\n");
playerNames.RemoveAt(i);
nextTransfer.RemoveAt(i);
}
}
}
if (data[0] == 0)
{
Thread t = new Thread(TransferWorld);
transferThreads.Add(t);
t.Start(endPoint);
}
@@ -106,8 +99,6 @@ public class Server
//add player
players.Add(endPoint);
playerNames.Add(name);
nextTransfer.Add(false);
Console.WriteLine(name + " joined the game");
}
if (data[0] == 3)
@@ -127,6 +118,13 @@ public class Server
}, e);
}
}
if (data[0] == 6)
{
int id = GetPlayerID(endPoint);
Console.WriteLine(playerNames[id] + " joined" + "\n");
}
}
catch (Exception e)
@@ -134,6 +132,7 @@ public class Server
}
}
}
private int GetPlayerID(IPEndPoint endPoint)
{
@@ -152,17 +151,25 @@ public class Server
}
private void TransferWorld(object o)
{
try
{
IPEndPoint endPoint = (IPEndPoint)o;
int id = GetPlayerID(endPoint);
Console.WriteLine("begin world transfer for " + playerNames[id]);
TcpListener listener = new TcpListener(IPAddress.Any, 8051);
listener.Start();
TcpClient tcpClient = listener.AcceptTcpClient();
listener.Stop();
// Console.WriteLine("tcp connected for " + playerNames[id] + "\n");
StreamWriter writer = new StreamWriter(tcpClient.GetStream());
writer.AutoFlush = true;
Console.WriteLine("begin world transfer for " + playerNames[id] + "\n");
for (int xChunk = 0; xChunk < world.worldSize; xChunk++)
{
for (int zChunk = 0; zChunk < world.worldSize; zChunk++)
{
nextTransfer[id] = false;
byte[] send = new byte[Data.chunkWidth * Data.chunkWidth * Data.chunkHeight + 2];
send[0] = 1;
send[1] = 0;
@@ -179,20 +186,21 @@ public class Server
}
}
}
Send(send, endPoint);
writer.WriteLine(Encoding.ASCII.GetString(send));
}
}
while (!nextTransfer[id])
writer.WriteLine(Encoding.ASCII.GetString(new byte[] { 1, 1 }));
Console.WriteLine("world transfer finished for " + playerNames[id] + "\n");
writer.Close();
tcpClient.Close();
}
catch (Exception e)
{
}
nextTransfer[id] = false;
}
}
Send(new byte[] { 1, 1 }, endPoint);
Console.WriteLine("world transfer finished for " + playerNames[id]);
}
private void Send(byte[] data, IPEndPoint endPoint)
{
client.Send(data, data.Length, endPoint);
@@ -201,13 +209,7 @@ public class Server
public void Disconnect()
{
receiveThread.Abort();
for (int i = 0; i < transferThreads.Count; i++)
{
if (transferThreads[i] != null)
{
transferThreads[i].Abort();
}
}
client.Close();
}
}
+2
View File
@@ -58,6 +58,7 @@ public class World : MonoBehaviour
{
CreateChunks();
}
Console.WriteLine("world loaded! \n");
server = new Server(this, transferDelay);
}
@@ -214,5 +215,6 @@ public class World : MonoBehaviour
}
}
writer.Close();
Console.WriteLine("world saved! \n");
}
}
Binary file not shown.
@@ -134,7 +134,7 @@ PlayerSettings:
16:10: 1
16:9: 1
Others: 1
bundleVersion: 0.1
bundleVersion: 0.2
preloadedAssets: []
metroInputSource: 0
wsaTransparentSwapchain: 0