|
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using GameCore.Network;
|
|
|
|
|
using Unity.Collections;
|
|
|
|
|
using Unity.Jobs;
|
|
|
|
|
using Unity.Networking.Transport;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace ECSTest
|
|
|
|
|
{
|
|
|
|
|
public class ServerMain : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
public string address = "127.0.0.1";
|
|
|
|
|
public NetworkDriverAsset driverAsset;
|
|
|
|
|
|
|
|
|
|
private NetworkDriver driver;
|
|
|
|
|
private NetworkPipeline unreliablePipeline;
|
|
|
|
|
private NetworkPipeline reliablePipeline;
|
|
|
|
|
private JobHandle networkJobHandle;
|
|
|
|
|
private NativeList<NetworkConnection> connectionList;
|
|
|
|
|
private NativeList<int> disconnectIndexList;
|
|
|
|
|
|
|
|
|
|
private void OnEnable()
|
|
|
|
|
{
|
|
|
|
|
driver = driverAsset.CreateServerDriver(address, out unreliablePipeline, out reliablePipeline);
|
|
|
|
|
|
|
|
|
|
connectionList = new NativeList<NetworkConnection>(16, Allocator.Persistent);
|
|
|
|
|
disconnectIndexList = new NativeList<int>(16, Allocator.Persistent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnDisable()
|
|
|
|
|
{
|
|
|
|
|
networkJobHandle.Complete();
|
|
|
|
|
|
|
|
|
|
driver.Dispose();
|
|
|
|
|
|
|
|
|
|
connectionList.Dispose();
|
|
|
|
|
disconnectIndexList.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Update()
|
|
|
|
|
{
|
|
|
|
|
networkJobHandle.Complete();
|
|
|
|
|
|
|
|
|
|
var concurrentDriver = driver.ToConcurrent();
|
|
|
|
|
|
|
|
|
|
networkJobHandle = driver.ScheduleUpdate();
|
|
|
|
|
networkJobHandle = new ServerConnectionJob()
|
|
|
|
|
{
|
|
|
|
|
driver = driver,
|
|
|
|
|
connectionList = connectionList,
|
|
|
|
|
disconnectIndexList = disconnectIndexList,
|
|
|
|
|
}.Schedule(networkJobHandle);
|
|
|
|
|
|
|
|
|
|
networkJobHandle = new ServerReceiveJob()
|
|
|
|
|
{
|
|
|
|
|
driver = concurrentDriver,
|
|
|
|
|
connectionList = connectionList,
|
|
|
|
|
disconnectIndexList = disconnectIndexList.AsParallelWriter(),
|
|
|
|
|
}.Schedule(connectionList, 16, networkJobHandle);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|