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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于Django+python编写开发的毕业生就业管理系统支持学生教师角色+db数据库(毕业设计新项目).zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我! 基于Django+python编写开发的毕业生就业管理系统支持学生教师角色+db数据库(毕业设计新项目).zip基于Django+python编写开发的毕业生就业管理系统支持学生教师角色+db数据库(毕业设计新项目).zip基于Django+python编写开发的毕业生就业管理系统支持学生教师角色+db数据库(毕业设计新项目).zip基于Django+python编写开发的毕业生就业管理系统支持学生教师角色+db数据库(毕业设计新项目).zip基于Django+python编写开发的毕业生就业管理系统支持学生教师角色+db数据库(毕业设计新项目).zip基于Django+python编写开发的毕业生就业管理系统支持学生教师角色+db数据库(毕业设计新项目).zip基于Django+python编写开发的毕业生就业管理系统支持学生教师角色+db数据库(毕业设计新项目).zip基于Django+python编写开发的毕业生就业管理系统支持学生教师角色+db数据库(毕业设计新项目).zip基于Django+python编写开发的毕业生就业管理系统支持学生教师角色+db数据库(毕业设计新项目).zip
毕设新项目基于python3.7+django+sqlite开发的学生就业管理系统源码+使用说明(含vue前端源码).zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我! 学生就业管理系统(前端) ## 项目开发环境 - IDE: vscode - node版本: v12.14.1 - npm版本: 6.13.4 - vue版本: @vue/cli 4.1.2 - 操作系统: UOS 20 ## 1.进入项目目录安装依赖 ``` npm install ``` ## 2.命令行执行进入UI界面进行项目管理 ``` vue ui ``` ## 3.编译发布包(请注意编译后存储路径) #### PS:需要将编译后的包复制到后端项目的根目录下并命名为'static' 学生就业管理系统(后端) ## 1.项目开发环境 - IDE: vscode - Django版本: 3.0.3 - Python版本: python3.7.3 - 数据库 : sqlite3(测试专用) - 操作系统 : UOS 20 ## 2.csdn下载本项目并生成/安装依赖 ``` pip freeze > requirements.txt pip install -r requirements.txt ``` ## 3.项目MySQL数据库链接错误 [点击查看解决方法](https://www.cnblogs.com/izbw/p/11279237.html)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值