Exemplo n.º 1
0
    private void OnReceiveRestData(IAsyncResult iar)
    {
        StateObj state = (StateObj)iar.AsyncState;
        int      len   = state.Client.EndReceive(iar);

        byte[] buff = state.RecvTempBuff;
        //  state.RecvBuff.addRange<byte>(buff , 0 , len);
        state.RecvBuff.Write(buff, 0, len);
        int contentRest = state.RecvLen - state.RecvBuff.Length;

        if (contentRest <= 0)
        {
            state.OnReceive(state);
            return;
        }
        try
        {
            state.Client.BeginReceive(state.RecvTempBuff, 0, state.RecvTempBuff.Length, 0, new AsyncCallback(OnReceiveRestData), state);
        }
        catch (Exception)
        {
            state.OnRecvError(state);
            UITools.log("receive data timeout....");
        }
    }
Exemplo n.º 2
0
    private void OnReceive(IAsyncResult iar)
    {
        StateObj state = (StateObj)iar.AsyncState;
        //读取的消息总长度
        int len = state.Client.EndReceive(iar);

        if (len < 4)
        {
            UITools.logError("invaild msg");
            state.OnRecvError(state);
            return;
        }
        byte[] buff       = state.RecvTempBuff;
        int    msgRealLen = MsgUtils.DecodeMsgRealLen(buff, 0, 4);

        UITools.log("msg real length : " + msgRealLen);
        state.RecvLen = msgRealLen;
        // state.RecvBuff.addRange<byte>(buff, AppConst.MsgHeadLen, len - 4);
        state.RecvBuff.Write(buff, AppConst.MsgHeadLen, len - AppConst.MsgHeadLen);

        if (state.RecvLen - state.RecvBuff.Length > 0)
        {
            try
            {
                state.Client.BeginReceive(state.RecvTempBuff, 0, state.RecvTempBuff.Length, 0, new AsyncCallback(OnReceiveRestData), state);
            }
            catch (Exception)
            {
                state.OnRecvError(state);
                UITools.log("receive data timeout....");
            }
        }
        else
        {
            state.OnReceive(state);
        }
    }
Exemplo n.º 3
0
    private void OnSendCallBack(IAsyncResult iar)
    {
        StateObj state = (StateObj)iar.AsyncState;

        state.OnSend(state);
        if (state.IsNeedRecv)
        {
            try
            {
                state.Client.BeginReceive(state.RecvTempBuff, 0, state.RecvTempBuff.Length, 0, new AsyncCallback(OnReceive), state);
            }
            catch (Exception)
            {
                state.OnRecvError(state);
            }
        }
        else
        {
            state.OnReceive(state);
        }
    }