public Proto.BaseData toProtoBuf() { Proto.BaseData bsData = new Proto.BaseData { }; if (mIsBaseObject == false) { bsData.MIsBaseObject = false; if (mValue == null) { bsData.MValue = new Proto.BaseData.Types.BaseValue { MType = 0 }; return(bsData); } else { if (mValue is int) { bsData.MValue = new Proto.BaseData.Types.BaseValue { BvInt = (int)mValue, MType = 1 }; } else if (mValue is float) { bsData.MValue = new Proto.BaseData.Types.BaseValue { BvFloat = (float)mValue, MType = 2 }; } else if (mValue is double) { bsData.MValue = new Proto.BaseData.Types.BaseValue { BvDouble = (double)mValue, MType = 3 }; } else if (mValue is String) { bsData.MValue = new Proto.BaseData.Types.BaseValue { BvString = (String)mValue, MType = 4 }; } else if (mValue is bool) { bsData.MValue = new Proto.BaseData.Types.BaseValue { BvBool = (bool)mValue, MType = 5 }; } else if (mValue is long) { bsData.MValue = new Proto.BaseData.Types.BaseValue { BvLong = (long)mValue, MType = 6 }; } else if (mValue is short) { bsData.MValue = new Proto.BaseData.Types.BaseValue { BvInt = (int)mValue, MType = 1 }; } return(bsData); } } else { bsData.MIsBaseObject = true; foreach (var item in mObject) { BaseData value = item.Value; Object key = item.Key; Proto.BaseData.Types.BaseValue keyData = new Proto.BaseData.Types.BaseValue { }; if (key is String) { keyData.BvString = ((String)key); keyData.MType = 4; } else if (key is int) { keyData.BvInt = ((int)key); keyData.MType = 1; } else { continue; } Proto.BaseData.Types.ObjectType objData = new Proto.BaseData.Types.ObjectType { }; objData.MKey = keyData; objData.MValue = value.toProtoBuf(); bsData.MObject.Add(objData); } } return(bsData); }
public void OnReceive(IAsyncResult ar) { Socket ts = (Socket)ar.AsyncState; int bytesRead = bytes.Length; bytesRead = ts.EndReceive(ar); if (bytesRead > 0) { if (surplusBuffer == null) { surplusBuffer = new byte[bytesRead]; Buffer.BlockCopy(bytes, 0, surplusBuffer, 0, bytesRead); } else { byte[] tempByte = new byte[bytesRead]; Buffer.BlockCopy(bytes, 0, tempByte, 0, bytesRead); surplusBuffer = surplusBuffer.Concat(tempByte).ToArray(); } int haveRead = 0; int totalLen = surplusBuffer.Length; while (haveRead <= totalLen) { if (totalLen - haveRead < headSize) { byte[] byteSub = new byte[totalLen - haveRead]; Buffer.BlockCopy(surplusBuffer, haveRead, byteSub, 0, totalLen - haveRead); surplusBuffer = byteSub; totalLen = 0; break; } byte[] headByte = new byte[headSize]; Buffer.BlockCopy(surplusBuffer, haveRead, headByte, 0, headSize); Array.Reverse(headByte); int bodySize = BitConverter.ToInt32(headByte, 0); if (haveRead + headSize + bodySize > totalLen) { byte[] byteSub = new byte[totalLen - haveRead]; Buffer.BlockCopy(surplusBuffer, haveRead, byteSub, 0, totalLen - haveRead); surplusBuffer = byteSub; break; } else { BaseData bsdata = null; if (bodySize >= 0) { Proto.BaseData bd = Proto.BaseData.Parser.ParseFrom(surplusBuffer, haveRead + headSize, bodySize); bsdata = BaseData.bytetoBaseData(bd); } haveRead = haveRead + headSize + bodySize; if (headSize + bodySize == bytesRead) { surplusBuffer = null; totalLen = 0; } recvEvent.onRecv(bsdata); } } Array.Clear(bytes, 0, bytes.Length); ts.BeginReceive(bytes, 0, bytes.Length, SocketFlags.None, new AsyncCallback(OnReceive), ts); } }
public void onRecv(BaseData bsd) { Console.WriteLine("onRecv:"); Console.WriteLine(bsd.ToString()); Console.WriteLine("onRecv:"); }
public void onSend(IAsyncResult ar) { BaseData bd = (BaseData)ar.AsyncState; Console.WriteLine("basenettcp onSend sunncess " + bd.ToString()); }
public void onSend(IAsyncResult ar) { BaseData bd = (BaseData)ar.AsyncState; Debug.Log("basenettcp onSend sunncess " + bd.ToString()); }