Example #1
0
    /// <summary>
    /// 发送消息
    /// </summary>
    public void StartSendMessage <T>(T ProtocolPacket, int ProtocolType) where T : IMessage
    {
        if (!IsConnected)
        {
            return;
        }

        MsgCls msgcls = new MsgCls();

        msgcls.ObjectToByte <T>(ProtocolPacket, ProtocolType);

        AsyncSendMsg(msgcls);
    }
Example #2
0
    public void AsyncSendMsg(MsgCls msgcls, int offset = 0)
    {
        if (!IsConnected)
        {
            return;
        }

        try
        {
            socket.BeginSend(msgcls.byteBuff, offset, msgcls.send_num, 0, new AsyncCallback(SendMessage), msgcls);
        }
        catch (Exception e)
        {
            Debug.LogError(ClaName + " " + e.ToString() + "SendMessageInAsync");
        }
    }
Example #3
0
    public void SendMessage(IAsyncResult asyncSend)
    {
        MsgCls msgcls = (MsgCls)asyncSend.AsyncState;

        try
        {
            int send_num = socket.EndSend(asyncSend);
            if (send_num < msgcls.send_num)                          //异步发包的时候没有一次性的发完
            {
                msgcls.send_num -= send_num;                         //再次发包时要发的字节数
                AsyncSendMsg(msgcls, send_num);
            }
        }
        catch (Exception e)
        {
            Debug.LogError(ClaName + " " + e.ToString() + " EndSendException");
        }
    }