提交 574fc958 编写于 作者: T tanghai

整理代码格式

上级 52025150
......@@ -27,7 +27,7 @@ namespace Editor
{
IRegionBehaviorFactory factory = base.ConfigureDefaultRegionBehaviors();
factory.AddIfMissing("AutoPopulateExportedViewsBehavior",
typeof (AutoPopulateExportedViewsBehavior));
typeof (AutoPopulateExportedViewsBehavior));
return factory;
}
......
......@@ -92,7 +92,7 @@ namespace Modules.BehaviorTreeModule
public void Remove(int treeId)
{
TreeViewModel treeViewModel = treeViewModelsDict[treeId];
TreeViewModel treeViewModel = this.treeViewModelsDict[treeId];
this.treeViewModelsDict.Remove(treeId);
this.rootList.Remove(treeViewModel.Root);
}
......
......@@ -15,9 +15,9 @@ namespace Modules.BehaviorTreeModule
{
this.InitializeComponent();
nodeTypes = Enum.GetNames(typeof (NodeType));
Array.Sort(nodeTypes);
this.cbType.ItemsSource = nodeTypes;
this.nodeTypes = Enum.GetNames(typeof (NodeType));
Array.Sort(this.nodeTypes);
this.cbType.ItemsSource = this.nodeTypes;
}
public AllTreeView AllTreeView { get; set; }
......@@ -41,7 +41,7 @@ namespace Modules.BehaviorTreeModule
return;
}
string typeStr = ((NodeType) this.TreeNodeViewModel.Type).ToString();
int selectIndex = Array.IndexOf(nodeTypes, typeStr);
int selectIndex = Array.IndexOf(this.nodeTypes, typeStr);
this.cbType.SelectedIndex = selectIndex;
}
......
......@@ -36,8 +36,6 @@
// 方形陷阱选择目标
RectTrapSelectTarget = 100020,
// action节点 20000开始
CastSpell = 20000,
Chase = 20001,
......
......@@ -30,7 +30,7 @@ namespace Modules.BehaviorTreeModule
public TreeViewModel(AllTreeViewModel allTreeViewModel)
{
this.AllTreeViewModel = allTreeViewModel;
this.TreeId = ++AllTreeViewModel.MaxTreeId;
this.TreeId = ++this.AllTreeViewModel.MaxTreeId;
TreeNodeViewModel treeNodeViewModel = new TreeNodeViewModel(this, 300, 100);
this.treeNodes.Add(treeNodeViewModel);
this.treeNodeDict[treeNodeViewModel.Id] = treeNodeViewModel;
......@@ -52,13 +52,13 @@ namespace Modules.BehaviorTreeModule
TreeLayout treeLayout = new TreeLayout(this);
treeLayout.ExcuteLayout();
}
public List<TreeNodeData> GetDatas()
{
var treeNodeDatas = new List<TreeNodeData>();
foreach (TreeNodeViewModel treeNodeViewModel in this.treeNodes)
{
TreeNodeData treeNodeData = (TreeNodeData)treeNodeViewModel.Data.Clone();
TreeNodeData treeNodeData = (TreeNodeData) treeNodeViewModel.Data.Clone();
treeNodeDatas.Add(treeNodeData);
}
return treeNodeDatas;
......@@ -271,17 +271,17 @@ namespace Modules.BehaviorTreeModule
public void Copy(TreeNodeViewModel copyTreeNodeViewModel)
{
copyId = copyTreeNodeViewModel.Id;
this.copyId = copyTreeNodeViewModel.Id;
}
public void Paste(TreeNodeViewModel pasteTreeNodeViewModel)
{
if (copyId == 0)
if (this.copyId == 0)
{
return;
}
TreeNodeViewModel copyTreeNodeViewModel = treeNodeDict[copyId];
TreeNodeViewModel copyTreeNodeViewModel = this.treeNodeDict[this.copyId];
// copy节点不能是paste节点的父级节点
TreeNodeViewModel tmpNode = pasteTreeNodeViewModel;
while (tmpNode != null)
......@@ -296,13 +296,13 @@ namespace Modules.BehaviorTreeModule
}
tmpNode = tmpNode.Parent;
}
copyId = 0;
CopyTree(copyTreeNodeViewModel, pasteTreeNodeViewModel);
this.copyId = 0;
this.CopyTree(copyTreeNodeViewModel, pasteTreeNodeViewModel);
}
private void CopyTree(TreeNodeViewModel copyTreeNodeViewModel, TreeNodeViewModel parent)
{
TreeNodeData newTreeNodeData = (TreeNodeData)copyTreeNodeViewModel.Data.Clone();
TreeNodeData newTreeNodeData = (TreeNodeData) copyTreeNodeViewModel.Data.Clone();
newTreeNodeData.Id = ++this.AllTreeViewModel.MaxNodeId;
newTreeNodeData.TreeId = this.TreeId;
newTreeNodeData.Children.Clear();
......@@ -319,7 +319,7 @@ namespace Modules.BehaviorTreeModule
public object Clone()
{
int treeId = ++AllTreeViewModel.MaxTreeId;
int treeId = ++this.AllTreeViewModel.MaxTreeId;
List<TreeNodeData> treeNodeDatas = this.GetDatas();
// 旧id和新id的映射关系
var idMapping = new Dictionary<int, int>();
......
......@@ -38,7 +38,8 @@ namespace Model
Type classType = type;
this.dictionary.Add(attribute.NodeType,
config => (Node) Activator.CreateInstance(classType, new object[] { config }));
config =>
(Node) Activator.CreateInstance(classType, new object[] { config }));
}
}
......
......@@ -38,7 +38,7 @@ namespace Model
{
base.EndInit();
foreach (var buff in this.buffs)
foreach (Buff buff in this.buffs)
{
this.idBuff.Add(buff.Id, buff);
this.typeBuff.Add(buff.Config.Type, buff);
......@@ -50,13 +50,13 @@ namespace Model
if (this.buffs.Contains(buff))
{
throw new ArgumentException(string.Format("already exist same buff, Id: {0} ConfigId: {1}",
buff.Id, buff.Config.Id));
buff.Id, buff.Config.Id));
}
if (this.idBuff.ContainsKey(buff.Id))
{
throw new ArgumentException(string.Format("already exist same buff, Id: {0} ConfigId: {1}",
buff.Id, buff.Config.Id));
buff.Id, buff.Config.Id));
}
Env env = new Env();
......
......@@ -28,7 +28,7 @@ namespace Model
if (iEvent == null)
{
throw new Exception(string.Format("event not inherit IEvent interface: {0}",
obj.GetType().FullName));
obj.GetType().FullName));
}
AEventAttribute iEventAttribute = (AEventAttribute) attrs[0];
......
......@@ -16,18 +16,18 @@ namespace Model
switch (protocol)
{
case NetworkProtocol.TCP:
service = new TService("127.0.0.1", 8888);
this.service = new TService("127.0.0.1", 8888);
break;
case NetworkProtocol.UDP:
service = new UService("127.0.0.1", 8888);
this.service = new UService("127.0.0.1", 8888);
break;
default:
throw new ArgumentOutOfRangeException("protocol");
}
service.Add(AcceptChannel);
service.Run();
this.service.Add(this.AcceptChannel);
this.service.Run();
}
/// <summary>
......@@ -37,7 +37,7 @@ namespace Model
{
while (true)
{
IChannel channel = await service.GetChannel();
IChannel channel = await this.service.GetChannel();
ProcessChannel(channel);
}
}
......
......@@ -26,7 +26,7 @@ namespace Common.Base
{
throw new Exception(
string.Format("AddComponent, component already exist, id: {0}, component: {1}", this.Id,
typeof (K).Name));
typeof (K).Name));
}
if (this.components == null)
......@@ -45,7 +45,7 @@ namespace Common.Base
{
throw new Exception(
string.Format("AddComponent, component already exist, id: {0}, component: {1}", this.Id,
component.GetComponentType().Name));
component.GetComponentType().Name));
}
if (this.components == null)
......@@ -63,7 +63,7 @@ namespace Common.Base
{
throw new Exception(
string.Format("RemoveComponent, component not exist, id: {0}, component: {1}", this.Id,
typeof (K).Name));
typeof (K).Name));
}
this.components.Remove(component);
......@@ -105,7 +105,7 @@ namespace Common.Base
this.components = null;
return;
}
foreach (var component in this.components)
foreach (Component<T> component in this.components)
{
component.Owner = (T) this;
this.componentDict.Add(component.GetComponentType(), component);
......
......@@ -9,7 +9,7 @@ namespace Common.Base
public void Add(T t, K k)
{
list.Add(t);
this.list.Add(t);
this.dictionary.Add(t, k);
}
......@@ -32,7 +32,7 @@ namespace Common.Base
{
get
{
return list[0];
return this.list[0];
}
}
......
......@@ -38,7 +38,7 @@ namespace Common.Helper
using (MemoryStream memoryStream = new MemoryStream(bytes))
{
memoryStream.Seek(index, SeekOrigin.Begin);
return (T) BsonSerializer.Deserialize(memoryStream, typeof(T));
return (T) BsonSerializer.Deserialize(memoryStream, typeof (T));
}
}
}
......
......@@ -24,7 +24,8 @@ namespace Common.Helper
if (hexString.Length % 2 != 0)
{
throw new ArgumentException(String.Format(CultureInfo.InvariantCulture,
"The binary key cannot have an odd number of digits: {0}", hexString));
"The binary key cannot have an odd number of digits: {0}",
hexString));
}
var hexAsBytes = new byte[hexString.Length / 2];
......
......@@ -28,4 +28,4 @@ namespace Network
string RemoteAddress { get; }
}
}
}
\ No newline at end of file
......@@ -27,4 +27,4 @@ namespace Network
void Run();
}
}
}
\ No newline at end of file
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// 有关程序集的常规信息通过以下
// 特性集控制。更改这些特性值可修改
// 与程序集关联的信息。
[assembly: AssemblyTitle("Network")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
......@@ -17,9 +17,11 @@ using System.Runtime.InteropServices;
// 将 ComVisible 设置为 false 使此程序集中的类型
// 对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型,
// 则将该类型上的 ComVisible 特性设置为 true。
[assembly: ComVisible(false)]
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("16f6d5fb-6535-4e2b-afb5-e9a1b240c584")]
// 程序集的版本信息由下面四个值组成:
......@@ -32,5 +34,6 @@ using System.Runtime.InteropServices;
// 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
// 方法是按如下所示使用“*”:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
\ No newline at end of file
......@@ -8,4 +8,4 @@ namespace TNet
void Run(int timeout);
}
}
}
\ No newline at end of file
......@@ -34,31 +34,31 @@ namespace TNet
bool finish = false;
while (!finish)
{
switch (state)
switch (this.state)
{
case ParserState.PacketSize:
if (buffer.Count < 4)
if (this.buffer.Count < 4)
{
finish = true;
}
else
{
buffer.RecvFrom(packetSizeBuffer);
packetSize = BitConverter.ToInt32(packetSizeBuffer, 0);
state = ParserState.PacketBody;
this.buffer.RecvFrom(this.packetSizeBuffer);
this.packetSize = BitConverter.ToInt32(this.packetSizeBuffer, 0);
this.state = ParserState.PacketBody;
}
break;
case ParserState.PacketBody:
if (buffer.Count < packetSize)
if (this.buffer.Count < this.packetSize)
{
finish = true;
}
else
{
this.packet = new byte[packetSize];
buffer.RecvFrom(this.packet);
this.packet = new byte[this.packetSize];
this.buffer.RecvFrom(this.packet);
this.isOK = true;
state = ParserState.PacketSize;
this.state = ParserState.PacketSize;
finish = true;
}
break;
......@@ -69,9 +69,9 @@ namespace TNet
public byte[] GetPacket()
{
byte[] result = packet;
byte[] result = this.packet;
this.isOK = false;
return result;
}
}
}
}
\ No newline at end of file
......@@ -63,14 +63,15 @@ namespace TNet
{
if (this.Count < buffer.Length || buffer.Length == 0)
{
throw new Exception(string.Format("bufferList size < n, bufferList: {0} buffer length: {1}", this.Count, buffer.Length));
throw new Exception(string.Format("bufferList size < n, bufferList: {0} buffer length: {1}",
this.Count, buffer.Length));
}
int alreadyCopyCount = 0;
while (alreadyCopyCount < buffer.Length)
{
int n = buffer.Length - alreadyCopyCount;
if (ChunkSize - this.FirstIndex > n)
{
{
Array.Copy(this.bufferList.First.Value, this.FirstIndex, buffer, alreadyCopyCount, n);
this.FirstIndex += n;
alreadyCopyCount += n;
......@@ -78,7 +79,7 @@ namespace TNet
else
{
Array.Copy(this.bufferList.First.Value, this.FirstIndex, buffer, alreadyCopyCount,
ChunkSize - this.FirstIndex);
ChunkSize - this.FirstIndex);
alreadyCopyCount += ChunkSize - this.FirstIndex;
this.FirstIndex = 0;
this.bufferList.RemoveFirst();
......@@ -105,7 +106,7 @@ namespace TNet
else
{
Array.Copy(buffer, alreadyCopyCount, this.bufferList.Last.Value, this.LastIndex,
ChunkSize - this.LastIndex);
ChunkSize - this.LastIndex);
alreadyCopyCount += ChunkSize - this.LastIndex;
this.LastIndex = 0;
}
......
......@@ -26,15 +26,15 @@ namespace TNet
{
this.socket = socket;
this.service = service;
this.parser = new PacketParser(recvBuffer);
this.parser = new PacketParser(this.recvBuffer);
this.remoteAddress = this.socket.RemoteAddress;
StartRecv();
this.StartRecv();
}
protected virtual void Dispose(bool disposing)
{
if (socket == null)
if (this.socket == null)
{
return;
}
......@@ -42,7 +42,7 @@ namespace TNet
if (disposing)
{
// 释放托管的资源
socket.Dispose();
this.socket.Dispose();
}
// 释放非托管资源
......@@ -57,7 +57,7 @@ namespace TNet
public void Dispose()
{
Dispose(true);
this.Dispose(true);
GC.SuppressFinalize(this);
}
......@@ -84,13 +84,13 @@ namespace TNet
{
var tcs = new TaskCompletionSource<byte[]>();
if (parser.Parse())
if (this.parser.Parse())
{
tcs.SetResult(parser.GetPacket());
tcs.SetResult(this.parser.GetPacket());
}
else
{
this.onParseComplete = () => this.ParseComplete(tcs);
this.onParseComplete = () => this.ParseComplete(tcs);
}
return tcs.Task;
}
......@@ -104,13 +104,13 @@ namespace TNet
{
get
{
return remoteAddress;
return this.remoteAddress;
}
}
private void ParseComplete(TaskCompletionSource<byte[]> tcs)
{
byte[] packet = parser.GetPacket();
byte[] packet = this.parser.GetPacket();
this.onParseComplete = () => { };
tcs.SetResult(packet);
}
......@@ -130,8 +130,8 @@ namespace TNet
{
sendSize = this.sendBuffer.Count;
}
int n = await this.socket.SendAsync(
this.sendBuffer.First, this.sendBuffer.FirstIndex, sendSize);
int n =
await this.socket.SendAsync(this.sendBuffer.First, this.sendBuffer.FirstIndex, sendSize);
this.sendBuffer.FirstIndex += n;
if (this.sendBuffer.FirstIndex == TBuffer.ChunkSize)
{
......@@ -154,8 +154,10 @@ namespace TNet
{
while (true)
{
int n = await this.socket.RecvAsync(
this.recvBuffer.Last, this.recvBuffer.LastIndex, TBuffer.ChunkSize - this.recvBuffer.LastIndex);
int n =
await
this.socket.RecvAsync(this.recvBuffer.Last, this.recvBuffer.LastIndex,
TBuffer.ChunkSize - this.recvBuffer.LastIndex);
if (n == 0)
{
break;
......@@ -169,7 +171,7 @@ namespace TNet
}
// 解析封包
if (parser.Parse())
if (this.parser.Parse())
{
this.onParseComplete();
}
......@@ -181,4 +183,4 @@ namespace TNet
}
}
}
}
}
\ No newline at end of file
......@@ -4,11 +4,11 @@ using System.Collections.Generic;
namespace TNet
{
public class TPoller : IPoller
public class TPoller: IPoller
{
// 线程同步队列,发送接收socket回调都放到该队列,由poll线程统一执行
private readonly BlockingCollection<Action> blockingCollection = new BlockingCollection<Action>();
public void Add(Action action)
{
this.blockingCollection.Add(action);
......@@ -18,27 +18,28 @@ namespace TNet
{
// 处理读写线程的回调
Action action;
if (this.blockingCollection.TryTake(out action, timeout))
if (!this.blockingCollection.TryTake(out action, timeout))
{
return;
}
var queue = new Queue<Action>();
queue.Enqueue(action);
var queue = new Queue<Action>();
queue.Enqueue(action);
while (true)
while (true)
{
if (!this.blockingCollection.TryTake(out action, 0))
{
if (!this.blockingCollection.TryTake(out action, 0))
{
break;
}
queue.Enqueue(action);
break;
}
queue.Enqueue(action);
}
while (queue.Count > 0)
{
Action a = queue.Dequeue();
a();
}
while (queue.Count > 0)
{
Action a = queue.Dequeue();
a();
}
}
}
}
}
\ No newline at end of file
......@@ -10,7 +10,7 @@ namespace TNet
{
private readonly IPoller poller = new TPoller();
private TSocket acceptor;
private readonly Dictionary<string, TChannel> channels = new Dictionary<string, TChannel>();
private readonly TimerManager timerManager = new TimerManager();
......@@ -22,7 +22,7 @@ namespace TNet
/// <param name="port"></param>
public TService(string host, int port)
{
this.acceptor = new TSocket(poller);
this.acceptor = new TSocket(this.poller);
this.acceptor.Bind(host, port);
this.acceptor.Listen(100);
}
......@@ -56,7 +56,7 @@ namespace TNet
public void Dispose()
{
Dispose(true);
this.Dispose(true);
GC.SuppressFinalize(this);
}
......@@ -67,10 +67,10 @@ namespace TNet
private async Task<IChannel> ConnectAsync(string host, int port)
{
TSocket newSocket = new TSocket(poller);
TSocket newSocket = new TSocket(this.poller);
await newSocket.ConnectAsync(host, port);
TChannel channel = new TChannel(newSocket, this);
channels[newSocket.RemoteAddress] = channel;
this.channels[newSocket.RemoteAddress] = channel;
return channel;
}
......@@ -81,9 +81,9 @@ namespace TNet
throw new Exception(string.Format("service construct must use host and port param"));
}
TSocket socket = new TSocket(this.poller);
await acceptor.AcceptAsync(socket);
await this.acceptor.AcceptAsync(socket);
TChannel channel = new TChannel(socket, this);
channels[channel.RemoteAddress] = channel;
this.channels[channel.RemoteAddress] = channel;
return channel;
}
......@@ -105,12 +105,12 @@ namespace TNet
{
return channel;
}
return await ConnectAsync(host, port);
return await this.ConnectAsync(host, port);
}
public void RunOnce(int timeout)
{
poller.Run(timeout);
this.poller.Run(timeout);
}
public void Run()
......@@ -130,4 +130,4 @@ namespace TNet
}
}
}
}
}
\ No newline at end of file
......@@ -33,7 +33,8 @@ namespace TNet
{
get
{
return ((IPEndPoint)socket.RemoteEndPoint).Address + ":" + ((IPEndPoint)socket.RemoteEndPoint).Port;
return ((IPEndPoint) this.socket.RemoteEndPoint).Address + ":" +
((IPEndPoint) this.socket.RemoteEndPoint).Port;
}
}
......@@ -67,7 +68,7 @@ namespace TNet
public void Dispose()
{
Dispose(true);
this.Dispose(true);
GC.SuppressFinalize(this);
}
......@@ -122,7 +123,7 @@ namespace TNet
private static void OnConnectComplete(SocketAsyncEventArgs e)
{
var tcs = (TaskCompletionSource<bool>)e.UserToken;
var tcs = (TaskCompletionSource<bool>) e.UserToken;
e.UserToken = null;
if (e.SocketError != SocketError.Success)
{
......@@ -146,7 +147,7 @@ namespace TNet
private static void OnAcceptComplete(SocketAsyncEventArgs e)
{
var tcs = (TaskCompletionSource<bool>)e.UserToken;
var tcs = (TaskCompletionSource<bool>) e.UserToken;
e.UserToken = null;
if (e.SocketError != SocketError.Success)
{
......@@ -171,7 +172,7 @@ namespace TNet
private static void OnRecvComplete(SocketAsyncEventArgs e)
{
Log.Debug("OnRecvComplete: " + e.BytesTransferred);
var tcs = (TaskCompletionSource<int>)e.UserToken;
var tcs = (TaskCompletionSource<int>) e.UserToken;
e.UserToken = null;
if (e.SocketError != SocketError.Success)
{
......@@ -195,7 +196,7 @@ namespace TNet
private static void OnSendComplete(SocketAsyncEventArgs e)
{
var tcs = (TaskCompletionSource<int>)e.UserToken;
var tcs = (TaskCompletionSource<int>) e.UserToken;
e.UserToken = null;
if (e.SocketError != SocketError.Success)
{
......@@ -218,7 +219,7 @@ namespace TNet
private static void OnDisconnectComplete(SocketAsyncEventArgs e)
{
var tcs = (TaskCompletionSource<bool>)e.UserToken;
var tcs = (TaskCompletionSource<bool>) e.UserToken;
e.UserToken = null;
if (e.SocketError != SocketError.Success)
{
......@@ -228,4 +229,4 @@ namespace TNet
tcs.SetResult(true);
}
}
}
}
\ No newline at end of file
......@@ -21,7 +21,7 @@ namespace TNetTest
byte[] bytes = await channel.RecvAsync();
CollectionAssert.AreEqual("9876543210".ToByteArray(), bytes);
barrier.RemoveParticipant();
this.barrier.RemoveParticipant();
}
private async void ServerEvent(IService service)
......@@ -32,7 +32,7 @@ namespace TNetTest
Array.Reverse(bytes);
channel.SendAsync(bytes);
barrier.RemoveParticipant();
this.barrier.RemoveParticipant();
}
[TestMethod]
......@@ -46,17 +46,15 @@ namespace TNetTest
Task.Factory.StartNew(() => clientService.Run(), TaskCreationOptions.LongRunning);
Task.Factory.StartNew(() => serverService.Run(), TaskCreationOptions.LongRunning);
// 往server host线程增加事件,accept
serverService.Add(() => ServerEvent(serverService));
serverService.Add(() => this.ServerEvent(serverService));
Thread.Sleep(1000);
// 往client host线程增加事件,client线程连接server
clientService.Add(() => ClientEvent(clientService, hostName, port));
clientService.Add(() => this.ClientEvent(clientService, hostName, port));
barrier.SignalAndWait();
this.barrier.SignalAndWait();
}
}
}
\ No newline at end of file
......@@ -79,7 +79,8 @@ namespace UNet
[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_host_flush")]
internal static extern void EnetHostFlush(IntPtr host);
[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_host_check_events")]
[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_host_check_events"
)]
internal static extern int EnetHostCheckEvents(IntPtr host, ENetEvent ev);
[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_host_service")]
......
......@@ -25,7 +25,7 @@ namespace UNet
AcknowledgingDisconnect = 8,
Zombie = 9
}
[StructLayout(LayoutKind.Sequential)]
internal struct ENetAddress
{
......
......@@ -10,39 +10,38 @@ namespace UNet
private USocket socket;
private readonly string remoteAddress;
public UChannel(USocket socket, UService service)
{
this.socket = socket;
this.service = service;
remoteAddress = this.socket.RemoteAddress;
this.remoteAddress = this.socket.RemoteAddress;
}
protected void Dispose(bool disposing)
{
if (socket == null)
if (this.socket == null)
{
return;
}
if (disposing)
{
socket.Dispose();
this.socket.Dispose();
}
service.Remove(this);
this.service.Remove(this);
this.socket = null;
}
~UChannel()
{
Dispose(false);
this.Dispose(false);
}
public void Dispose()
{
Dispose(true);
this.Dispose(true);
GC.SuppressFinalize(this);
}
......@@ -51,7 +50,6 @@ namespace UNet
this.socket.SendAsync(buffer, channelID, flags);
}
public async Task<byte[]> RecvAsync()
{
return await this.socket.RecvAsync();
......@@ -61,7 +59,7 @@ namespace UNet
{
get
{
return remoteAddress;
return this.remoteAddress;
}
}
......@@ -70,4 +68,4 @@ namespace UNet
return await this.socket.DisconnectAsync();
}
}
}
}
\ No newline at end of file
......@@ -4,7 +4,7 @@ using System.Runtime.Serialization;
namespace UNet
{
[Serializable]
internal class UException : Exception
internal class UException: Exception
{
public UException()
{
......
......@@ -3,7 +3,6 @@ using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading.Tasks;
using Common.Base;
using Common.Logger;
namespace UNet
{
......@@ -15,7 +14,9 @@ namespace UNet
}
private readonly USocketManager uSocketManager = new USocketManager();
private readonly QueueDictionary<IntPtr, ENetEvent> connQueue = new QueueDictionary<IntPtr, ENetEvent>();
private readonly QueueDictionary<IntPtr, ENetEvent> connQueue =
new QueueDictionary<IntPtr, ENetEvent>();
private IntPtr host;
......@@ -29,8 +30,8 @@ namespace UNet
{
UAddress address = new UAddress(hostName, port);
ENetAddress nativeAddress = address.Struct;
this.host = NativeMethods.EnetHostCreate(
ref nativeAddress, NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID, 0, 0, 0);
this.host = NativeMethods.EnetHostCreate(ref nativeAddress,
NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID, 0, 0, 0);
if (this.host == IntPtr.Zero)
{
......@@ -42,8 +43,8 @@ namespace UNet
public UPoller()
{
this.host = NativeMethods.EnetHostCreate(
IntPtr.Zero, NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID, 0, 0, 0);
this.host = NativeMethods.EnetHostCreate(IntPtr.Zero, NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID,
0, 0, 0);
if (this.host == IntPtr.Zero)
{
......@@ -82,7 +83,7 @@ namespace UNet
}
var tcs = new TaskCompletionSource<USocket>();
// 如果有请求连接缓存的包,从缓存中取
if (this.connQueue.Count > 0)
{
......@@ -95,8 +96,8 @@ namespace UNet
}
else
{
this.uSocketManager.Add(acceptor.PeerPtr, acceptor);
acceptor.Connected = eEvent =>
this.uSocketManager.Add(this.acceptor.PeerPtr, this.acceptor);
this.acceptor.Connected = eEvent =>
{
if (eEvent.Type == EventType.Disconnect)
{
......@@ -117,9 +118,9 @@ namespace UNet
var tcs = new TaskCompletionSource<USocket>();
UAddress address = new UAddress(hostName, port);
ENetAddress nativeAddress = address.Struct;
IntPtr ptr = NativeMethods.EnetHostConnect(
this.host, ref nativeAddress, NativeMethods.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT, 0);
IntPtr ptr = NativeMethods.EnetHostConnect(this.host, ref nativeAddress,
NativeMethods.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT, 0);
USocket socket = new USocket(ptr);
if (socket.PeerPtr == IntPtr.Zero)
{
......@@ -159,7 +160,7 @@ namespace UNet
public void Add(Action action)
{
blockingCollection.Add(action);
this.blockingCollection.Add(action);
}
private void OnEvents(int timeout)
......
......@@ -18,7 +18,7 @@ namespace UNet
/// <param name="port"></param>
public UService(string host, int port)
{
this.poller = new UPoller(host, (ushort)port);
this.poller = new UPoller(host, (ushort) port);
}
/// <summary>
......@@ -38,19 +38,19 @@ namespace UNet
if (disposing)
{
this.poller.Dispose();
this.poller.Dispose();
}
this.poller = null;
}
~UService()
{
Dispose(false);
this.Dispose(false);
}
public void Dispose()
{
Dispose(true);
this.Dispose(true);
GC.SuppressFinalize(this);
}
......@@ -61,9 +61,9 @@ namespace UNet
private async Task<IChannel> ConnectAsync(string host, int port)
{
USocket newSocket = await this.poller.ConnectAsync(host, (ushort)port);
USocket newSocket = await this.poller.ConnectAsync(host, (ushort) port);
UChannel channel = new UChannel(newSocket, this);
channels[channel.RemoteAddress] = channel;
this.channels[channel.RemoteAddress] = channel;
return channel;
}
......@@ -74,14 +74,14 @@ namespace UNet
{
return channel;
}
return await ConnectAsync(host, port);
return await this.ConnectAsync(host, port);
}
public async Task<IChannel> GetChannel()
{
USocket socket = await this.poller.AcceptAsync();
UChannel channel = new UChannel(socket, this);
channels[channel.RemoteAddress] = channel;
this.channels[channel.RemoteAddress] = channel;
return channel;
}
......@@ -105,4 +105,4 @@ namespace UNet
this.poller.Run();
}
}
}
}
\ No newline at end of file
......@@ -2,12 +2,11 @@
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Common.Logger;
using Network;
namespace UNet
{
internal sealed class USocket : IDisposable
internal sealed class USocket: IDisposable
{
private IntPtr peerPtr;
private readonly Queue<byte[]> recvQueue = new Queue<byte[]>();
......@@ -35,12 +34,12 @@ namespace UNet
~USocket()
{
Dispose(false);
this.Dispose(false);
}
public void Dispose()
{
Dispose(true);
this.Dispose(true);
GC.SuppressFinalize(this);
}
......@@ -51,7 +50,7 @@ namespace UNet
return this.peerPtr;
}
}
private ENetPeer Struct
{
get
......
......@@ -2,10 +2,9 @@
using System.Threading;
using System.Threading.Tasks;
using Common.Helper;
using Common.Logger;
using UNet;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Network;
using UNet;
namespace UNetTest
{
......@@ -22,7 +21,7 @@ namespace UNetTest
byte[] bytes = await channel.RecvAsync();
CollectionAssert.AreEqual("9876543210".ToByteArray(), bytes);
barrier.RemoveParticipant();
this.barrier.RemoveParticipant();
}
private async void ServerEvent(IService service)
......@@ -32,8 +31,8 @@ namespace UNetTest
CollectionAssert.AreEqual("0123456789".ToByteArray(), bytes);
Array.Reverse(bytes);
channel.SendAsync(bytes);
barrier.RemoveParticipant();
this.barrier.RemoveParticipant();
}
[TestMethod]
......@@ -47,17 +46,15 @@ namespace UNetTest
Task.Factory.StartNew(() => clientService.Run(), TaskCreationOptions.LongRunning);
Task.Factory.StartNew(() => serverService.Run(), TaskCreationOptions.LongRunning);
// 往server host线程增加事件,accept
serverService.Add(() => ServerEvent(serverService));
serverService.Add(() => this.ServerEvent(serverService));
Thread.Sleep(1000);
// 往client host线程增加事件,client线程连接server
clientService.Add(() => ClientEvent(clientService, hostName, port));
clientService.Add(() => this.ClientEvent(clientService, hostName, port));
barrier.SignalAndWait();
this.barrier.SignalAndWait();
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册