public void Start(int port) { pool = new UserTokenPool(maxClient); acceptClients = new Semaphore(maxClient, maxClient); for (int i = 0; i < maxClient; i++) { UserToken userToken = new UserToken(); //初始化token信息 userToken.receiveSAEA.Completed += IO_Comleted; userToken.sendSAEA.Completed += IO_Comleted; userToken.LD = LD; userToken.LE = LE; userToken.encode = encode; userToken.decode = decode; userToken.sendProcess = ProcessSend; userToken.closeProcess = ClientClose; userToken.center = center; pool.Push(userToken); } //监听当前服务器网卡所有可用IP地址的port端口 // 外网IP 内网IP192.168.x.x 本机IP一个127.0.0.1 try { server.Bind(new IPEndPoint(IPAddress.Any, port)); //置于监听状态 server.Listen(10); StartAccept(null); } catch (Exception e) { Console.WriteLine(e.Message); } }
private Socket _server; //服务器Socket监听对象 #endregion Fields #region Constructors /// <summary> /// 初始化通信监听 /// </summary> /// <param name="max">最大连接数</param> /// <param name="port">监听的端口</param> public ServerStart(int max,int port) { _server = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); _maxClient = max; //实例化socket监听对象 _pool=new UserTokenPool(max); //实例化连接池对象 //实例化一个最大允许max个线程允许的信号量 //并将它的计数器的初始值设为max //这就是说除了调用该信号量的线程都将被阻塞 _acceptClients = new Semaphore(max,max); //初始化创建max个数的链接对象并存入连接池 for (var i = 0; i < max; i++) { //初始化token信息 var token=new UserToken(); //绑定接收事件 token.ReceiveSAEA.Completed+= new EventHandler<SocketAsyncEventArgs>(IO_Comleted); //绑定发送事件 token.SendSAEA.Completed+= new EventHandler<SocketAsyncEventArgs>(IO_Comleted); token.LD = LD; token.LE = LE; token.encode = encode; token.decode = decode; token.sendProcess = ProccessSend; _pool.Push(token); } }
/// <summary>开启服务端 </summary> public void Start(int port) { //创建连接池 pool = new UserTokenPool(maxClient); //连接信号量 acceptClients = new Semaphore(maxClient, maxClient); for (int i = 0; i < maxClient; i++) { UserToken token = new UserToken(); //初始化token信息 token.ReceiveSAEA.Completed += new EventHandler <SocketAsyncEventArgs>(IOCompleted); token.SendSAEA.Completed += new EventHandler <SocketAsyncEventArgs>(IOCompleted); token.LD = LD; token.LE = LE; token.Encode = Encode; token.Decode = Decode; token.SendProcess = ProcessSend; token.CloseProcess = ClientClose; token.Center = Center; pool.Push(token); } //监听当前服务器网卡所有可用IP地址的port端口 //外网IP 内网IP192.168.x.x 本机IP一个127.0.0.1 try { server.Bind(new IPEndPoint(IPAddress.Any, port)); //置于监听状态,并没有真正的有监听事件 server.Listen(10); StartAccept(null); } catch (Exception e) { Console.WriteLine(e.Message); } }
public void ProcessSend(SocketAsyncEventArgs e) { UserToken token = e.UserToken as UserToken; if (e.SocketError != SocketError.Success) { ClientClose(token, e.SocketError.ToString()); } else { if (token.CheckWriteQueue()) { //消息发送成功,回调成功 token.Writed(); } else { token.Close(); pool.Push(token); } } }
// 客户端断开连接 断开连接的错误编码 public void ClientClose(UserToken token, string error) { if (token.conn != null) { lock (token) { //通知应用层 客户端断开连接 token.Close(); pool.Push(token); acceptClients.Release(); } } }
public ServerStart(int max) { server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); maxClient = max; pool = new UserTokenPool(max); acceptClients = new Semaphore(max, max); for (int i = 0; i < max; i++) { UserToken token = new UserToken(); token.receiveSAEA.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Completed); token.sendSAEA.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Completed); pool.Push(token); } }
/// <summary> /// 客户端断开连接 /// </summary> /// <param name="token">断开连接的用户对象</param> /// <param name="error">断开连接的错误编码</param> public void ClientClose(UserToken token, string error) { if (token.conn != null) { //防止关闭释放的时候,出现多线程的访问,也是避免同一个userToken同时有多个线程操作 lock (token) { //通知应用层面,客户端断开连接了 Center.ClientClose(token, error); token.Close(); pool.Push(token); //加回一个信号量,供其他用户使用 acceptClients.Release(); } } }
/// <summary> /// 客户端断开连接 /// </summary> /// <param name="token"> 断开连接的用户对象</param> /// <param name="error">断开连接的错误编码</param> public void ClientClose(UserToken token, string error) { if (token.conn != null) { lock (token) { //通知应用层面 客户端断开连接了 center.ClientClose(token, error); token.Close(); //加回一个信号量,供其它用户使用 pool.Push(token); acceptClients.Release(); } } }
/// <summary> /// 客户端断开连接 /// </summary> /// <param name="token">断开连接的用户</param> public void ClientClose(UserToken token, string error) { if (token.conn != null) { lock (token) { //TODO 通知应用层 客户端断开连接 center.ClientClose(token, error); token.Close(); //加回一个信号量 供其他用户使用 pool.Push(token); acceptClient.Release(); } } }
/// <summary> /// 初始化对象池 /// </summary> /// <param name="max">连接数</param> private void InitTokenPool(int max) { pool = new UserTokenPool(max); //创建连接池 acceptClient = new Semaphore(max, max); //连接信号量,初始化数量和最大连接数量 //在初始化的时候就将连接对象依次创建,好处:省去了连接时候创建,断开时销毁的过程,坏处:服务器连接空余较多 for (int i = 0; i < max; i++) { UserToken token = new UserToken(); //初始化token信息 token.receiveSAEA.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Completed); token.sendSAEA.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Completed); token.lengthEncode = lengthEncode; token.lengthDecode = lengthDecode; token.messageEncode = messageEncode; token.messageDecode = messageDecode; token.sendProcess = ProcessSend; token.closeProcess = ClientClose; token.center = center; pool.Push(token); } }
/// <summary>开启服务端 </summary> public void Start(int port) { //创建连接池 pool = new UserTokenPool(maxClient); //连接信号量 acceptClients = new Semaphore(maxClient, maxClient); for (int i = 0; i < maxClient; i++) { UserToken token = new UserToken(); //初始化token信息 token.ReceiveSAEA = null; token.SendSAEA.Completed += new EventHandler <SocketAsyncEventArgs>(IOCompleted); token.LD = LD; token.LE = LE; token.Encode = Encode; token.Decode = Decode; token.SendProcess = ProcessSend; token.CloseProcess = ClientClose; token.Center = Center; pool.Push(token); } receiveSocketArgs = new SocketAsyncEventArgs(); receiveSocketArgs.Completed += new EventHandler <SocketAsyncEventArgs>(IOCompleted); receiveSocketArgs.RemoteEndPoint = new IPEndPoint(IPAddress.Any, port); receiveSocketArgs.SetBuffer(new byte[1024], 0, 1024); //监听当前服务器网卡所有可用IP地址的port端口 //外网IP 内网IP192.168.x.x 本机IP一个127.0.0.1 try { server.Bind(new IPEndPoint(IPAddress.Any, port)); sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); //置于监听状态,并没有真正的有监听事件 //server.Listen(10); StartReceive(); } catch (Exception e) { Console.WriteLine(e.Message); } }
public void Start(int port) { //创建连接池 pool = new UserTokenPool(maxClient); //连接信号量 acceptClient = new Semaphore(maxClient, maxClient); for (int i = 0; i < maxClient; i++) { UserToken token = new UserToken(); token.receiveSAEA.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Completed); token.sendSAEA.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Completed); token.LD = LD; token.LE = LE; token.encode = encode; token.decode = decode; token.sendProcess = ProcessSend; token.closeProcess = ClientClose; token.center = center;//在ServerStart和Start之间赋值 //初始化token信息 pool.Push(token); } try { //监听服务器当前网卡的所有可用IP和port端口 server.Bind(new IPEndPoint(IPAddress.Any, port)); //置于监听状态 server.Listen(10); StartAccept(null); } catch (Exception e) { Console.WriteLine(e.Message); } }
/// <summary> /// 初始化对象池 /// </summary> /// <param name="max">连接数</param> private void InitTokenPool(int max) { pool = new UserTokenPool(max); //创建连接池 acceptClient = new Semaphore(max, max); //连接信号量,初始化数量和最大连接数量 //在初始化的时候就将连接对象依次创建,好处:省去了连接时候创建,断开时销毁的过程,坏处:服务器连接空余较多 for (int i = 0; i < max; i++) { UserToken token = new UserToken(); //初始化token信息 token.receiveSAEA.Completed += new EventHandler<SocketAsyncEventArgs>(IO_Completed); token.sendSAEA.Completed += new EventHandler<SocketAsyncEventArgs>(IO_Completed); token.lengthEncode = lengthEncode; token.lengthDecode = lengthDecode; token.messageEncode = messageEncode; token.messageDecode = messageDecode; token.sendProcess = ProcessSend; token.closeProcess = ClientClose; token.center = center; pool.Push(token); } }