void SendMobilePollRequest(string DeviceID, int MsgId) { try { request = (HttpWebRequest)HttpWebRequest.Create(conf.CPassServer + "/Api/Authorization/CpassAuth1Poll"); request.ContentType = "application/json; charset=utf-8"; request.Accept = "application/json, text/javascript, */*"; request.Method = "POST"; request.Headers.Add("Authorization", "Bearer " + conf.CPassKey); using (StreamWriter writer = new StreamWriter(request.GetRequestStream())) { writer.Write(@"{""requestId"": """ + MsgID.ToString() + @""",""dateTime"": """ + dt + @""",""cpass_UTN"":""" + DeviceID + @""",""tagid"": """ + DeviceID + @""",""pumP_ID"": ""1"",""fuel_Type_code"": ""0"",""nR_ID"": ""101"",""odometer"": ""0"",""engine_Hours"": ""0""}"); } request.BeginGetResponse(new AsyncCallback(FinishWebRequest), null); } catch (Exception ex) { myLog.Log(ex.Message); } }
/// <summary> /// 当收到网络消息 /// </summary> /// <param name='cMsgType'>消息类型</param> /// <param name='pMsg'>消息数据</param> /// <param name='len'>数据长度</param> void OnNetMessage(ushort cMsgType, byte[] pMsg, int len) { switch (cMsgType) { case NetMsg.CLIENT_SERVER_RES: DEBUG.Networking(DEBUG.RECV_MSG_LOG + MsgID.ToString(cMsgType)); HanldeGateSeverRes(pMsg, len); break; default: DEBUG.Networking(DEBUG.TRACER_LOG + "未处理的接收消息类型:" + MsgID.ToString(cMsgType), LogType.Warning); break; } NetworkMgr.Instance.OnNetMessage(cMsgType); }
public int send(byte[] data, bool bEncrypt) { int ir = -3; if (null != data) { try { string s = MsgID.ToString(data[1]); s += (" 长度: " + data.Length); s += " 数据:"; for (int i = 0; i < data.Length; ++i) { s += (" " + data[i].ToString("X2")); } ////// TODO 查看发送十六进制数据(底层发送十六进制消息) if (MahjongLobby_SX_Changzhi.LobbyContants.isOpenDebugMessage_Send) { Debug.Log("+++++++++++++++++++++" + s); } //DEBUG.NetworkClient(data[1], data); byte[] pMsg = (bEncrypt) ? AESCrypt.Encrypt(data, AESCrypt.KEY) : data; if (null != pMsg) { short iMsgLen = (short)(pMsg.Length + 4); byte[] msg = new byte[iMsgLen]; msg[0] = PACKAGE_IDENTIFY; msg[1] = (byte)((bEncrypt) ? 0 : 1); iMsgLen = IPAddress.HostToNetworkOrder(iMsgLen); byte[] len = BitConverter.GetBytes(iMsgLen); Array.Copy(len, 0, msg, 2, len.Length); Array.Copy(pMsg, 0, msg, 4, pMsg.Length); ir = send(msg); } } catch (Exception e) { DEBUG.Networking(DEBUG.TRACER_LOG + "NetConnection.send, exp: " + e.Message, LogType.Exception); } } return(ir); }
protected override bool _RecvPack(ref List <Pack> outPacks, Stream stream) { lock (stream) { try { stream.Seek(0, SeekOrigin.Begin); while (true) { Pack inPack = new Pack(); inPack.headerBuffer = new byte[Pack.HeaderLength]; int read = stream.Read(inPack.headerBuffer, 0, Pack.HeaderLength); if (read == 0) { break; } inPack.ParseHeader(); inPack.buffer = new byte[inPack.Length]; read = stream.Read(inPack.buffer, 0, inPack.buffer.Length); decrypt(inPack); #if UNITY_EDITOR MsgID msg_id = (MsgID)inPack.MessageID; if (!MsgHandler.m_noLogMsg.Contains(msg_id)) { Debug.Log("-------_RecvPack with MessageID11: " + msg_id.ToString()); } #endif outPacks.Add(inPack); } stream.Seek(0, SeekOrigin.Begin); stream.SetLength(0); } catch (Exception exp) { m_curBufferPos = 0; Debug.LogError("network conn exception: " + exp.Message); stream.Seek(0, SeekOrigin.Begin); stream.SetLength(0); return(false); } } //Debug.Log("outPacks count " + outPacks.Count); return(outPacks.Count != 0); }
/// <summary> /// 记录接收服务器的消息数据 /// </summary> /// <param name='id'> /// 消息类型 /// </param> /// <param name='bytes'> /// 消息的字节数组 /// </param> public static void NetworkServer(ushort id, byte[] bytes) { if (MsgID.SkipLog(id)) { return; } string s = MsgID.ToString(id); s += (" 长度: " + bytes.Length); s += " 数据:"; for (int i = 0; i < bytes.Length; ++i) { s += (" " + bytes[i].ToString("X2")); } //Debug.Log(s); NetworkServer(s); }
/// <summary> /// 记录客户端发送的消息数据 /// </summary> /// <param name='id'> /// 消息类型 /// </param> /// <param name='bytes'> /// 消息的字节数组 /// </param> public static void NetworkClient(ushort id, byte[] bytes) { if (MsgID.SkipLog(id)) { //return; } string s = MsgID.ToString(id); s += (" 长度: " + bytes.Length); s += " 数据:"; for (int i = 0; i < bytes.Length; ++i) { s += (" " + bytes[i].ToString("X2")); } NetworkClient(s); }
public void SendPack <T>(uint type, T content, MsgID msg_id) where T : ProtoBuf.IExtensible { #if UNITY_EDITOR if (!MsgHandler.m_noLogMsg.Contains(msg_id)) { Debug.Log("-------SendMsg with MessageID: " + msg_id.ToString()); } #endif if (!CanSend()) { Debug.LogError("SendPack failed, Msg: " + msg_id); return; } Pack pack = SerializeToPack(type, content, msg_id); //encrypt(pack); SendPack(pack); }
void SendMobileConfirm(string DeviceID, int MsgId) { try { request = (HttpWebRequest)HttpWebRequest.Create(conf.CPassServer + "/Api/Authorization/CpassAuth1Confirm"); request.ContentType = "application/json; charset=utf-8"; request.Accept = "application/json, text/javascript, */*"; request.Method = "POST"; request.Headers.Add("Authorization", "Bearer " + conf.CPassKey); using (StreamWriter writer = new StreamWriter(request.GetRequestStream())) { writer.Write(@"{""requestId"": """ + MsgID.ToString() + @""",""result"": ""OK""}"); } request.BeginGetResponse(new AsyncCallback(FinishWebRequest), null); } catch (Exception ex) { myLog.Log(ex.Message); } }
private int DecodeBuffer(byte[] buffer, ref List <Pack> outPacks) { Pack pack = null; int processed = 0; try { int restSizeInBuffer = m_totalBytesRead - m_processed; if (mCurPack == null) { mCurPack = new Pack(); if (restSizeInBuffer >= Pack.HeaderLength) { Buffer.BlockCopy(buffer, m_processed, mCurPack.headerBuffer, 0, Pack.HeaderLength); mCurPack.ParseHeader(); mCurPack.buffer = new byte[mCurPack.Length]; mCurPack.curHeaderSize = Pack.HeaderLength; processed += Pack.HeaderLength; } else { Buffer.BlockCopy(buffer, m_processed, mCurPack.headerBuffer, 0, restSizeInBuffer); mCurPack.curHeaderSize = restSizeInBuffer; processed += restSizeInBuffer; Debug.Log("header to combine"); return(processed); } } if (mCurPack.curHeaderSize < Pack.HeaderLength) { int restHeaderToReceive = Pack.HeaderLength - mCurPack.curHeaderSize; if (restSizeInBuffer >= restHeaderToReceive) { Buffer.BlockCopy(buffer, m_processed, mCurPack.headerBuffer, mCurPack.curHeaderSize, restHeaderToReceive); mCurPack.ParseHeader(); mCurPack.buffer = new byte[mCurPack.Length]; mCurPack.curHeaderSize = Pack.HeaderLength; processed += restHeaderToReceive; } else { Buffer.BlockCopy(buffer, m_processed, mCurPack.headerBuffer, mCurPack.curHeaderSize, restSizeInBuffer); mCurPack.curHeaderSize += restSizeInBuffer; processed += restSizeInBuffer; Debug.Log("header to combine"); return(processed); } } int restSizeToReceive = (int)mCurPack.Length - mCurPack.curRecSize; restSizeInBuffer -= processed; if (restSizeInBuffer < 0 || restSizeToReceive < 0) { Debug.LogError("restSizeInBuffer: " + restSizeInBuffer + "restSizeToReceive: " + restSizeToReceive); return(0); } if (restSizeToReceive <= restSizeInBuffer) { Buffer.BlockCopy(buffer, m_processed + processed, mCurPack.buffer, mCurPack.curRecSize, restSizeToReceive); pack = mCurPack; mCurPack = null; decrypt(pack); #if UNITY_EDITOR MsgID msg_id = (MsgID)pack.MessageID; if (!MsgHandler.m_noLogMsg.Contains(msg_id)) { Debug.Log("-------_RecvPack with MessageID22: " + msg_id.ToString()); } #endif outPacks.Add(pack); processed += restSizeToReceive; } else { Buffer.BlockCopy(buffer, m_processed + processed, mCurPack.buffer, mCurPack.curRecSize, restSizeInBuffer); mCurPack.curRecSize += restSizeInBuffer; processed += restSizeInBuffer; Debug.Log("body to combine"); } } catch (Exception exp) { pack = null; mCurPack = null; Debug.LogError("network conn exception: " + exp.Message); return(0); } return(processed); }