Last active
June 26, 2020 21:17
-
-
Save James-Frowen/79728faf66fdd974851265884c2053d1 to your computer and use it in GitHub Desktop.
Script to send image over multiple packets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.IO; | |
using Mirror; | |
using UnityEngine; | |
namespace ScreenShotDemo | |
{ | |
public class SendImage : MonoBehaviour | |
{ | |
public int bytesPerPacket = 8000; | |
IEnumerator Start() | |
{ | |
// wait for server or client to be active | |
while (!NetworkServer.active && !NetworkClient.active) | |
{ | |
yield return null; | |
} | |
// if server is active then register handler | |
if (NetworkServer.active) | |
{ | |
NetworkServer.RegisterHandler<SendImageMessage>(OnSendImageReceived); | |
yield break; | |
} | |
// else client must be active | |
// Take and send screen shot | |
string filePath = string.Format("{0}/SendImageDemo.png", Application.persistentDataPath); | |
// wait for client to fully start | |
yield return new WaitForSeconds(1); | |
yield return TakeScreenShot(filePath); | |
yield return SendImageToServer(filePath); | |
} | |
IEnumerator TakeScreenShot(string filePath) | |
{ | |
ScreenCapture.CaptureScreenshot(filePath); | |
// give screenshot enough time to save | |
yield return new WaitForSeconds(1); | |
} | |
IEnumerator SendImageToServer(string filePath) | |
{ | |
byte[] bytes = File.ReadAllBytes(filePath); | |
int packetCount = bytes.Length / bytesPerPacket; | |
for (int i = 0; i < packetCount; i++) | |
{ | |
NetworkClient.Send(new SendImageMessage | |
{ | |
index = i, | |
imageSize = bytes.Length, | |
segment = new ArraySegment<byte>(bytes, i * bytesPerPacket, bytesPerPacket), | |
finished = false | |
}); | |
yield return null; | |
} | |
// send last message that isnt the the full size | |
NetworkClient.Send(new SendImageMessage | |
{ | |
index = packetCount, | |
imageSize = bytes.Length, | |
segment = new ArraySegment<byte>(bytes, packetCount * bytesPerPacket, bytes.Length - packetCount * bytesPerPacket), | |
finished = true | |
}); | |
} | |
byte[] incomingBytes; | |
void OnSendImageReceived(NetworkConnection arg1, SendImageMessage msg) | |
{ | |
if (msg.index == 0) | |
{ | |
incomingBytes = new byte[msg.imageSize]; | |
} | |
Array.Copy(msg.segment.Array, msg.segment.Offset, incomingBytes, msg.index * bytesPerPacket, msg.segment.Count); | |
if (msg.finished) | |
{ | |
string filePath = string.Format("{0}/SendImageDemoIncoming.png", Application.persistentDataPath); | |
File.WriteAllBytes(filePath, incomingBytes); | |
} | |
} | |
} | |
public struct SendImageMessage : IMessageBase | |
{ | |
public ArraySegment<byte> segment; | |
public int index; | |
public int imageSize; | |
public bool finished; | |
public void Deserialize(NetworkReader reader) | |
{ | |
segment = reader.ReadBytesAndSizeSegment(); | |
index = reader.ReadInt32(); | |
imageSize = reader.ReadInt32(); | |
finished = reader.ReadBoolean(); | |
} | |
public void Serialize(NetworkWriter writer) | |
{ | |
writer.WriteBytesAndSizeSegment(segment); | |
writer.WriteInt32(index); | |
writer.WriteInt32(imageSize); | |
writer.WriteBoolean(finished); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment