-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetworkDataSender
36 lines (30 loc) · 1.03 KB
/
NetworkDataSender
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
#pragma warning disable CS0618 // Type or member is obsolete
public class NetworkDataSender
{
readonly int BYTE_SIZE;
NetworkConnection connection;
public NetworkDataSender(int byte_size)
{
BYTE_SIZE = byte_size;
this.connection = Client.GetInstance().GetConnection();
}
public void SendServerReliable(NetMsg msg)
{
byte error;
if (connection == null) return;
if (!connection.isStarted) return;
byte[] buffer = new byte[BYTE_SIZE];
using (MemoryStream ms = new MemoryStream(buffer))
{
BinaryWriter writer = new BinaryWriter(ms);
writer.Write(msg.ToString());
Debug.Log($"msg = {msg.ToString()}, message byte size = " + (int)ms.Position);
NetworkTransport.Send(connection.hostId, connection.connectionId, connection.reliableChannel, buffer, BYTE_SIZE, out error);
}
}
}