예제 #1
0
        private void assembleData(BufferState bufState)
        {
            //do

            if (!bufState.hasRemaing)
            {
                try
                {
                    //Console.WriteLine("开始接收数据,");
                    bufState.remaining = client.Receive(bufState.buf);
                    //Console.WriteLine("接收数据完成");
                    //Console.WriteLine("remaining: "+bufState.remaining);
                    bufState.hasRemaing = true;
                }
                catch
                {
                    disConnect();
                    updateInfo("接收数据异常,退出Socket连接", MyConfig.INT_UPDATEUI_TXBOX);
                    bufState.init();
                    return;
                }
            }
            if (bufState.hasHead)
            {
                //Console.WriteLine("remaining : " + bufState.remaining);
                while (bufState.remaining < MyConfig.INT_PACKAGE_HEAD_LEN)
                {
                    //复制下一个buf的头部;
                    MyUtil.copyArray(bufState.buf, bufState.bufPos, bufState.buf, 0, bufState.remaining);
                    //接收数据组合正常头部包;
                    Int32 size = client.Receive(bufState.buf, bufState.remaining, MyConfig.INT_SOCKET_BUFFER_SIZE - bufState.remaining, SocketFlags.None);
                    bufState.remaining = bufState.remaining + size;
                    bufState.bufPos = 0;
                    bufState.tempPos = 0;
                }
                byte headFlag = decode.getHeadFlag(bufState.buf, bufState.bufPos);
                byte dataType = decode.getDataType(bufState.buf, bufState.bufPos);
                byte reqType = decode.getReqType(bufState.buf, bufState.bufPos);
                if (headFlag == MyConfig.PROTOCOL_HEAD_FLAG && reqType > 0 && reqType < 8)
                {
                    bufState.target = decode.getPackLen(bufState.buf, bufState.bufPos);
                    if (bufState.target < MyConfig.INT_PACKAGE_HEAD_LEN || bufState.target >= Int32.MaxValue)
                    {
                        Console.WriteLine("bufState.target err : " + bufState.target);
                        bufState.drop();
                    }
                    else
                    {
                        bufState.isRightPackage = true;
                        bufState.pending = bufState.target;
                        bufState.temp = new byte[bufState.target];
                    }
                }
                else
                {
                    Console.WriteLine("dataType err : " + dataType);
                    bufState.drop();
                }
            }

            if (bufState.pending == bufState.remaining && bufState.isRightPackage == true)
            {
                MyUtil.copyArray(bufState.buf, bufState.bufPos, bufState.temp, bufState.tempPos, bufState.remaining);
                byte reqType = decode.getReqType(bufState.temp, 0);
                String token = decode.getToken(bufState.temp);
                byte[] result = decode.getData(bufState.temp);
                //Console.WriteLine("temp: " + bufState.temp.Length);
                coreProccessing(reqType, token, result);
                bufState.init();
            }

            if (bufState.pending < bufState.remaining && bufState.isRightPackage == true)
            {
                MyUtil.copyArray(bufState.buf, bufState.bufPos, bufState.temp, bufState.tempPos, bufState.pending);
                byte reqType = decode.getReqType(bufState.temp, 0);
                String token = decode.getToken(bufState.temp);
                byte[] result = decode.getData(bufState.temp);
                //Console.WriteLine("temp:  " + bufState.temp.Length);
                coreProccessing(reqType, token, result);
                bufState.skip();
            }
            if (bufState.pending > bufState.remaining && bufState.isRightPackage == true)
            {
                MyUtil.copyArray(bufState.buf, bufState.bufPos, bufState.temp, bufState.tempPos, bufState.remaining);
                //数据没有接收完整,重新接收数据
                //Console.WriteLine("temp: " + bufState.temp.Length);
                bufState.connect();
            }
        }
예제 #2
0
 public void startRecive()
 {
     BufferState bufferState = new BufferState();
     bufferState.init();
     while (isConn)
     {
         assembleData(bufferState);
     }
     bufferState.init();
     Console.WriteLine("您的网络已断开,退出数据接收线程");
     updateInfo("您的网络已断开,退出数据接收线程", MyConfig.INT_UPDATEUI_TXBOX);
 }