private void processAccepte(SocketAsyncEventArgs args) { //从池中取出一个空闲的异步操作对象 AsyncSocketUserToken token = this.asyncSocketUserTokenPool.pop(); //将此对象添加到正在连接集合中 lock (this.asyncSocketUserTokenList) { this.asyncSocketUserTokenList.add(token); } //设置保存连接socket token.ConnectedSocket = args.AcceptSocket; if (SysCache.ShowInfoLog) { if (token.ConnectedSocket.RemoteEndPoint != null) { LogHelper.Info(string.Format("WEB通信服务连接上一个客户端,信息为{0}", token.ConnectedSocket.RemoteEndPoint.ToString())); } } try { //对连接后的socket对象投递接收请求 //投递异步操作对象AsyncSocketUserToken中的ReceiveAysncEventArgs //进行数据接收处理时,也是用这个ReceiveAysncEventArgs bool willRaiseEvent = token.ConnectedSocket.ReceiveAsync(token.ReceiveAysncEventArgs); if (!willRaiseEvent) { lock (token) { processReceive(token.ReceiveAysncEventArgs); } } } catch (Exception e) { string error = ""; if (token.ConnectedSocket.RemoteEndPoint != null) { error = string.Format("WEB通信服务客户端{0}在投递接收请求时出错,错误信息为{1}", token.ConnectedSocket.RemoteEndPoint.ToString(), e.Message); } else { error = string.Format("在投递接收请求时出错,错误信息为{0}", e.Message); } closeClientSocket(token); } finally { //数据是否处理成功,都要进行下一次的异步接收 if (onServerConnectedClientHandler != null) { string connectedCount = string.Format("当前连接上来的web客户端总数为:{0},缓冲区大小为:{1}", this.asyncSocketUserTokenList.count(), this.asyncSocketUserTokenPool.Count); onServerConnectedClientHandler(this, connectedCount); } startAccept(args); } }
public void remove(AsyncSocketUserToken userToken) { lock (userTokenList) { this.userTokenList.Remove(userToken); } }
public void add(AsyncSocketUserToken userToken) { lock (userTokenList) { this.userTokenList.Add(userToken); } }
public void copyList(ref AsyncSocketUserToken[] array) { lock (userTokenList) { array = new AsyncSocketUserToken[this.userTokenList.Count]; this.userTokenList.CopyTo(array); } }
private void processReceive(SocketAsyncEventArgs args) { AsyncSocketUserToken token = args.UserToken as AsyncSocketUserToken; if (token.ConnectedSocket == null) { return; } try { //接收数据缓冲区的偏移量 int offset = token.ReceiveAysncEventArgs.Offset; //当前缓冲区接收到数据的总量 int count = token.ReceiveAysncEventArgs.BytesTransferred; if (token.ReceiveAysncEventArgs.BytesTransferred > 0 && token.ReceiveAysncEventArgs.SocketError == SocketError.Success) { if (token.InvokeElement == null) { token.InvokeElement = new WebInvokeElement(this, token); } if (count > 0) { if (!token.InvokeElement.processReceive(token.ReceiveAysncEventArgs.Buffer, offset, count)) { closeClientSocket(token); } else { bool willRaiseEvent = token.ConnectedSocket.ReceiveAsync(token.ReceiveAysncEventArgs); if (!willRaiseEvent) { processReceive(token.ReceiveAysncEventArgs); } } } else { bool willRaiseEvent = token.ConnectedSocket.ReceiveAsync(token.ReceiveAysncEventArgs); if (!willRaiseEvent) { processReceive(token.ReceiveAysncEventArgs); } } } else { closeClientSocket(token); } } catch (Exception) { closeClientSocket(token); } }
public void push(AsyncSocketUserToken item) { if (item == null) { throw new ArgumentException("item added to a AsyncSocketUserToken is not null!"); } lock (userTokenPool) { this.userTokenPool.Push(item); } }
private void init() { //Console.WriteLine("the server init....."); AsyncSocketUserToken userToken; for (int i = 0; i < numConnections; i++) { userToken = new AsyncSocketUserToken(asyncReceiveBufferSize); userToken.ReceiveAysncEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Complete); userToken.SendAysncEvetnArgs.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Complete); this.asyncSocketUserTokenPool.push(userToken); } }
public void closeClientSocket(AsyncSocketUserToken token) { if (onServerCloseSocketHandler != null) { onServerCloseSocketHandler(this, token); } if (token.ConnectedSocket == null) { return; } try { token.ConnectedSocket.Shutdown(SocketShutdown.Both); } catch (Exception e) { if (SysCache.ShowErrorLog) { string error = string.Format("程序关闭连接时出错,错误信息{0}", e.Message); LogHelper.Error(error); } } token.ConnectedSocket.Close(); token.ConnectedSocket = null; //释放信号量资源 this.maxConnection.Release(); //并将此客户端资源回收到池中 this.asyncSocketUserTokenPool.push(token); //正在连接的集合中祛除此客户端信息 lock (this.asyncSocketUserTokenList) { this.asyncSocketUserTokenList.remove(token); } if (onServerConnectedClientHandler != null) { string connectedCount = string.Format("当前连接上来的web客户端总数为:{0},缓冲区大小为:{1}", this.asyncSocketUserTokenList.count(), this.asyncSocketUserTokenPool.Count); onServerConnectedClientHandler(this, connectedCount); } }
private void IO_Complete(Object sender, SocketAsyncEventArgs args) { AsyncSocketUserToken userToken = args.UserToken as AsyncSocketUserToken; userToken.ActiveDateTime = DateTime.Now; try { lock (userToken) { if (args.LastOperation == SocketAsyncOperation.Receive) { processReceive(args); } else if (args.LastOperation == SocketAsyncOperation.Send) { processSend(args); } else { if (SysCache.ShowErrorLog) { if (userToken.ConnectedSocket.RemoteEndPoint != null) { string error = string.Format("程序的最后一次操作在{0}并不是receive或者send", userToken.ConnectedSocket.RemoteEndPoint.ToString()); LogHelper.Error(error); } } } } } catch (Exception e) { if (SysCache.ShowErrorLog) { string error = string.Format("程序处理IO时出错,错误信息{0}", e.Message); LogHelper.Error(error); } closeClientSocket(userToken); } }
private bool processSend(SocketAsyncEventArgs args) { AsyncSocketUserToken token = args.UserToken as AsyncSocketUserToken; if (token.ConnectedSocket == null) { return(false); } //token.ActiveDateTime = DateTime.Now; if (args.SocketError == SocketError.Success) { //设置发送的数据 //接收到什么数据就发送什么数据 //args.SetBuffer(args.Offset, args.BytesTransferred); //设置数据发送缓冲区后,可以在这里进行下一次的数据发送 return(token.InvokeElement.sendComplete()); //return false; } else { closeClientSocket(token); return(false); } }