Exemplo n.º 1
0
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="clientSocket"></param>
        /// <param name="messageBase"></param>
        public static void Send(ClientSocket clientSocket, MessageBase messageBase)
        {
            if (clientSocket == null || clientSocket.Socket.Connected == false)
            {
                return;
            }

            try
            {
                // 分为三部分,协议头(总协议长度);协议名称;协议类容
                byte[] nameBytes = MessageBase.EncodeName(messageBase);
                byte[] bodyBytes = MessageBase.Encode(messageBase);
                int    len       = nameBytes.Length + bodyBytes.Length;

                byte[] byteHead  = BitConverter.GetBytes(len);
                byte[] sendBytes = new byte[byteHead.Length + len];

                Array.Copy(byteHead, 0, sendBytes, 0, byteHead.Length);
                Array.Copy(nameBytes, 0, sendBytes, byteHead.Length, nameBytes.Length);
                Array.Copy(bodyBytes, 0, sendBytes, byteHead.Length + nameBytes.Length, bodyBytes.Length);

                try
                {
                    clientSocket.Socket.BeginSend(sendBytes, 0, sendBytes.Length, 0, null, null);
                }
                catch (Exception ex)
                {
                    Debug.LogError("Socket Begin Send Error :" + ex);
                }
            }
            catch (Exception ex)
            {
                Debug.LogError("Socket Send Mesaage Fail :" + ex);
            }
        }
Exemplo n.º 2
0
    /// <summary>
    /// 发送数据到服务器
    /// </summary>
    /// <param name="messageBase"></param>
    public void SendMessage(MessageBase messageBase)
    {
        if (m_Socket == null && m_Socket.Connected == false)
        {
            return;
        }

        if (m_Contecting)
        {
            Debug.LogError("正在连接服务器中,无法发送消息给服务器");
            return;
        }

        if (m_Closing)
        {
            Debug.LogError("正在关闭连接中,无法发送消息给服务器");
            return;
        }

        try
        {
            byte[] nameBytes = MessageBase.EncodeName(messageBase);
            byte[] bodyBytes = MessageBase.Encode(messageBase);
            int    len       = nameBytes.Length + bodyBytes.Length;
            byte[] byteHead  = BitConverter.GetBytes(len);
            byte[] sendBytes = new byte[byteHead.Length + len];

            Array.Copy(byteHead, 0, sendBytes, 0, byteHead.Length);
            Array.Copy(nameBytes, 0, sendBytes, byteHead.Length, nameBytes.Length);
            Array.Copy(bodyBytes, 0, sendBytes, byteHead.Length + nameBytes.Length, bodyBytes.Length);
            ByteArray ba = new ByteArray(sendBytes);



            int count = 0;

            // 添加消息到队列
            lock (m_WriteQueue)
            {
                m_WriteQueue.Enqueue(ba);

                count = m_WriteQueue.Count;
            }

            if (count == 1)
            {
                m_Socket.BeginSend(sendBytes, 0, sendBytes.Length, 0, SendCallback, m_Socket);
            }
        }
        catch (Exception ex)
        {
            Debug.LogError("SendMessage Error : " + ex);
            Close();
        }
    }