示例#1
0
    //发送协议并加锁,防止连续多次发送协议,必须收到指定协议之后才可继续发送协议
    public bool sendCmd_And_Lock(BaseCmd _cmd, int _unLockKey)
    {
        if (_cmd.getSize() <= 0 || !m_isConnected || m_sendLock)
        {
            return(false);
        }

        int         _message_length = _cmd.getSize() + 4;
        BytesBuffer _sendBuf        = new BytesBuffer(_message_length);

        _sendBuf.writeInt32(_message_length - 4);

        System.Buffer.BlockCopy(_cmd.getBuffer.bytes, 0, _sendBuf.bytes, 4, _cmd.getSize());

        m_ClientSocket.Client.BeginSend(_sendBuf.bytes, 0, _sendBuf.bytes.Length, SocketFlags.None, new AsyncCallback(_onSendMsg), m_ClientSocket);

        m_sendUnLockKey = _unLockKey;
        m_sendLock      = true;

        return(true);
    }
示例#2
0
    //发送协议
    public bool sendCmd(BaseCmd _cmd)
    {
        if (!m_isConnected)
        {
            Debug.LogErrorFormat("当前未连接服务器! {0}", DateTime.Now);
        }

        if (_cmd.getSize() <= 0 || !m_isConnected || m_sendLock)
        {
            Debug.LogWarning("send field");
            return(false);
        }

        // Debug.Log("[SEND] cmd : " + _cmd.Cmd.ToString() + " para :" + _cmd.Para.ToString() + " msgLength : " + _cmd.getSize().ToString());

        int _message_length = _cmd.getSize();
        //BytesBuffer _sendBuf = new BytesBuffer (_message_length);
        BytesBuffer _sendBuf = _cmd.sendBuffer;

        _sendBuf.resetPosition();
        _sendBuf.writeInt32(_message_length - 4);

        //System.Buffer.BlockCopy (_cmd.getBuffer.bytes, 0, _sendBuf.bytes, 4, _cmd.getSize());

        if (_sendAsyncCallback == null)
        {
            _sendAsyncCallback = new AsyncCallback(_onSendMsg);
        }
        //m_ClientSocket.Client.BeginSend (_sendBuf.bytes, 0, _sendBuf.bytes.Length, SocketFlags.None, _sendAsyncCallback, m_ClientSocket);

        m_ClientSocket.Client.Send(_sendBuf.bytes, 0, _message_length, SocketFlags.None);

        /* 断包测试
         * int bl = 5;
         * m_ClientSocket.Client.Send (_sendBuf.bytes, 0, _sendBuf.bytes.Length - bl, SocketFlags.None);
         * System.Threading.Thread.Sleep (1000);
         * m_ClientSocket.Client.Send (_sendBuf.bytes, bl, bl, SocketFlags.None);
         * ///*/
        return(true);
    }