216 lines
6.5 KiB
C#
216 lines
6.5 KiB
C#
using System.Collections.Generic;
|
|
using System.Net;
|
|
using System.Threading;
|
|
|
|
namespace BF
|
|
{
|
|
internal partial class TCPChannelService : INetChannelService
|
|
{
|
|
private NetConfiguration configuration;
|
|
private Thread networkThread;
|
|
|
|
/// <summary>
|
|
/// 主线程同步
|
|
/// </summary>
|
|
private readonly AutoResetEvent mainThreadResetEvent;
|
|
|
|
private readonly NetSafeQueue<BaseCommandMethod> commandMethodQueue = new NetSafeQueue<BaseCommandMethod>(2);
|
|
|
|
public NetServiceType ServiceType => configuration.ServiceType;
|
|
|
|
public TCPChannelService()
|
|
{
|
|
mainThreadResetEvent = new AutoResetEvent(false);
|
|
}
|
|
|
|
~TCPChannelService()
|
|
{
|
|
mainThreadResetEvent?.Dispose();
|
|
quitEvent?.Dispose();
|
|
}
|
|
|
|
public void Start(NetConfiguration configuration)
|
|
{
|
|
this.configuration = configuration;
|
|
|
|
networkThread = new Thread(new ThreadStart(NetworkLoop))
|
|
{
|
|
IsBackground = true,
|
|
Name = configuration.ThreadName
|
|
};
|
|
|
|
networkThread.Start();
|
|
}
|
|
|
|
public void Shutdown()
|
|
{
|
|
var commandMethod = ShutdownMethod.DefaultInstance;
|
|
commandMethod.ResetMethod();
|
|
commandMethodQueue.Enqueue(commandMethod);
|
|
|
|
quitEvent.Set();
|
|
|
|
mainThreadResetEvent.WaitOne();
|
|
|
|
quitFlag = true;
|
|
|
|
quitEvent.Reset();
|
|
}
|
|
|
|
public void Connect(NetConnectConfiguration configuration, string domainName, int port)
|
|
{
|
|
var commandMethod = ConnectDomainNameMethod.DefaultInstance;
|
|
commandMethod.ResetMethod();
|
|
|
|
commandMethod.Configuration = configuration;
|
|
commandMethod.DomainName = domainName;
|
|
commandMethod.Port = port;
|
|
commandMethodQueue.Enqueue(commandMethod);
|
|
|
|
mainThreadResetEvent.WaitOne();
|
|
}
|
|
|
|
public void Connect(NetConnectConfiguration configuration, IPAddress ipAddress, int port)
|
|
{
|
|
var commandMethod = ConnectIPAddressMethod.DefaultInstance;
|
|
commandMethod.ResetMethod();
|
|
|
|
commandMethod.Configuration = configuration;
|
|
commandMethod.IpAddress = ipAddress;
|
|
commandMethod.Port = port;
|
|
commandMethodQueue.Enqueue(commandMethod);
|
|
|
|
mainThreadResetEvent.WaitOne();
|
|
}
|
|
|
|
public void Reconnect(string uniqueIdentifier)
|
|
{
|
|
var commandMethod = ReconnectMethod.DefaultInstance;
|
|
commandMethod.ResetMethod();
|
|
|
|
commandMethod.UniqueIdentifier = uniqueIdentifier;
|
|
commandMethodQueue.Enqueue(commandMethod);
|
|
|
|
mainThreadResetEvent.WaitOne();
|
|
}
|
|
|
|
public void Close(string uniqueIdentifier)
|
|
{
|
|
var commandMethod = CloseMethod.DefaultInstance;
|
|
commandMethod.ResetMethod();
|
|
|
|
commandMethod.UniqueIdentifier = uniqueIdentifier;
|
|
commandMethodQueue.Enqueue(commandMethod);
|
|
|
|
mainThreadResetEvent.WaitOne();
|
|
}
|
|
|
|
public NetConnectStatus GetConnectStatus(string uniqueIdentifier)
|
|
{
|
|
var commandMethod = GetConnectStatusMethod.DefaultInstance;
|
|
commandMethod.ResetMethod();
|
|
|
|
commandMethod.UniqueIdentifier = uniqueIdentifier;
|
|
commandMethodQueue.Enqueue(commandMethod);
|
|
|
|
mainThreadResetEvent.WaitOne();
|
|
|
|
return commandMethod.Result;
|
|
}
|
|
|
|
public string GetConnectIP(string uniqueIdentifier)
|
|
{
|
|
var commandMethod = GetConnectIPMethod.DefaultInstance;
|
|
commandMethod.ResetMethod();
|
|
|
|
commandMethod.UniqueIdentifier = uniqueIdentifier;
|
|
commandMethodQueue.Enqueue(commandMethod);
|
|
|
|
mainThreadResetEvent.WaitOne();
|
|
|
|
return commandMethod.Result;
|
|
}
|
|
|
|
public long GetConnectServerTimestamp(string uniqueIdentifier)
|
|
{
|
|
var commandMethod = GetConnectServerTimestampMethod.DefaultInstance;
|
|
commandMethod.ResetMethod();
|
|
|
|
commandMethod.UniqueIdentifier = uniqueIdentifier;
|
|
commandMethodQueue.Enqueue(commandMethod);
|
|
|
|
mainThreadResetEvent.WaitOne();
|
|
|
|
return commandMethod.Result;
|
|
}
|
|
|
|
public INetOutgoingMessage CreateMessage(string uniqueIdentifier)
|
|
{
|
|
var commandMethod = CreateMessageMethod.DefaultInstance;
|
|
commandMethod.ResetMethod();
|
|
|
|
commandMethod.UniqueIdentifier = uniqueIdentifier;
|
|
commandMethodQueue.Enqueue(commandMethod);
|
|
|
|
mainThreadResetEvent.WaitOne();
|
|
|
|
return commandMethod.Result;
|
|
}
|
|
|
|
public NetSendResult Send(string uniqueIdentifier, INetOutgoingMessage message)
|
|
{
|
|
var commandMethod = SendMessageMethod.DefaultInstance;
|
|
commandMethod.ResetMethod();
|
|
|
|
commandMethod.UniqueIdentifier = uniqueIdentifier;
|
|
commandMethod.Message = message;
|
|
commandMethodQueue.Enqueue(commandMethod);
|
|
|
|
mainThreadResetEvent.WaitOne();
|
|
|
|
return commandMethod.Result;
|
|
}
|
|
|
|
public NetSendResult Send(string uniqueIdentifier, uint group, byte cmd, byte[] data)
|
|
{
|
|
var commandMethod = SendRawMethod.DefaultInstance;
|
|
commandMethod.ResetMethod();
|
|
|
|
commandMethod.UniqueIdentifier = uniqueIdentifier;
|
|
commandMethod.Group = group;
|
|
commandMethod.CMD = cmd;
|
|
commandMethod.Data = data;
|
|
commandMethodQueue.Enqueue(commandMethod);
|
|
|
|
mainThreadResetEvent.WaitOne();
|
|
|
|
return commandMethod.Result;
|
|
}
|
|
|
|
public INetIncomingMessage ReadMessage(string uniqueIdentifier)
|
|
{
|
|
var commandMethod = ReadMessageMethod.DefaultInstance;
|
|
commandMethod.ResetMethod();
|
|
|
|
commandMethod.UniqueIdentifier = uniqueIdentifier;
|
|
commandMethodQueue.Enqueue(commandMethod);
|
|
|
|
mainThreadResetEvent.WaitOne();
|
|
|
|
return commandMethod.Result;
|
|
}
|
|
|
|
public void RecycleMessage(string uniqueIdentifier, INetIncomingMessage message)
|
|
{
|
|
var commandMethod = RecycleMessageMethod.DefaultInstance;
|
|
commandMethod.ResetMethod();
|
|
|
|
commandMethod.UniqueIdentifier = uniqueIdentifier;
|
|
commandMethod.Message = message;
|
|
commandMethodQueue.Enqueue(commandMethod);
|
|
|
|
mainThreadResetEvent.WaitOne();
|
|
}
|
|
|
|
}
|
|
} |