Exemplo n.º 1
0
        public void Send(long handle, byte[] data)
        {
            var args = GetFreeEventArgs();

            //var args = new SocketAsyncEventArgs();
            //args.Completed += onCompletedSend;

            args.UserToken = Connects[handle];
            args.SetBuffer(data, 0, data.Length);
            //args.RemoteEndPoint = Connects[handle].Socket.RemoteEndPoint;
            try
            {
                bool basync = Connects[handle].Socket.SendToAsync(args);
                if (basync == false)
                {
                    ReuseSocketAsyncEventArgs(args);//复用这个发送参数
                    //这个操作同步完成了
                    OnSocketSend?.Invoke(handle);
                }
            }
            catch (Exception err)
            {
                ReuseSocketAsyncEventArgs(args); //复用这个发送参数

                CloseConnect(handle);            //
            }
        }
Exemplo n.º 2
0
        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;
                }
                }
            }
        }