public void SendEx(IocpUserToken token, byte[] buffer) { byte[] encrypted = AESEx.AESEncrypt(buffer, CommonConfig.key, CommonConfig.iv); MemoryStream st = new MemoryStream(); BinaryWriter bw = new BinaryWriter(st); bw.Write(encrypted.Length + sizeof(int) + sizeof(ushort)); bw.Write(Shared.PackageSignature); bw.Write(encrypted); RealSend(token, BufferEx.MemoryStreamToBytes(st)); }
public void SendEx(IocpUserToken token, byte[] buffer) { byte[] encrypted = AESEx.AESEncrypt(buffer, CommonConfig.key, CommonConfig.iv); MemoryStream st = new MemoryStream(); BinaryWriter bw = new BinaryWriter(st); bw.Write(encrypted.Length + sizeof(int) + sizeof(ushort)); bw.Write(Shared.PackageSignature); bw.Write(encrypted); RealSend(token, BufferEx.MemoryStreamToBytes(st)); }
/// <summary> /// 发送数据包给客户 /// </summary> /// <param name="token"></param> /// <param name="sendBuffer"></param> public void RealSend(IocpUserToken token, byte[] sendBuffer) { SocketAsyncEventArgs args = GetSocketAsyncEventArgs(); args.SetBuffer(sendBuffer, 0, sendBuffer.Length); args.UserToken = token; try { if (!token.Connection.SendAsync(args)) { OnClientIoSend(args); } } catch (Exception) { CloseClientSocket(args); //关闭一个套接字 } }
/// <summary> /// 当这个被调用时,代表一个异步接收操作完成。 /// 处理新用户的连接请求。 /// </summary> /// <param name="e">SocketAsyncEventArg表示一个已经完成的异步IO事件。</param> private void ProcessAccept(SocketAsyncEventArgs e) { Socket s = e.AcceptSocket; if (s.Connected) { SocketAsyncEventArgs readEventArgs = this.GetSocketAsyncEventArgs(); try { IocpUserToken token = new IocpUserToken(s, Interlocked.Increment(ref unique_id)); readEventArgs.UserToken = token; //增加当前服务器的连接数量计数器。 Interlocked.Increment(ref this.numConnectedSockets); //添加用户连接到表 lock (tokenMap) { tokenMap[token.UniqueId] = token; } if (NotifyNewConnection != null) { NotifyNewConnection(this, new IocpNewConnectionArgs(token)); } if (!s.ReceiveAsync(readEventArgs)) { this.OnClientIoReceive(readEventArgs); } } catch (Exception) { CloseClientSocket(readEventArgs); } // 接收下一个连接请求 this.PostIoAccept(e); } else { s.Close(); } }
/// <summary> /// 关闭一个连接。 /// </summary> /// <param name="token">用户关联的IocpUserToken结构</param> /// <param name="e">与发送接收相关的SocketAsyncEventArgs结构</param> private void CloseClientSocket(IocpUserToken token, SocketAsyncEventArgs e) { lock (tokenMap) { if (tokenMap.Remove(token.UniqueId)) { //通知用户断开了连接 if (NotifyDisconnectConnection != null) { NotifyDisconnectConnection(this, new IocpDisconnectConnectionArgs(token)); } // 减少总连接数 Interlocked.Decrement(ref this.numConnectedSockets); token.Connection.Close(); } } // 释放SocketAsyncEventArgs结构,以方便让其他套接字重用该结构 this.ReleaseSocketAsyncEventArgs(e); }
/// <summary> /// 当这个被调用时,代表一个异步接收操作完成。 /// 当远程主机关闭连接,这个Socket也将被关闭 /// 如果有数据接收,那么就会执行事件通知用户 /// </summary> /// <param name="e">SocketAsyncEventArg表示一个已经完成的异步IO事件。</param> private void OnClientIoReceive(SocketAsyncEventArgs e) { //获取与用户关联的Token结构 IocpUserToken token = e.UserToken as IocpUserToken; //获取用户的套接字 Socket s = token.Connection; // 检查是否是数据接收 // 是否非错误并且SOCKET是有效的 try { if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success && s.Available == 0) { if (NotifyReceivedPackage != null) { MemoryStream TempBuffer = new MemoryStream(); new BinaryWriter(TempBuffer).Write(e.Buffer, 0, e.BytesTransferred); NotifyReceivedPackage(this, new IocpPacketEventArgs(token, BufferEx.MemoryStreamToBytes(TempBuffer))); } //投递下一个接收函数 if (!s.ReceiveAsync(e)) { // 如果投递失败,检查是否是成功接收 this.OnClientIoReceive(e); } } else { throw new SocketException(10054); //WSAECONNRESET } } catch (Exception) { this.CloseClientSocket(e); } }
/// <summary> /// 当这个被调用时,代表一个异步接收操作完成。 /// 当远程主机关闭连接,这个Socket也将被关闭 /// 如果没有错误,那么就会执行事件通知用户发送的字节数以及数据 /// </summary> /// <param name="e">SocketAsyncEventArg表示一个已经完成的异步IO事件。</param> private void OnClientIoSend(SocketAsyncEventArgs e) { //获取与用户关联的Token结构 IocpUserToken token = e.UserToken as IocpUserToken; //获取用户的套接字 Socket s = token.Connection; if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success) { if (NotifySendComplete != null) { MemoryStream TempBuffer = new MemoryStream(); new BinaryWriter(TempBuffer).Write(e.Buffer, 0, e.BytesTransferred); NotifySendComplete(this, new IocpPacketEventArgs(token, BufferEx.MemoryStreamToBytes(TempBuffer))); } ReleaseSocketAsyncEventArgs(e); } else { this.CloseClientSocket(e); } }
/// <summary> /// 关闭一个连接。 /// </summary> /// <param name="e">与发送接收相关的SocketAsyncEventArgs结构</param> public void CloseClientSocket(SocketAsyncEventArgs e) { IocpUserToken token = e.UserToken as IocpUserToken; this.CloseClientSocket(token, e); }
/// <summary> /// 发送数据包给客户 /// </summary> /// <param name="token"></param> /// <param name="sendBuffer"></param> public void RealSend(IocpUserToken token, byte[] sendBuffer) { SocketAsyncEventArgs args = GetSocketAsyncEventArgs(); args.SetBuffer(sendBuffer, 0, sendBuffer.Length); args.UserToken = token; try { if (!token.Connection.SendAsync(args)) { OnClientIoSend(args); } } catch (Exception) { CloseClientSocket(args); //关闭一个套接字 } }
public IocpDisconnectConnectionArgs(IocpUserToken token) { this.token = token; }
public IocpDisconnectConnectionArgs(IocpUserToken token) { this.token = token; }
public IocpNewConnectionArgs(IocpUserToken token) { this.token = token; }
private void ProcessError(SocketAsyncEventArgs e) { IocpUserToken token = e.UserToken as IocpUserToken; this.CloseClientSocket(token, e); }
public NetworkComputeBuffer(IocpUserToken _instance) { this.token = _instance; BytesTransferred = 0; TotalBuffer = new MemoryStream(); }
public USER_NetworkReceivedArgs(IocpUserToken token, byte[] buffer) { this.token = token; this.buffer = buffer; }
public IocpPacketEventArgs(IocpUserToken token, byte[] buffer) { this.token = token; this.Buffer = buffer; }
public IocpPacketEventArgs(IocpUserToken token, byte[] buffer) { this.token = token; this.Buffer = buffer; }
/// <summary> /// 当这个被调用时,代表一个异步接收操作完成。 /// 处理新用户的连接请求。 /// </summary> /// <param name="e">SocketAsyncEventArg表示一个已经完成的异步IO事件。</param> private void ProcessAccept(SocketAsyncEventArgs e) { Socket s = e.AcceptSocket; if (s.Connected) { SocketAsyncEventArgs readEventArgs = this.GetSocketAsyncEventArgs(); try { IocpUserToken token = new IocpUserToken(s, Interlocked.Increment(ref unique_id)); readEventArgs.UserToken = token; //增加当前服务器的连接数量计数器。 Interlocked.Increment(ref this.numConnectedSockets); //添加用户连接到表 lock (tokenMap) { tokenMap[token.UniqueId] = token; } if (NotifyNewConnection != null) { NotifyNewConnection(this, new IocpNewConnectionArgs(token)); } if (!s.ReceiveAsync(readEventArgs)) { this.OnClientIoReceive(readEventArgs); } } catch (Exception) { CloseClientSocket(readEventArgs); } // 接收下一个连接请求 this.PostIoAccept(e); } else { s.Close(); } }
public IocpNewConnectionArgs(IocpUserToken token) { this.token = token; }
/// <summary> /// 关闭一个连接。 /// </summary> /// <param name="token">用户关联的IocpUserToken结构</param> /// <param name="e">与发送接收相关的SocketAsyncEventArgs结构</param> private void CloseClientSocket(IocpUserToken token, SocketAsyncEventArgs e) { lock (tokenMap) { if (tokenMap.Remove(token.UniqueId)) { //通知用户断开了连接 if (NotifyDisconnectConnection != null) { NotifyDisconnectConnection(this, new IocpDisconnectConnectionArgs(token)); } // 减少总连接数 Interlocked.Decrement(ref this.numConnectedSockets); token.Connection.Close(); } } // 释放SocketAsyncEventArgs结构,以方便让其他套接字重用该结构 this.ReleaseSocketAsyncEventArgs(e); }
public USER_NetworkReceivedArgs(IocpUserToken token, byte[] buffer) { this.token = token; this.buffer = buffer; }
public NetworkComputeBuffer(IocpUserToken _instance) { this.token = _instance; BytesTransferred = 0; TotalBuffer = new MemoryStream(); }