Ejemplo n.º 1
0
        public void Send(AsyncUserToken userToken, byte[] buffer)
        {
            //查找有没有空闲的发送MySocketEventArgs,有就直接拿来用,没有就创建新的.So easy!
            MySocketEventArgs sendArgs = null;

            lock (listArgs)
            {
                sendArgs = listArgs.Find(a => a.IsUsing == false);
                if (sendArgs == null)
                {
                    sendArgs = initSendArgs();
                }
                sendArgs.IsUsing = true;
            }

            //Log4Debug("发送所用的套接字编号:" + sendArgs.ArgsTag);
            //lock (sendArgs) //要锁定,不锁定让别的线程抢走了就不妙了.
            {
                sendArgs.SetBuffer(buffer, 0, buffer.Length);
            }
            Socket s = userToken.ConnectSocket;

            if (!s.SendAsync(sendArgs))//投递发送请求,这个函数有可能同步发送出去,这时返回false,并且不会引发SocketAsyncEventArgs.Completed事件
            {
                // 同步发送时处理发送完成事件
                ProcessSend(sendArgs);
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 发送完成时处理函数
 /// </summary>
 /// <param name="e">与发送完成操作相关联的SocketAsyncEventArg对象</param>
 private void ProcessSend(MySocketEventArgs e)
 {
     //SocketAsyncEventArgs e = userToken.SAEA_Send;
     if (e.SocketError == SocketError.Success)
     {
         e.IsUsing = false;
     }
     else
     {
         Log4Debug("发送未成功,回调:" + e.SocketError);
     }
 }
Ejemplo n.º 3
0
    /// <summary>
    /// 异步发送操作完成后调用该方法
    /// </summary>
    private void ProcessSend(SocketAsyncEventArgs arg)
    {
        MySocketEventArgs e = (MySocketEventArgs)arg;

        //SocketAsyncEventArgs e = userToken.SAEA_Send;
        e.IsUsing = false;
        if (e.SocketError == SocketError.Success)
        {
        }
        else if (e.SocketError == SocketError.Shutdown)
        {
            AsyncUserToken userToken = (AsyncUserToken)e.UserToken;
            CloseClientSocket(userToken);
            Log4Debug("Socket已断线");
        }
    }
Ejemplo n.º 4
0
    /// <summary>
    /// 初始化发送参数MySocketEventArgs
    /// </summary>
    /// <returns></returns>
    MySocketEventArgs initSendArgs()
    {
        MySocketEventArgs sendArg = new MySocketEventArgs();

        sendArg.Completed     += new EventHandler <SocketAsyncEventArgs>(OnIOCompleted);
        sendArg.UserToken      = _Socket;
        sendArg.RemoteEndPoint = hostEndPoint;
        sendArg.IsUsing        = false;
        Interlocked.Increment(ref tagCount);
        sendArg.ArgsTag = tagCount;
        lock (listArgs)
        {
            listArgs.Add(sendArg);
        }
        return(sendArg);
    }
Ejemplo n.º 5
0
    private void Send(AsyncUserToken userToken, byte[] send)
    {
        try
        {
            byte[] buffer = AsyncUserToken.GetSendBytes(send);
            //string sClientIP = ((IPEndPoint)userToken.ConnectSocket.RemoteEndPoint).ToString();
            //string info = "";
            //for (int i = 0; i < buffer.Length; i++)
            //{
            //    info += buffer[i] + ",";
            //}
            //Log4Debug("From the " + sClientIP + " to send " + buffer.Length + " bytes of data:" + info);

            //查找有没有空闲的发送MySocketEventArgs,有就直接拿来用,没有就创建新的.So easy!
            MySocketEventArgs sendArgs = null;
            lock (listArgs)//要锁定,不锁定让别的线程抢走了就不妙了.
            {
                sendArgs = listArgs.Find(a => a.IsUsing == false);
                if (sendArgs == null)
                {
                    sendArgs = initSendArgs();
                }
                sendArgs.IsUsing = true;
            }
            sendArgs.SetBuffer(buffer, 0, buffer.Length);
            sendArgs.UserToken = userToken;

            Socket s = userToken.ConnectSocket;
            if (!s.SendAsync(sendArgs))//投递发送请求,这个函数有可能同步发送出去,这时返回false,并且不会引发SocketAsyncEventArgs.Completed事件
            {
                // 同步发送时处理发送完成事件
                ProcessSend(sendArgs);
            }
        }
        catch (Exception e)
        {
            CloseClientSocket(userToken);
        }
    }