private void BeginRead(IAsyncResult ar) { int read = pipe.EndRead(ar); if (read == 0) { return; } int length = BitConverter.ToInt16(buffer, 0) + 2; if (length <= 2) { return; } byte[] received = new byte[length]; Array.Copy(buffer, received, 2); int pos = 2; while (length - pos > 0) { pos += pipe.Read(received, pos, length - pos); } // Call OnReceive asynchronously if (OnReceive != null) { OnReceive.BeginInvoke(new NetworkMessage(client, received, read), null, null); } PipePacketType type = (PipePacketType)received[2]; switch (type) { case PipePacketType.OnClickContextMenu: if (OnContextMenuClick != null) { if (length == 7) { OnContextMenuClick.BeginInvoke(new NetworkMessage(client, received, length), null, null); } } break; case PipePacketType.HookReceivedPacket: if (OnSocketRecv != null) { OnSocketRecv.BeginInvoke(new NetworkMessage(client, received, length), null, null); } break; case PipePacketType.HookSentPacket: if (OnSocketSend != null) { OnSocketSend.BeginInvoke(new NetworkMessage(client, received, length), null, null); } break; case PipePacketType.OnClickIcon: if (OnIconClick != null) { OnIconClick.BeginInvoke(new NetworkMessage(client, received, length), null, null); } break; default: break; } pipe.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(BeginRead), null); }
private void _OnCompleted(object sender, SocketAsyncEventArgs args) { //try { switch (args.LastOperation) { case SocketAsyncOperation.Accept: //accept a connect { if (args.SocketError != SocketError.Success) { //直接复用 args.AcceptSocket = null; listenSocket.AcceptAsync(args); } if (args.SocketError == SocketError.Success) { var info = new Link(args.AcceptSocket, null); Connects[info.Handle] = info; //直接复用 args.AcceptSocket = null; listenSocket.AcceptAsync(args); if (args.SocketError == SocketError.Success) { OnSocketAccept?.Invoke(info.Handle); SetRecivce(info); } } } return; case SocketAsyncOperation.Connect: //connect succ { if (args.SocketError != SocketError.Success) { OnSocketError?.Invoke(args.UserToken as Socket); args.Dispose(); } else { var info = new Link(args.ConnectSocket, args); Connects[info.Handle] = info; OnSocketLinked?.Invoke(info.Handle); SetRecivce(info); } //connect 的这个args不能复用 } return; case SocketAsyncOperation.Send: { var hash = (args.UserToken as Link).Handle; if (args.SocketError != SocketError.Success) { ReuseSocketAsyncEventArgs(args); //断链,复用这个接受参数 CloseConnect(hash); } else { //发送成功,也复用这个发送参数 ReuseSocketAsyncEventArgs(args); //断链,复用这个接受参数 OnSocketSend?.Invoke(hash); } } return; case SocketAsyncOperation.Receive: { var hash = (args.UserToken as Link).Handle; if (args.BytesTransferred == 0 || args.SocketError != SocketError.Success) { ReuseRecvSocketAsyncEventArgs(args); //断链,复用这个接受参数 CloseConnect(hash); return; } else { byte[] recv = new byte[args.BytesTransferred]; Buffer.BlockCopy(args.Buffer, args.Offset, recv, 0, args.BytesTransferred); (args.UserToken as Link).Socket.ReceiveAsync(args); //直接复用 OnSocketRecv?.Invoke(hash, recv); } } return; default: { break; } } } }