Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
guo450212436
et
提交
506accd7
E
et
项目概览
guo450212436
/
et
与 Fork 源项目一致
从无法访问的项目Fork
通知
3
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
E
et
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
506accd7
编写于
12月 31, 2014
作者:
T
tanghai
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
TService测试OK
上级
4f37269f
变更
13
隐藏空白更改
内联
并排
Showing
13 changed file
with
129 addition
and
36 deletion
+129
-36
CSharp/Platform/Network/IService.cs
CSharp/Platform/Network/IService.cs
+3
-1
CSharp/Platform/TNet/TBuffer.cs
CSharp/Platform/TNet/TBuffer.cs
+1
-1
CSharp/Platform/TNet/TChannel.cs
CSharp/Platform/TNet/TChannel.cs
+3
-2
CSharp/Platform/TNet/TPoller.cs
CSharp/Platform/TNet/TPoller.cs
+1
-5
CSharp/Platform/TNet/TService.cs
CSharp/Platform/TNet/TService.cs
+27
-18
CSharp/Platform/TNet/TSocket.cs
CSharp/Platform/TNet/TSocket.cs
+2
-0
CSharp/Platform/TNetTest/TNetTest.csproj
CSharp/Platform/TNetTest/TNetTest.csproj
+5
-0
CSharp/Platform/TNetTest/TServiceTest.cs
CSharp/Platform/TNetTest/TServiceTest.cs
+62
-0
CSharp/Platform/UNet/EService.cs
CSharp/Platform/UNet/EService.cs
+1
-1
CSharp/Platform/UNet/UService.cs
CSharp/Platform/UNet/UService.cs
+12
-2
CSharp/Platform/UNet/USocket.cs
CSharp/Platform/UNet/USocket.cs
+1
-1
CSharp/Platform/UNetTest/UNetTest.csproj
CSharp/Platform/UNetTest/UNetTest.csproj
+4
-0
CSharp/Platform/UNetTest/UServiceTest.cs
CSharp/Platform/UNetTest/UServiceTest.cs
+7
-5
未找到文件。
CSharp/Platform/Network/IService.cs
浏览文件 @
506accd7
...
...
@@ -19,6 +19,8 @@ namespace Network
void
Remove
(
IChannel
channel
);
void
Start
();
void
RunOnce
(
int
timeout
);
void
Run
();
}
}
CSharp/Platform/TNet/TBuffer.cs
浏览文件 @
506accd7
...
...
@@ -3,7 +3,7 @@ using System.Collections.Generic;
namespace
TNet
{
internal
class
TBuffer
public
class
TBuffer
{
public
const
int
ChunkSize
=
8096
;
...
...
CSharp/Platform/TNet/TChannel.cs
浏览文件 @
506accd7
...
...
@@ -26,7 +26,6 @@ namespace TNet
this
.
socket
=
socket
;
this
.
service
=
service
;
this
.
parser
=
new
PacketParser
(
recvBuffer
);
Start
();
}
public
void
Dispose
()
...
...
@@ -42,6 +41,8 @@ namespace TNet
public
void
SendAsync
(
byte
[]
buffer
,
byte
channelID
=
0
,
PacketFlags
flags
=
PacketFlags
.
Reliable
)
{
byte
[]
size
=
BitConverter
.
GetBytes
(
buffer
.
Length
);
this
.
sendBuffer
.
SendTo
(
size
);
this
.
sendBuffer
.
SendTo
(
buffer
);
if
(
this
.
sendTimer
==
ObjectId
.
Empty
)
{
...
...
@@ -125,7 +126,7 @@ namespace TNet
tcs
.
SetResult
(
packet
);
}
p
rivate
async
void
Start
()
p
ublic
async
void
Start
()
{
try
{
...
...
CSharp/Platform/TNet/TPoller.cs
浏览文件 @
506accd7
...
...
@@ -4,7 +4,7 @@ using System.Collections.Generic;
namespace
TNet
{
internal
class
TPoller
:
IPoller
public
class
TPoller
:
IPoller
{
// 线程同步队列,发送接收socket回调都放到该队列,由poll线程统一执行
private
readonly
BlockingCollection
<
Action
>
blockingCollection
=
new
BlockingCollection
<
Action
>();
...
...
@@ -14,10 +14,6 @@ namespace TNet
this
.
blockingCollection
.
Add
(
action
);
}
public
void
Dispose
()
{
}
public
void
Run
(
int
timeout
)
{
// 处理读写线程的回调
...
...
CSharp/Platform/TNet/TService.cs
浏览文件 @
506accd7
...
...
@@ -8,13 +8,18 @@ namespace TNet
{
public
class
TService
:
IService
{
private
IPoller
poller
=
new
TPoller
();
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
();
/// <summary>
/// 用作server端的构造函数
/// </summary>
/// <param name="host"></param>
/// <param name="port"></param>
public
TService
(
string
host
,
int
port
)
{
this
.
acceptor
=
new
TSocket
(
poller
);
...
...
@@ -22,16 +27,22 @@ namespace TNet
this
.
acceptor
.
Listen
(
100
);
}
/// <summary>
/// 用作client端的构造函数
/// </summary>
public
TService
()
{
}
public
void
Dispose
()
{
if
(
this
.
polle
r
==
null
)
if
(
this
.
accepto
r
==
null
)
{
return
;
}
this
.
acceptor
.
Dispose
();
this
.
acceptor
=
null
;
this
.
poller
=
null
;
}
public
void
Add
(
Action
action
)
...
...
@@ -39,32 +50,27 @@ namespace TNet
this
.
poller
.
Add
(
action
);
}
private
async
void
AcceptAsync
()
{
while
(
true
)
{
TSocket
newSocket
=
new
TSocket
(
poller
);
await
this
.
acceptor
.
AcceptAsync
(
newSocket
);
TChannel
channel
=
new
TChannel
(
newSocket
,
this
);
channels
[
newSocket
.
RemoteAddress
]
=
channel
;
}
}
private
async
Task
<
IChannel
>
ConnectAsync
(
string
host
,
int
port
)
{
TSocket
newSocket
=
new
TSocket
(
poller
);
await
newSocket
.
ConnectAsync
(
host
,
port
);
TChannel
channel
=
new
TChannel
(
newSocket
,
this
);
channels
[
newSocket
.
RemoteAddress
]
=
channel
;
channel
.
Start
();
return
channel
;
}
public
async
Task
<
IChannel
>
GetChannel
()
{
if
(
this
.
acceptor
==
null
)
{
throw
new
Exception
(
string
.
Format
(
"service construct must use host and port param"
));
}
TSocket
socket
=
new
TSocket
(
this
.
poller
);
await
acceptor
.
AcceptAsync
(
socket
);
TChannel
channel
=
new
TChannel
(
socket
,
this
);
channels
[
channel
.
RemoteAddress
]
=
channel
;
channel
.
Start
();
return
channel
;
}
...
...
@@ -96,13 +102,16 @@ namespace TNet
return
await
GetChannel
(
ss
[
0
],
port
);
}
public
void
Start
(
)
public
void
RunOnce
(
int
timeout
)
{
AcceptAsync
();
poller
.
Run
(
timeout
);
}
public
void
Run
()
{
while
(
true
)
{
poller
.
Run
(
1
);
this
.
RunOnce
(
1
);
this
.
timerManager
.
Refresh
();
}
}
...
...
CSharp/Platform/TNet/TSocket.cs
浏览文件 @
506accd7
...
...
@@ -2,6 +2,7 @@
using
System.Net
;
using
System.Net.Sockets
;
using
System.Threading.Tasks
;
using
Common.Logger
;
namespace
TNet
{
...
...
@@ -153,6 +154,7 @@ namespace TNet
private
static
void
OnRecvComplete
(
SocketAsyncEventArgs
e
)
{
Log
.
Debug
(
"OnRecvComplete: "
+
e
.
BytesTransferred
);
var
tcs
=
(
TaskCompletionSource
<
int
>)
e
.
UserToken
;
e
.
UserToken
=
null
;
if
(
e
.
SocketError
!=
SocketError
.
Success
)
...
...
CSharp/Platform/TNetTest/TNetTest.csproj
浏览文件 @
506accd7
...
...
@@ -53,12 +53,17 @@
<ItemGroup>
<Compile
Include=
"Properties\AssemblyInfo.cs"
/>
<Compile
Include=
"TcpListenerTest.cs"
/>
<Compile
Include=
"TServiceTest.cs"
/>
</ItemGroup>
<ItemGroup>
<ProjectReference
Include=
"..\Common\Common.csproj"
>
<Project>
{19f8f043-1f99-4550-99df-dea5c7d77e55}
</Project>
<Name>
Common
</Name>
</ProjectReference>
<ProjectReference
Include=
"..\Network\Network.csproj"
>
<Project>
{3bd499ff-3c34-4920-8b21-c55fba580843}
</Project>
<Name>
Network
</Name>
</ProjectReference>
<ProjectReference
Include=
"..\TNet\TNet.csproj"
>
<Project>
{b42d431a-3a54-4649-942a-c5356d7f9fbc}
</Project>
<Name>
TNet
</Name>
...
...
CSharp/Platform/TNetTest/TServiceTest.cs
0 → 100644
浏览文件 @
506accd7
using
System
;
using
System.Threading
;
using
System.Threading.Tasks
;
using
Common.Helper
;
using
Microsoft.VisualStudio.TestTools.UnitTesting
;
using
Network
;
using
TNet
;
namespace
TServiceTest
{
[
TestClass
]
public
class
TServiceTest
{
private
readonly
Barrier
barrier
=
new
Barrier
(
3
);
private
async
void
ClientEvent
(
IService
service
,
string
hostName
,
ushort
port
)
{
IChannel
channel
=
await
service
.
GetChannel
(
hostName
,
port
);
channel
.
SendAsync
(
"0123456789"
.
ToByteArray
());
byte
[]
bytes
=
await
channel
.
RecvAsync
();
CollectionAssert
.
AreEqual
(
"9876543210"
.
ToByteArray
(),
bytes
);
barrier
.
RemoveParticipant
();
}
private
async
void
ServerEvent
(
IService
service
)
{
IChannel
channel
=
await
service
.
GetChannel
();
byte
[]
bytes
=
await
channel
.
RecvAsync
();
CollectionAssert
.
AreEqual
(
"0123456789"
.
ToByteArray
(),
bytes
);
Array
.
Reverse
(
bytes
);
channel
.
SendAsync
(
bytes
);
barrier
.
RemoveParticipant
();
}
[
TestMethod
]
public
void
ClientSendToServer
()
{
const
string
hostName
=
"127.0.0.1"
;
const
ushort
port
=
8889
;
IService
clientService
=
new
TService
();
IService
serverService
=
new
TService
(
hostName
,
8889
);
Task
.
Factory
.
StartNew
(()
=>
clientService
.
Run
(),
TaskCreationOptions
.
LongRunning
);
Task
.
Factory
.
StartNew
(()
=>
serverService
.
Run
(),
TaskCreationOptions
.
LongRunning
);
// 往server host线程增加事件,accept
serverService
.
Add
(()
=>
ServerEvent
(
serverService
));
Thread
.
Sleep
(
1000
);
// 往client host线程增加事件,client线程连接server
clientService
.
Add
(()
=>
ClientEvent
(
clientService
,
hostName
,
port
));
barrier
.
SignalAndWait
();
}
}
}
\ No newline at end of file
CSharp/Platform/UNet/EService.cs
浏览文件 @
506accd7
...
...
@@ -270,7 +270,7 @@ namespace UNet
}
}
public
void
Start
(
int
timeout
=
0
)
public
void
Run
(
int
timeout
=
0
)
{
while
(
this
.
isRunning
)
{
...
...
CSharp/Platform/UNet/UService.cs
浏览文件 @
506accd7
...
...
@@ -16,6 +16,11 @@ namespace UNet
this
.
service
=
new
EService
(
host
,
(
ushort
)
port
);
}
public
UService
()
{
this
.
service
=
new
EService
();
}
public
void
Dispose
()
{
if
(
service
==
null
)
...
...
@@ -84,9 +89,14 @@ namespace UNet
return
await
ConnectAsync
(
host
,
port
);
}
public
void
Start
()
public
void
RunOnce
(
int
timeout
)
{
this
.
service
.
RunOnce
(
timeout
);
}
public
void
Run
()
{
this
.
service
.
Start
();
this
.
service
.
Run
();
}
}
}
CSharp/Platform/UNet/USocket.cs
浏览文件 @
506accd7
...
...
@@ -102,7 +102,7 @@ namespace UNet
}
var
tcs
=
new
TaskCompletionSource
<
bool
>();
var
address
=
new
Address
{
HostName
=
hostName
,
Port
=
port
};
Address
address
=
new
Address
{
HostName
=
hostName
,
Port
=
port
};
ENetAddress
nativeAddress
=
address
.
Struct
;
this
.
peerPtr
=
NativeMethods
.
EnetHostConnect
(
this
.
service
.
HostPtr
,
ref
nativeAddress
,
channelLimit
,
data
);
...
...
CSharp/Platform/UNetTest/UNetTest.csproj
浏览文件 @
506accd7
...
...
@@ -65,6 +65,10 @@
<Project>
{3bd499ff-3c34-4920-8b21-c55fba580843}
</Project>
<Name>
Network
</Name>
</ProjectReference>
<ProjectReference
Include=
"..\TNet\TNet.csproj"
>
<Project>
{b42d431a-3a54-4649-942a-c5356d7f9fbc}
</Project>
<Name>
TNet
</Name>
</ProjectReference>
<ProjectReference
Include=
"..\UNet\UNet.csproj"
>
<Project>
{d0b4cfac-a368-4742-9863-68776cfa9938}
</Project>
<Name>
UNet
</Name>
...
...
CSharp/Platform/UNetTest/UServiceTest.cs
浏览文件 @
506accd7
...
...
@@ -2,9 +2,11 @@
using
System.Threading
;
using
System.Threading.Tasks
;
using
Common.Helper
;
using
Common.Logger
;
using
UNet
;
using
Microsoft.VisualStudio.TestTools.UnitTesting
;
using
Network
;
using
TNet
;
namespace
UNetTest
{
...
...
@@ -19,7 +21,7 @@ namespace UNetTest
channel
.
SendAsync
(
"0123456789"
.
ToByteArray
());
byte
[]
bytes
=
await
channel
.
RecvAsync
();
Assert
.
AreEqual
(
"9876543210"
.
ToByteArray
(),
bytes
);
Collection
Assert
.
AreEqual
(
"9876543210"
.
ToByteArray
(),
bytes
);
barrier
.
RemoveParticipant
();
}
...
...
@@ -28,7 +30,7 @@ namespace UNetTest
{
IChannel
channel
=
await
service
.
GetChannel
();
byte
[]
bytes
=
await
channel
.
RecvAsync
();
Assert
.
AreEqual
(
"0123456789"
.
ToByteArray
(),
bytes
);
Collection
Assert
.
AreEqual
(
"0123456789"
.
ToByteArray
(),
bytes
);
Array
.
Reverse
(
bytes
);
channel
.
SendAsync
(
bytes
);
...
...
@@ -40,11 +42,11 @@ namespace UNetTest
{
const
string
hostName
=
"127.0.0.1"
;
const
ushort
port
=
8889
;
IService
clientService
=
new
UService
(
hostName
,
8888
);
IService
clientService
=
new
UService
();
IService
serverService
=
new
UService
(
hostName
,
8889
);
Task
.
Factory
.
StartNew
(()
=>
clientService
.
Start
(),
TaskCreationOptions
.
LongRunning
);
Task
.
Factory
.
StartNew
(()
=>
serverService
.
Start
(),
TaskCreationOptions
.
LongRunning
);
Task
.
Factory
.
StartNew
(()
=>
clientService
.
Run
(),
TaskCreationOptions
.
LongRunning
);
Task
.
Factory
.
StartNew
(()
=>
serverService
.
Run
(),
TaskCreationOptions
.
LongRunning
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录