为fastCSharp的TCP服务写了一个简单的demo。源码详见demo.chatServer与demo.chatClient项目,服务器端代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
using
System;
using
System.Collections.Generic;
using
System.Threading;
using
fastCSharp.setup.cSharp;
using
fastCSharp.threading;
namespace
fastCSharp.demo.chatServer
{
/// <summary>
/// 服务端
/// </summary>
[fastCSharp.setup.cSharp.tcpServer(IsIdentityCommand =
true
, IsAsynchronous =
true
, Host =
"127.0.0.1"
, Port = 12345)]
public
partial
class
server
{
/// <summary>
/// 命令枚举
/// </summary>
private
enum
command
{
/// <summary>
/// 登陆
/// </summary>
Login,
/// <summary>
/// 退出
/// </summary>
Logout,
/// <summary>
/// 获取用户列表
/// </summary>
GetUsers,
/// <summary>
/// 发送消息
/// </summary>
Send,
/// <summary>
/// 接收消息
/// </summary>
Receive,
}
/// <summary>
/// 用户信息
/// </summary>
private
sealed
class
userInfo
{
/// <summary>
/// 用户名
/// </summary>
public
string
User;
/// <summary>
/// 用户版本
/// </summary>
public
int
UserVersion =
int
.MinValue;
/// <summary>
/// 获取用户列表委托
/// </summary>
public
action<asynchronousMethod.returnValue<usersVerison>> OnUserChanged;
/// <summary>
/// 用户消息集合
/// </summary>
public
list<message> Messages =
new
list<message>();
/// <summary>
/// 获取消息委托
/// </summary>
public
action<asynchronousMethod.returnValue<message[]>> OnMessage;
}
/// <summary>
/// 用户集合
/// </summary>
private
Dictionary<
string
, userInfo> users =
new
Dictionary<
string
, userInfo>();
/// <summary>
/// 用户集合访问锁
/// </summary>
private
int
userLock;
/// <summary>
/// 用户版本
/// </summary>
private
int
userVersion;
/// <summary>
/// 用户登陆
/// </summary>
public
event
action<
string
> OnLogin;
/// <summary>
/// 用户登陆
/// </summary>
/// <param name="client">客户端标识</param>
/// <param name="user"></param>
/// <returns></returns>
[fastCSharp.setup.cSharp.tcpServer(CommandIentity = (
int
)command.Login)]
private
bool
login(tcpBase.client client,
string
user)
{
if
(user.length() != 0)
{
if
(userChangeHandle ==
null
) userChangeHandle = userChange;
interlocked.CompareSetSleep0(
ref
userLock);
try
{
if
(!users.ContainsKey(user))
{
users.Add(user,
new
userInfo { User = user });
client.UserInfo = user;
++userVersion;
task.TinyTask.Add(userChangeHandle, user);
return
true
;
}
}
finally
{ userLock = 0; }
}
return
false
;
}
/// <summary>
/// 用户退出
/// </summary>
public
event
action<
string
> OnLogout;
/// <summary>
/// 退出
/// </summary>
/// <param name="client"></param>
[fastCSharp.setup.cSharp.tcpServer(CommandIentity = (
int
)command.Logout)]
private
void
logout(tcpBase.client client)
{
string
user = (
string
)client.UserInfo;
userInfo userInfo;
interlocked.CompareSetSleep0(
ref
userLock);
try
{
if
(users.TryGetValue(user,
out
userInfo)) users.Remove(user);
++userVersion;
}
finally
{ userLock = 0; }
if
(userInfo !=
null
)
{
if
(userChangeHandle ==
null
) userChangeHandle = userChange;
task.TinyTask.Add(userChangeHandle,
null
);
if
(userInfo.OnUserChanged !=
null
) userInfo.OnUserChanged(
new
usersVerison { UserVersion =
int
.MinValue });
if
(userInfo.OnMessage !=
null
) userInfo.OnMessage(userInfo.Messages.toArray());
if
(OnLogout !=
null
) task.TinyTask.Add(OnLogout, user);
}
}
/// <summary>
/// 用户列表与版本信息
/// </summary>
[fastCSharp.setup.cSharp.serialize(IsBaseSerialize =
true
)]
public
partial
struct
usersVerison
{
/// <summary>
/// 用户列表
/// </summary>
public
string
[] Users;
/// <summary>
/// 用户列表版本
/// </summary>
public
int
UserVersion;
}
/// <summary>
/// 获取用户列表
/// </summary>
/// <param name="client"></param>
/// <param name="userVersion"></param>
/// <param name="onUserChanged"></param>
[fastCSharp.setup.cSharp.tcpServer(CommandIentity = (
int
)command.GetUsers, IsAsynchronousCallback =
true
, IsClientAsynchronous =
true
, IsClientSynchronous =
false
)]
private
void
getUsers(tcpBase.client client,
int
userVersion, action<asynchronousMethod.returnValue<usersVerison>> onUserChanged)
{
usersVerison usersVerison =
new
usersVerison();
action<asynchronousMethod.returnValue<usersVerison>> oldOnUserChanged =
null
;
interlocked.CompareSetSleep0(
ref
userLock);
try
{
userInfo userInfo;
if
(users.TryGetValue((
string
)client.UserInfo,
out
userInfo))
{
if
(
this
.userVersion == userVersion)
{
oldOnUserChanged = userInfo.OnUserChanged;
userInfo.OnUserChanged = onUserChanged;
userInfo.UserVersion = userVersion;
onUserChanged =
null
;
}
else
{
usersVerison.Users = users.Keys.getArray();
usersVerison.UserVersion =
this
.userVersion;
}
}
else
{
oldOnUserChanged = onUserChanged;
onUserChanged =
null
;
}
}
finally
{
userLock = 0;
if
(onUserChanged !=
null
) onUserChanged(usersVerison);
if
(oldOnUserChanged !=
null
) oldOnUserChanged(
new
usersVerison { UserVersion =
int
.MinValue });
}
}
/// <summary>
/// 获取用户列表委托集合
/// </summary>
private
list<action<asynchronousMethod.returnValue<usersVerison>>> onUserChangeds =
new
list<action<asynchronousMethod.returnValue<usersVerison>>>();
/// <summary>
/// 获取用户列表委托集合访问锁
/// </summary>
private
int
onUserChangedLock;
/// <summary>
/// 用户列表更新
/// </summary>
private
action<
string
> userChangeHandle;
/// <summary>
/// 用户列表更新
/// </summary>
/// <param name="user">新增用户名</param>
private
void
userChange(
string
user)
{
usersVerison usersVerison =
new
usersVerison { UserVersion =
int
.MinValue };
while
(userVersion != usersVerison.UserVersion && Interlocked.CompareExchange(
ref
onUserChangedLock, 1, 0) == 0)
{
try
{
interlocked.CompareSetSleep0(
ref
userLock);
try
{
usersVerison.Users = users.Keys.getArray();
usersVerison.UserVersion = userVersion;
foreach
(userInfo userInfo
in
users.Values)
{
if
(userInfo.UserVersion != userVersion && userInfo.OnUserChanged !=
null
)
{
onUserChangeds.Add(userInfo.OnUserChanged);
userInfo.OnUserChanged =
null
;
}
}
}
finally
{ userLock = 0; }
while
(userVersion == usersVerison.UserVersion && onUserChangeds.Count != 0)
{
try
{
onUserChangeds.Pop()(usersVerison);
}
catch
{ }
}
}
finally
{ onUserChangedLock = 0; }
}
if
(user !=
null
&& OnLogin !=
null
) OnLogin(user);
}
/// <summary>
/// 消息
/// </summary>
[fastCSharp.setup.cSharp.serialize(IsBaseSerialize =
true
)]
public
partial
struct
message
{
/// <summary>
/// 发送者
/// </summary>
public
string
User;
/// <summary>
/// 发送时间
/// </summary>
public
DateTime Time;
/// <summary>
/// 发送内容
/// </summary>
public
string
Message;
}
/// <summary>
/// 发送消息
/// </summary>
public
event
action<message> OnMessage;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
/// <summary>
/// 发送消息
/// </summary>
/// <param name="client"></param>
/// <param name="message"></param>
/// <param name="users">接收用户列表,null表示向所有用户发送消息</param>
[fastCSharp.setup.cSharp.tcpServer(CommandIentity = (
int
)command.Send)]
private
void
send(tcpBase.client client,
string
message,
string
[] users)
{
if
(newMessageHandle ==
null
) newMessageHandle = newMessage;
string
user = (
string
)client.UserInfo;
message messageInfo =
new
message { User = user, Time = date.NowTime, Message = message };
bool
isSend =
false
;
interlocked.CompareSetSleep0(
ref
userLock);
try
{
if
(
this
.users.ContainsKey(user))
{
if
(users ==
null
)
{
foreach
(userInfo userInfo
in
this
.users.Values)
{
if
(userInfo.User != user)
{
userInfo.Messages.Add(messageInfo);
if
(userInfo.OnMessage !=
null
) isSend =
true
;
}
}
}
else
{
userInfo userInfo;
foreach
(
string
receiveUser
in
users)
{
if
(receiveUser != user &&
this
.users.TryGetValue(receiveUser,
out
userInfo))
{
userInfo.Messages.Add(messageInfo);
if
(userInfo.OnMessage !=
null
) isSend =
true
;
}
}
}
if
(isSend) ++messageVersion;
}
}
finally
{ userLock = 0; }
if
(isSend) task.TinyTask.Add(newMessageHandle, messageInfo);
}
/// <summary>
/// 获取消息委托集合
/// </summary>
private
list<keyValue<action<asynchronousMethod.returnValue<message[]>>, message[]>> onMessages =
new
list<keyValue<action<asynchronousMethod.returnValue<message[]>>, message[]>>();
/// <summary>
/// 消息版本
/// </summary>
private
int
messageVersion;
/// <summary>
/// 消息消息委托集合访问锁
/// </summary>
private
int
messageLock;
/// <summary>
/// 消息更新
/// </summary>
private
action<message> newMessageHandle;
/// <summary>
/// 消息更新
/// </summary>
/// <param name="message">发送的消息</param>
private
void
newMessage(message message)
{
int
version =
int
.MinValue;
while
(version != messageVersion && Interlocked.CompareExchange(
ref
messageLock, 1, 0) == 0)
{
version = messageVersion;
try
{
interlocked.CompareSetSleep0(
ref
userLock);
try
{
foreach
(userInfo userInfo
in
users.Values)
{
if
(userInfo.OnMessage !=
null
&& userInfo.Messages.Count != 0)
{
onMessages.Add(
new
keyValue<action<asynchronousMethod.returnValue<message[]>>, message[]>(userInfo.OnMessage, userInfo.Messages.getArray()));
userInfo.Messages.Empty();
userInfo.OnMessage =
null
;
}
}
}
finally
{ userLock = 0; }
while
(onMessages.Count != 0)
{
keyValue<action<asynchronousMethod.returnValue<message[]>>, message[]> onMessage = onMessages.Pop();
try
{
onMessage.Key(onMessage.Value);
}
catch
{ }
}
}
finally
{ messageLock = 0; }
}
if
(OnMessage !=
null
) OnMessage(message);
}
/// <summary>
/// 获取消息
/// </summary>
/// <param name="client"></param>
/// <param name="onMessage"></param>
[fastCSharp.setup.cSharp.tcpServer(CommandIentity = (
int
)command.Receive, IsAsynchronousCallback =
true
, IsClientAsynchronous =
true
, IsClientSynchronous =
false
)]
private
void
receive(tcpBase.client client, action<asynchronousMethod.returnValue<message[]>> onMessage)
{
action<asynchronousMethod.returnValue<message[]>> oldOnMessage =
null
;
message[] messages =
null
;
interlocked.CompareSetSleep0(
ref
userLock);
try
{
userInfo userInfo;
if
(users.TryGetValue((
string
)client.UserInfo,
out
userInfo))
{
if
(userInfo.Messages.Count == 0)
{
oldOnMessage = userInfo.OnMessage;
userInfo.OnMessage = onMessage;
onMessage =
null
;
}
else
{
messages = userInfo.Messages.getArray();
userInfo.Messages.Empty();
}
}
else
{
oldOnMessage = onMessage;
onMessage =
null
;
}
}
finally
{
userLock = 0;
if
(onMessage !=
null
) onMessage(messages);
if
(oldOnMessage !=
null
) oldOnMessage(
null
);
}
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
using
System;
using
System.Threading;
using
fastCSharp.setup.cSharp;
namespace
fastCSharp.demo.chatServer
{
/// <summary>
/// 客户端
/// </summary>
public
class
client : IDisposable
{
/// <summary>
/// 客户端
/// </summary>
private
tcpClient.server tcpClient;
/// <summary>
/// 用户名
/// </summary>
public
string
User {
get
;
private
set
; }
/// <summary>
/// 是否已经释放资源
/// </summary>
private
int
isDisposed;
/// <summary>
/// 是否已经释放资源
/// </summary>
public
bool
IsDisposed
{
get
{
return
isDisposed != 0; }
}
/// <summary>
/// 用户列表与版本信息
/// </summary>
private
server.usersVerison userVerison;
/// <summary>
/// 用户列表
/// </summary>
public
string
[] Users
{
get
{
return
userVerison.Users; }
}
/// <summary>
/// 用户列表更新
/// </summary>
public
event
action<
string
[]> OnUserChanged;
/// <summary>
/// 消息更新
/// </summary>
public
event
action<server.message[]> OnMessage;
/// <summary>
/// 释放资源
/// </summary>
public
event
action OnDisposed;
/// <summary>
/// 客户端
/// </summary>
/// <param name="user">用户名</param>
public
client(
string
user)
{
tcpClient =
new
tcpClient.server();
if
(tcpClient.login(user))
{
User = user;
tcpClient.getUsers(userChangeHandle = userChange, userVerison.UserVersion =
int
.MinValue);
tcpClient.receive(receiveHandle = receive);
}
else
Dispose();
}
/// <summary>
/// 用户列表更新
/// </summary>
private
action<asynchronousMethod.returnValue<server.usersVerison>> userChangeHandle;
/// <summary>
/// 用户列表更新
/// </summary>
/// <param name="usersVerison">用户列表与版本信息</param>
private
void
userChange(asynchronousMethod.returnValue<server.usersVerison> usersVerison)
{
if
(usersVerison.IsReturn)
{
if
(usersVerison.Value.UserVersion !=
int
.MinValue)
{
userVerison = usersVerison.Value;
tcpClient.getUsers(userChangeHandle, userVerison.UserVersion);
if
(OnUserChanged !=
null
) OnUserChanged(userVerison.Users.removeFirst(User));
}
}
else
Dispose();
}
/// <summary>
/// 消息更新
/// </summary>
private
action<asynchronousMethod.returnValue<server.message[]>> receiveHandle;
/// <summary>
/// 消息更新
/// </summary>
/// <param name="messages">消息列表</param>
private
void
receive(asynchronousMethod.returnValue<server.message[]> messages)
{
if
(messages.IsReturn)
{
if
(messages.Value !=
null
)
{
tcpClient.receive(receiveHandle);
if
(OnMessage !=
null
) OnMessage(messages.Value);
}
}
else
Dispose();
}
/// <summary>
/// 发送消息
/// </summary>
/// <param name="message">消息内容</param>
/// <param name="users">接收用户列表,null表示所有用户</param>
public
void
Send(
string
message,
string
[] users =
null
)
{
tcpClient.send(message, users);
}
/// <summary>
/// 释放资源
/// </summary>
public
void
Dispose()
{
if
(Interlocked.Increment(
ref
isDisposed) == 1)
{
using
(tcpClient)
{
if
(User !=
null
) tcpClient.logout();
}
tcpClient =
null
;
if
(OnDisposed !=
null
) OnDisposed();
}
}
}
}
|
http://j.renren.com/album/1404130002213714472