Initial Commit (broken due to version update)
This commit is contained in:
parent
74003df84b
commit
3f7b3b7440
3132 changed files with 1103305 additions and 0 deletions
104
Assets/Scripts/LiteNetLib/NetThread.cs
Normal file
104
Assets/Scripts/LiteNetLib/NetThread.cs
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
#if WINRT && !UNITY_EDITOR
|
||||
#define USE_WINRT
|
||||
#endif
|
||||
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
#if USE_WINRT
|
||||
using Windows.Foundation;
|
||||
using Windows.System.Threading;
|
||||
using Windows.System.Threading.Core;
|
||||
#endif
|
||||
|
||||
namespace LiteNetLib
|
||||
{
|
||||
internal sealed class NetThread
|
||||
{
|
||||
#if USE_WINRT
|
||||
private readonly ManualResetEvent _updateWaiter = new ManualResetEvent(false);
|
||||
private readonly ManualResetEvent _joinWaiter = new ManualResetEvent(false);
|
||||
#else
|
||||
private Thread _thread;
|
||||
#endif
|
||||
|
||||
private readonly Action _callback;
|
||||
|
||||
public int SleepTime;
|
||||
private volatile bool _running;
|
||||
private readonly string _name;
|
||||
|
||||
public bool IsRunning
|
||||
{
|
||||
get { return _running; }
|
||||
}
|
||||
|
||||
public NetThread(string name, int sleepTime, Action callback)
|
||||
{
|
||||
_callback = callback;
|
||||
SleepTime = sleepTime;
|
||||
_name = name;
|
||||
}
|
||||
|
||||
public void Sleep(int msec)
|
||||
{
|
||||
#if USE_WINRT
|
||||
_updateWaiter.WaitOne(msec);
|
||||
#else
|
||||
Thread.Sleep(msec);
|
||||
#endif
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
if (_running)
|
||||
return;
|
||||
_running = true;
|
||||
#if USE_WINRT
|
||||
var thread = new PreallocatedWorkItem(ThreadLogic, WorkItemPriority.Normal, WorkItemOptions.TimeSliced);
|
||||
thread.RunAsync().AsTask();
|
||||
#else
|
||||
_thread = new Thread(ThreadLogic)
|
||||
{
|
||||
Name = _name,
|
||||
IsBackground = true
|
||||
};
|
||||
_thread.Start();
|
||||
#endif
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
if (!_running)
|
||||
return;
|
||||
_running = false;
|
||||
|
||||
#if USE_WINRT
|
||||
_joinWaiter.WaitOne();
|
||||
#else
|
||||
_thread.Join();
|
||||
#endif
|
||||
}
|
||||
|
||||
#if USE_WINRT
|
||||
private void ThreadLogic(IAsyncAction action)
|
||||
{
|
||||
while (_running)
|
||||
{
|
||||
_callback();
|
||||
_updateWaiter.WaitOne(SleepTime);
|
||||
}
|
||||
_joinWaiter.Set();
|
||||
}
|
||||
#else
|
||||
private void ThreadLogic()
|
||||
{
|
||||
while (_running)
|
||||
{
|
||||
_callback();
|
||||
Thread.Sleep(SleepTime);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue