MARK 群聊程序代码

fastCSharp的TCP服务写了一个简单的demo。源码详见demo.chatServer与demo.chatClient项目,服务器端代码

C# code
?
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;
继续服务器端代码
C# code
?
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 );
             }
         }
 
     }
}
下面是客户端代码
C# code
?
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


深度学习是机器学习的一个子领域,它基于人工神经网络的研究,特别是利用多层次的神经网络来进行学习和模式识别。深度学习模型能够学习数据的高层次特征,这些特征对于图像和语音识别、自然语言处理、医学图像分析等应用至关重要。以下是深度学习的一些关键概念和组成部分: 1. **神经网络(Neural Networks)**:深度学习的基础是人工神经网络,它是由多个层组成的网络结构,包括输入层、隐藏层和输出层。每个层由多个神经元组成,神经元之间通过权重连接。 2. **前馈神经网络(Feedforward Neural Networks)**:这是最常见的神经网络类型,信息从输入层流向隐藏层,最终到达输出层。 3. **卷积神经网络(Convolutional Neural Networks, CNNs)**:这种网络特别适合处理具有网格结构的数据,如图像。它们使用卷积层来提取图像的特征。 4. **循环神经网络(Recurrent Neural Networks, RNNs)**:这种网络能够处理序列数据,如时间序列或自然语言,因为它们具有记忆功能,能够捕捉数据中的时间依赖性。 5. **长短期记忆网络(Long Short-Term Memory, LSTM)**:LSTM 是一种特殊的 RNN,它能够学习长期依赖关系,非常适合复杂的序列预测任务。 6. **生成对抗网络(Generative Adversarial Networks, GANs)**:由两个网络组成,一个生成器和一个判别器,它们相互竞争,生成器生成数据,判别器评估数据的真实性。 7. **深度学习框架**:如 TensorFlow、Keras、PyTorch 等,这些框架提供了构建、训练和部署深度学习模型的工具和库。 8. **激活函数(Activation Functions)**:如 ReLU、Sigmoid、Tanh 等,它们在神经网络中用于添加非线性,使得网络能够学习复杂的函数。 9. **损失函数(Loss Functions)**:用于评估模型的预测与真实值之间的差异,常见的损失函数包括均方误差(MSE)、交叉熵(Cross-Entropy)等。 10. **优化算法(Optimization Algorithms)**:如梯度下降(Gradient Descent)、随机梯度下降(SGD)、Adam 等,用于更新网络权重,以最小化损失函数。 11. **正则化(Regularization)**:技术如 Dropout、L1/L2 正则化等,用于防止模型过拟合。 12. **迁移学习(Transfer Learning)**:利用在一个任务上训练好的模型来提高另一个相关任务的性能。 深度学习在许多领域都取得了显著的成就,但它也面临着一些挑战,如对大量数据的依赖、模型的解释性差、计算资源消耗大等。研究人员正在不断探索新的方法来解决这些问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值