Exemplo n.º 1
0
        /// <summary>
        /// 异步发送接收结束回调
        /// </summary>
        /// <param name="ar"></param>
        private void EndSend(IAsyncResult ar)
        {
            TcpSocketSendState sendState = ar.AsyncState as TcpSocketSendState;
            int length = UserToken.TcpSocket.EndSend(ar);

            sendState.CurrentLength += length;

            if (sendState.errorCode == SocketError.Success)
            {
                if (sendState.CurrentLength < sendState.TargetLength)
                {
                    UserToken.TcpSocket.BeginSend(sendState.Buffer, sendState.CurrentLength, sendState.TargetLength, SocketFlags.None, out sendState.errorCode, new AsyncCallback(EndSend), sendState);
                }
                else
                {
                    if (sendState.SendInvokeElement != null)
                    {
                        sendState.SendInvokeElement.SocketCompletedInvoke(UserToken);
                    }
                }
            }
            else
            {
                Error("End Send Error: ", sendState.errorCode);
                if (sendState.SendInvokeElement != null)
                {
                    sendState.SendInvokeElement.SocketErrorInvoke(UserToken, sendState.errorCode);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 异步发送,前一步次发送没完成,不影响后一次发送,强连接使用,比如帧同步战斗数据发送
        /// </summary>
        /// <param name="buffer">发送数据</param>
        /// <param name="invokeElement">发送完成回调元素</param>
        public void BeginSend(byte[] buffer, TcpSocketInvokeElement invokeElement)
        {
            if (UserToken.TcpSocket == null)
            {
                return;
            }
            if (!UserToken.TcpSocket.Connected)
            {
                invokeElement.SocketErrorInvoke(UserToken, SocketError.Shutdown);
                return;
            }
            TcpSocketSendState sendState = new TcpSocketSendState(UserToken.TcpSocket);

            sendState.SendInvokeElement = invokeElement;
            sendState.Buffer            = buffer;
            UserToken.TcpSocket.BeginSend(sendState.Buffer, sendState.CurrentLength, sendState.TargetLength, SocketFlags.None, out sendState.errorCode, new AsyncCallback(EndSend), sendState);
        }