private static PARSE_RESULT ParseString(Context c, Value v) { string str = null; PARSE_RESULT ret = ParseString_Internal(c, ref str); if (ret == PARSE_RESULT.OK) { v.Str = str; } return(ret); }
protected bool parseThread() { if (mInputDataSize > 0) { mInputBufferLock.waitForUnlock(); // 首先解析包头 PacketHeader header = new PacketHeader(GameDefine.REPORT_IN); PARSE_RESULT result = header.parseData(mInputBuffer, mInputDataSize); // 解析失败,则将缓冲区清空 if (result == PARSE_RESULT.PR_ERROR) { clearInputBuffer(); } // 数据不足,继续等待接收数据 else if (result == PARSE_RESULT.PR_NOT_ENOUGH) { ; } // 解析成功,判断包类型,继续解析 else if (result == PARSE_RESULT.PR_SUCCESS) { SerialPortPacket packet = createPacket(mInputBuffer[header.mHeaderLength]); if (packet != null) { int offset = 0; packet.parseData(header, mInputBuffer, mInputDataSize, ref offset); // 解析数量与包头不一致,清空缓冲区 if (offset != header.mHeaderLength + header.mPayloadLength) { clearInputBuffer(); } // 解析正确,移除已解析的数据 else { removeDataFromInputBuffer(0, offset); // 加入接收的消息包列表 mReceivedPacketLock.waitForUnlock(); if (mReceivedPacket.Count < MAX_RECEIVE_PACKET_COUNT) { mReceivedPacket.Add(packet); } mReceivedPacketLock.unlock(); } mLastPacketTime = DateTime.Now; } else { clearInputBuffer(); } } mInputBufferLock.unlock(); } return(true); }
// 接收Socket消息 protected void receiveSocket(ref bool run) { if (mServerSocket != null && mServerSocket.Connected && mConnectState == CONNECT_STATE.CS_CONNECTED) { try { (mRemoteEndPoint as IPEndPoint).Address = IPAddress.Any; (mRemoteEndPoint as IPEndPoint).Port = 0; int nRecv = mServerSocket.ReceiveFrom(mRecvBuff, ref mRemoteEndPoint); if (nRecv <= 0) { // 由于需要保证状态正确,所以此处需要立即调用设置状态 notifyDisconnectServer(); // 服务器异常 setNetState(NET_STATE.NS_SERVER_CLOSE); return; } mInputBuffer.addData(mRecvBuff, nRecv); // 解析接收到的数据 while (true) { PARSE_RESULT parseResult = parsePacket(); // 数据解析成功,继续解析 if (parseResult != PARSE_RESULT.PR_SUCCESS) { break; } } } catch (SocketException e) { // 由于需要保证状态正确,所以此处需要立即调用设置状态 notifyDisconnectServer(); // 本地网络异常 NET_STATE state = NET_STATE.NS_NET_CLOSE; if (e.ErrorCode == 10051) { state = NET_STATE.NS_NET_CLOSE; } else if (e.ErrorCode == 10061) { state = NET_STATE.NS_SERVER_CLOSE; } setNetState(state); } } }
public static PARSE_RESULT Parse(Value v, string json) { if (v == null || json == null) { throw new ArgumentNullException(); } Context c = new Context(json); ParseWhiteSpace(c); PARSE_RESULT ret = ParseValue(c, v); if (ret == PARSE_RESULT.OK) { ParseWhiteSpace(c); if (c.index != c.json.Length) { v.type = JSON_TYPE.NULL; return(PARSE_RESULT.ROOT_NOT_SINGULAR); } } return(ret); }