Skip to content

Instantly share code, notes, and snippets.

@duynguye duynguye/ServerBehavior.cs Secret
Created Apr 24, 2019

Embed
What would you like to do?
using UnityEngine;
using Unity.Networking.Transport;
using Unity.Networking.Transport.Utilities;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using NetworkConnection = Unity.Networking.Transport.NetworkConnection;
public class ServerBehavior : MonoBehaviour
{
public UdpNetworkDriver m_Driver;
public NetworkPipeline m_Pipeline;
public NetworkEndPoint m_Endpoint;
private NativeList<NetworkConnection> m_Connections;
// Start is called before the first frame update
private void Start()
{
m_Driver = new UdpNetworkDriver(new SimulatorUtility.Parameters { MaxPacketSize = 256, MaxPacketCount = 30, PacketDelayMs = 100 });
m_Pipeline = m_Driver.CreatePipeline(typeof(UnreliableSequencedPipelineStage), typeof(SimulatorPipelineStage));
m_Endpoint = new NetworkEndPoint();
m_Endpoint = NetworkEndPoint.Parse("0.0.0.0", 9000);
if (m_Driver.Bind(m_Endpoint) != 0)
{
Debug.Log("Failed to bind to port 9000");
}
else
{
m_Driver.Listen();
}
m_Connections = new NativeList<NetworkConnection>(16, Allocator.Persistent);
}
private void OnDestroy()
{
m_Driver.Dispose();
m_Connections.Dispose();
}
// Update is called once per frame
private void Update()
{
m_Driver.ScheduleUpdate().Complete();
// Clean up connections
for (int i = 0; i < m_Connections.Length; i++)
{
if (!m_Connections[i].IsCreated)
{
m_Connections.RemoveAtSwapBack(i);
--i;
}
}
// Accept new connections
NetworkConnection connection;
while ((connection = m_Driver.Accept()) != default(NetworkConnection))
{
m_Connections.Add(connection);
Debug.Log("Accepted a connection");
}
DataStreamReader stream;
for (int i = 0; i < m_Connections.Length; i++)
{
if (!m_Connections[i].IsCreated)
{
continue;
}
NetworkEvent.Type cmd;
while ((cmd = m_Driver.PopEventForConnection(m_Connections[i], out stream)) != NetworkEvent.Type.Empty)
{
if (cmd == NetworkEvent.Type.Data)
{
var readerCtx = default(DataStreamReader.Context);
uint number = stream.ReadUInt(ref readerCtx);
Debug.Log("Got " + number + " from the client adding + 2 to it and sending it back.");
number += 2;
using (var writer = new DataStreamWriter(4, Allocator.Temp))
{
writer.Write(number);
m_Driver.Send(m_Pipeline, m_Connections[i], writer);
}
}
else if (cmd == NetworkEvent.Type.Disconnect)
{
Debug.Log("Client disconnected from the server.");
m_Connections[i] = default;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.