TCP for world transfer
wichtige daten bei der UDP übertragung gehen verloren.
This commit is contained in:
@@ -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>
|
||||||
BIN
Binary file not shown.
|
Before Width: | Height: | Size: 78 KiB After Width: | Height: | Size: 50 KiB |
+1
-1
@@ -50,7 +50,7 @@ TextureImporter:
|
|||||||
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
|
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
|
||||||
spriteGenerateFallbackPhysicsShape: 1
|
spriteGenerateFallbackPhysicsShape: 1
|
||||||
alphaUsage: 1
|
alphaUsage: 1
|
||||||
alphaIsTransparency: 0
|
alphaIsTransparency: 1
|
||||||
spriteTessellationDetail: -1
|
spriteTessellationDetail: -1
|
||||||
textureType: 0
|
textureType: 0
|
||||||
textureShape: 1
|
textureShape: 1
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
@@ -27,8 +28,7 @@ public class Client
|
|||||||
client = new UdpClient();
|
client = new UdpClient();
|
||||||
client.DontFragment = false;
|
client.DontFragment = false;
|
||||||
|
|
||||||
IPAddress address;
|
if (IPAddress.TryParse(hostname, out IPAddress address))
|
||||||
if (IPAddress.TryParse(hostname, out address))
|
|
||||||
{
|
{
|
||||||
address = IPAddress.Parse(hostname);
|
address = IPAddress.Parse(hostname);
|
||||||
client.Connect(address, port);
|
client.Connect(address, port);
|
||||||
@@ -50,7 +50,7 @@ public class Client
|
|||||||
}
|
}
|
||||||
|
|
||||||
Send(sendList.ToArray());
|
Send(sendList.ToArray());
|
||||||
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0);
|
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 8051);
|
||||||
byte[] data = client.Receive(ref endPoint);
|
byte[] data = client.Receive(ref endPoint);
|
||||||
if (data[0] == 2)
|
if (data[0] == 2)
|
||||||
{
|
{
|
||||||
@@ -64,10 +64,23 @@ public class Client
|
|||||||
byte xChunk = 0;
|
byte xChunk = 0;
|
||||||
byte zChunk = 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)
|
while (true)
|
||||||
{
|
{
|
||||||
endPoint = new IPEndPoint(IPAddress.Any, 0);
|
data = Encoding.ASCII.GetBytes(reader.ReadLine());
|
||||||
data = client.Receive(ref endPoint);
|
|
||||||
|
|
||||||
if (data[0] == 1)
|
if (data[0] == 1)
|
||||||
{
|
{
|
||||||
@@ -102,23 +115,26 @@ public class Client
|
|||||||
zChunk = 0;
|
zChunk = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Send(new byte[]{5});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
reader.Close();
|
||||||
|
tcpClient.Close();
|
||||||
|
|
||||||
world.CreateChunks();
|
world.CreateChunks();
|
||||||
//world transfer finished
|
//world transfer finished
|
||||||
|
|
||||||
//start listening
|
//start listening
|
||||||
receiveThread = new Thread(Receive);
|
receiveThread = new Thread(Receive);
|
||||||
receiveThread.Start();
|
receiveThread.Start();
|
||||||
|
|
||||||
|
Send(new byte[]{6});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Receive()
|
private void Receive()
|
||||||
{
|
{
|
||||||
while(true)
|
while(true)
|
||||||
{
|
{
|
||||||
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0);
|
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 8051);
|
||||||
byte[] data = client.Receive(ref endPoint);
|
byte[] data = client.Receive(ref endPoint);
|
||||||
|
|
||||||
if (data[0] == 3)
|
if (data[0] == 3)
|
||||||
|
|||||||
@@ -42,8 +42,8 @@ PlayerSettings:
|
|||||||
m_SplashScreenLogos: []
|
m_SplashScreenLogos: []
|
||||||
m_VirtualRealitySplashScreen: {fileID: 0}
|
m_VirtualRealitySplashScreen: {fileID: 0}
|
||||||
m_HolographicTrackingLossScreen: {fileID: 0}
|
m_HolographicTrackingLossScreen: {fileID: 0}
|
||||||
defaultScreenWidth: 1024
|
defaultScreenWidth: 512
|
||||||
defaultScreenHeight: 768
|
defaultScreenHeight: 384
|
||||||
defaultScreenWidthWeb: 960
|
defaultScreenWidthWeb: 960
|
||||||
defaultScreenHeightWeb: 600
|
defaultScreenHeightWeb: 600
|
||||||
m_StereoRenderingPath: 0
|
m_StereoRenderingPath: 0
|
||||||
@@ -88,7 +88,7 @@ PlayerSettings:
|
|||||||
bakeCollisionMeshes: 0
|
bakeCollisionMeshes: 0
|
||||||
forceSingleInstance: 0
|
forceSingleInstance: 0
|
||||||
useFlipModelSwapchain: 1
|
useFlipModelSwapchain: 1
|
||||||
resizableWindow: 0
|
resizableWindow: 1
|
||||||
useMacAppStoreValidation: 0
|
useMacAppStoreValidation: 0
|
||||||
macAppStoreCategory: public.app-category.games
|
macAppStoreCategory: public.app-category.games
|
||||||
gpuSkinning: 1
|
gpuSkinning: 1
|
||||||
@@ -99,7 +99,7 @@ PlayerSettings:
|
|||||||
xboxEnableFitness: 0
|
xboxEnableFitness: 0
|
||||||
visibleInBackground: 1
|
visibleInBackground: 1
|
||||||
allowFullscreenSwitch: 1
|
allowFullscreenSwitch: 1
|
||||||
fullscreenMode: 1
|
fullscreenMode: 3
|
||||||
xboxSpeechDB: 0
|
xboxSpeechDB: 0
|
||||||
xboxEnableHeadOrientation: 0
|
xboxEnableHeadOrientation: 0
|
||||||
xboxEnableGuest: 0
|
xboxEnableGuest: 0
|
||||||
@@ -134,7 +134,7 @@ PlayerSettings:
|
|||||||
16:10: 1
|
16:10: 1
|
||||||
16:9: 1
|
16:9: 1
|
||||||
Others: 1
|
Others: 1
|
||||||
bundleVersion: 0.1
|
bundleVersion: 0.2
|
||||||
preloadedAssets: []
|
preloadedAssets: []
|
||||||
metroInputSource: 0
|
metroInputSource: 0
|
||||||
wsaTransparentSwapchain: 0
|
wsaTransparentSwapchain: 0
|
||||||
|
|||||||
BIN
Binary file not shown.
|
Before Width: | Height: | Size: 9.2 KiB After Width: | Height: | Size: 17 KiB |
+1
-1
@@ -50,7 +50,7 @@ TextureImporter:
|
|||||||
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
|
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
|
||||||
spriteGenerateFallbackPhysicsShape: 1
|
spriteGenerateFallbackPhysicsShape: 1
|
||||||
alphaUsage: 1
|
alphaUsage: 1
|
||||||
alphaIsTransparency: 0
|
alphaIsTransparency: 1
|
||||||
spriteTessellationDetail: -1
|
spriteTessellationDetail: -1
|
||||||
textureType: 0
|
textureType: 0
|
||||||
textureShape: 1
|
textureShape: 1
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
@@ -17,9 +18,7 @@ public class Server
|
|||||||
private int transferDelay;
|
private int transferDelay;
|
||||||
private List<IPEndPoint> players = new List<IPEndPoint>();
|
private List<IPEndPoint> players = new List<IPEndPoint>();
|
||||||
private List<string> playerNames = new List<string>();
|
private List<string> playerNames = new List<string>();
|
||||||
private List<bool> nextTransfer = new List<bool>();
|
|
||||||
private Thread receiveThread;
|
private Thread receiveThread;
|
||||||
private List<Thread> transferThreads = new List<Thread>();
|
|
||||||
|
|
||||||
public Server(World world, int transferDelay)
|
public Server(World world, int transferDelay)
|
||||||
{
|
{
|
||||||
@@ -31,9 +30,9 @@ public class Server
|
|||||||
client.DontFragment = false;
|
client.DontFragment = false;
|
||||||
|
|
||||||
//start listening
|
//start listening
|
||||||
receiveThread = new Thread(Receive); //info:
|
receiveThread = new Thread(Receive);
|
||||||
receiveThread.Start();
|
receiveThread.Start();
|
||||||
//0: init message
|
Console.WriteLine("server running on port 8051" + "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
//info:
|
//info:
|
||||||
@@ -42,23 +41,19 @@ public class Server
|
|||||||
//[0] = 2: world size
|
//[0] = 2: world size
|
||||||
//[0] = 3: edit block
|
//[0] = 3: edit block
|
||||||
//[0] = 4: disconnect player
|
//[0] = 4: disconnect player
|
||||||
//[0] = 5: next transfer
|
//[0] = 5:
|
||||||
|
//[0] = 6: player ready
|
||||||
|
//[0] = 7:
|
||||||
|
|
||||||
private void Receive()
|
private void Receive()
|
||||||
{
|
{
|
||||||
try
|
while (true)
|
||||||
{
|
{
|
||||||
while (true)
|
try
|
||||||
{
|
{
|
||||||
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0);
|
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 8051);
|
||||||
byte[] data = client.Receive(ref endPoint);
|
byte[] data = client.Receive(ref endPoint);
|
||||||
|
|
||||||
if (data[0] == 5)
|
|
||||||
{
|
|
||||||
int id = GetPlayerID(endPoint);
|
|
||||||
nextTransfer[id] = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data[0] == 4)
|
if (data[0] == 4)
|
||||||
{
|
{
|
||||||
IPEndPoint tempEndPoint = null;
|
IPEndPoint tempEndPoint = null;
|
||||||
@@ -78,17 +73,15 @@ public class Server
|
|||||||
if (tempEndPoint != null)
|
if (tempEndPoint != null)
|
||||||
{
|
{
|
||||||
players.Remove(tempEndPoint);
|
players.Remove(tempEndPoint);
|
||||||
Console.WriteLine(playerNames[i] + " left the game");
|
Console.WriteLine(playerNames[i] + " left the game" + "\n");
|
||||||
playerNames.RemoveAt(i);
|
playerNames.RemoveAt(i);
|
||||||
nextTransfer.RemoveAt(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data[0] == 0)
|
if (data[0] == 0)
|
||||||
{
|
{
|
||||||
Thread t = new Thread(TransferWorld);
|
Thread t = new Thread(TransferWorld);
|
||||||
transferThreads.Add(t);
|
|
||||||
t.Start(endPoint);
|
t.Start(endPoint);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,8 +99,6 @@ public class Server
|
|||||||
//add player
|
//add player
|
||||||
players.Add(endPoint);
|
players.Add(endPoint);
|
||||||
playerNames.Add(name);
|
playerNames.Add(name);
|
||||||
nextTransfer.Add(false);
|
|
||||||
Console.WriteLine(name + " joined the game");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data[0] == 3)
|
if (data[0] == 3)
|
||||||
@@ -127,11 +118,19 @@ public class Server
|
|||||||
}, e);
|
}, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
|
|
||||||
|
if (data[0] == 6)
|
||||||
|
{
|
||||||
|
int id = GetPlayerID(endPoint);
|
||||||
|
Console.WriteLine(playerNames[id] + " joined" + "\n");
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -153,44 +152,53 @@ public class Server
|
|||||||
|
|
||||||
private void TransferWorld(object o)
|
private void TransferWorld(object o)
|
||||||
{
|
{
|
||||||
IPEndPoint endPoint = (IPEndPoint)o;
|
try
|
||||||
int id = GetPlayerID(endPoint);
|
|
||||||
|
|
||||||
Console.WriteLine("begin world transfer for " + playerNames[id]);
|
|
||||||
for (int xChunk = 0; xChunk < world.worldSize; xChunk++)
|
|
||||||
{
|
{
|
||||||
for (int zChunk = 0; zChunk < world.worldSize; zChunk++)
|
IPEndPoint endPoint = (IPEndPoint)o;
|
||||||
|
int id = GetPlayerID(endPoint);
|
||||||
|
|
||||||
|
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++)
|
||||||
{
|
{
|
||||||
nextTransfer[id] = false;
|
for (int zChunk = 0; zChunk < world.worldSize; zChunk++)
|
||||||
|
|
||||||
byte[] send = new byte[Data.chunkWidth * Data.chunkWidth * Data.chunkHeight + 2];
|
|
||||||
send[0] = 1;
|
|
||||||
send[1] = 0;
|
|
||||||
int i = 2;
|
|
||||||
|
|
||||||
for (int y = 0; y < Data.chunkHeight; y++)
|
|
||||||
{
|
{
|
||||||
for (int x = 0; x < Data.chunkWidth; x++)
|
byte[] send = new byte[Data.chunkWidth * Data.chunkWidth * Data.chunkHeight + 2];
|
||||||
|
send[0] = 1;
|
||||||
|
send[1] = 0;
|
||||||
|
int i = 2;
|
||||||
|
|
||||||
|
for (int y = 0; y < Data.chunkHeight; y++)
|
||||||
{
|
{
|
||||||
for (int z = 0; z < Data.chunkWidth; z++)
|
for (int x = 0; x < Data.chunkWidth; x++)
|
||||||
{
|
{
|
||||||
send[i] = world.chunks[xChunk, zChunk].blocks[x, y, z];
|
for (int z = 0; z < Data.chunkWidth; z++)
|
||||||
i++;
|
{
|
||||||
|
send[i] = world.chunks[xChunk, zChunk].blocks[x, y, z];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
writer.WriteLine(Encoding.ASCII.GetString(send));
|
||||||
}
|
}
|
||||||
Send(send, endPoint);
|
|
||||||
|
|
||||||
while (!nextTransfer[id])
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
nextTransfer[id] = false;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Send(new byte[] { 1, 1 }, endPoint);
|
writer.WriteLine(Encoding.ASCII.GetString(new byte[] { 1, 1 }));
|
||||||
Console.WriteLine("world transfer finished for " + playerNames[id]);
|
Console.WriteLine("world transfer finished for " + playerNames[id] + "\n");
|
||||||
|
writer.Close();
|
||||||
|
tcpClient.Close();
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Send(byte[] data, IPEndPoint endPoint)
|
private void Send(byte[] data, IPEndPoint endPoint)
|
||||||
@@ -201,13 +209,7 @@ public class Server
|
|||||||
public void Disconnect()
|
public void Disconnect()
|
||||||
{
|
{
|
||||||
receiveThread.Abort();
|
receiveThread.Abort();
|
||||||
for (int i = 0; i < transferThreads.Count; i++)
|
|
||||||
{
|
|
||||||
if (transferThreads[i] != null)
|
|
||||||
{
|
|
||||||
transferThreads[i].Abort();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
client.Close();
|
client.Close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -58,6 +58,7 @@ public class World : MonoBehaviour
|
|||||||
{
|
{
|
||||||
CreateChunks();
|
CreateChunks();
|
||||||
}
|
}
|
||||||
|
Console.WriteLine("world loaded! \n");
|
||||||
server = new Server(this, transferDelay);
|
server = new Server(this, transferDelay);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -214,5 +215,6 @@ public class World : MonoBehaviour
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
writer.Close();
|
writer.Close();
|
||||||
|
Console.WriteLine("world saved! \n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
@@ -134,7 +134,7 @@ PlayerSettings:
|
|||||||
16:10: 1
|
16:10: 1
|
||||||
16:9: 1
|
16:9: 1
|
||||||
Others: 1
|
Others: 1
|
||||||
bundleVersion: 0.1
|
bundleVersion: 0.2
|
||||||
preloadedAssets: []
|
preloadedAssets: []
|
||||||
metroInputSource: 0
|
metroInputSource: 0
|
||||||
wsaTransparentSwapchain: 0
|
wsaTransparentSwapchain: 0
|
||||||
|
|||||||
Reference in New Issue
Block a user