public void SendMsg(ProtoBuf.IExtensible pMsg, Int32 n32MsgID) { if (m_Client != null) { //清除stream mSendStream.SetLength(0); mSendStream.Position = 0; //序列到stream ProtoBuf.Serializer.Serialize(mSendStream, pMsg); CMsg pcMsg = new CMsg((int)mSendStream.Length); pcMsg.SetProtocalID(n32MsgID); pcMsg.Add(mSendStream.ToArray(), 0, (int)mSendStream.Length); //ms.Close(); #if UNITY_EDITOR #else try { #endif #if LOG_FILE && UNITY_EDITOR if (n32MsgID != 8192 && n32MsgID != 16385) { string msgName = ""; if (Enum.IsDefined(typeof(GCToBS.MsgNum), n32MsgID)) { msgName = ((GCToBS.MsgNum)n32MsgID).ToString(); } else if (Enum.IsDefined(typeof(GCToCS.MsgNum), n32MsgID)) { msgName = ((GCToCS.MsgNum)n32MsgID).ToString(); } else if (Enum.IsDefined(typeof(GCToLS.MsgID), n32MsgID)) { msgName = ((GCToLS.MsgID)n32MsgID).ToString(); } else if (Enum.IsDefined(typeof(GCToSS.MsgNum), n32MsgID)) { msgName = ((GCToSS.MsgNum)n32MsgID).ToString(); } using (System.IO.StreamWriter sw = new System.IO.StreamWriter(@"E:\Log.txt", true)) { sw.WriteLine(Time.time + " 发送消息:\t" + n32MsgID + "\t" + msgName); } } #endif m_Client.GetStream().Write(pcMsg.GetMsgBuffer(), 0, (int)pcMsg.GetMsgSize()); #if UNITY_EDITOR #else } catch (Exception exc) { Debugger.LogError(exc.ToString()); Close(); } #endif } }
void CheckHandlerByIndex(int index, ProtoBuf.IExtensible msg) { if (_handlerDic.ContainsKey(index)) { _handlerDic[index](msg); _handlerDic.Remove(index); } }
public void request(string route, ProtoBuf.IExtensible msg, Action <Message> action) { this.eventManager.AddCallBack(reqId, action); byte[] bytes = EntityObject.Serialize(msg); protocol.send(route, reqId, bytes); reqId++; }
public static byte[] Serialize(ProtoBuf.IExtensible proto) { using (var stream = new MemoryStream()) { Serializer.Serialize(stream, proto); stream.Flush(); return(stream.ToArray()); } }
/// <summary> /// Sends the message to sever. /// TODO:设置bReliable为true,超时后可以重传该请求. /// </summary> /// <param name="shMsgID">Sh message I.</param> /// <param name="data">Data.</param> /// <param name="bReliable">If set to <c>true</c> b reliable.</param> public static void sendMsgSever(int shMsgID, ProtoBuf.IExtensible data) { SingleDkReqProto drp = new SingleDkReqProto(); drp.head = new proto_header(); drp.head.shMsgID = shMsgID; drp.req = data; Log.info("SingleDkReqProto", "[SingleDkReqProto][sendMsgSever] Send Message. ID " + (GameCmdCode)shMsgID); drp.request(); }
private static byte[] ProtobufSerizer(ProtoBuf.IExtensible data) { using (var m = new MemoryStream()) { Serializer.Serialize(m, data); m.Position = 0; var length = (int)m.Length; var buffer = new byte[length]; m.Read(buffer, 0, length); return(buffer); } }
private const int HEADER_SIZE = 8;//(2 stype 2 ctype 4 utag)8+msg.body //将msg对象序列化为字节流 private static byte[] protobuf_serializer(ProtoBuf.IExtensible data) { using (MemoryStream m = new MemoryStream()) { byte[] buffer = null; Serializer.Serialize(m, data); m.Position = 0; int length = (int)m.Length; buffer = new byte[length]; m.Read(buffer, 0, length); return(buffer); } }
/// <summary> /// Sends the message to sever.P2P Message. /// NetWork retransmit. /// </summary> /// <param name="shMsgID">Sh message I.</param> /// <param name="data">Data.</param> /// <param name="bReliable">If set to <c>true</c> b reliable.</param> public static void sendMsgUdp(int shMsgID, ProtoBuf.IExtensible data) { //Must set self-playerid. SingleDkReqProto drp = new SingleDkReqProto(); drp.head = new proto_header(); drp.head.shMsgID = shMsgID; drp.req = data; drp.isP2P = true; Log.info("SingleDkReqProto", "[SingleDkReqProto][sendMsgUdp] Send Message. ID " + (GameCmdCode)shMsgID); drp.request(); }
public void AsynSendMessage(gNetMsgType msgType, ProtoBuf.IExtensible data) { gNetMsg msg = new gNetMsg() { type = msgType, content = gPB.pbEncode(data), }; msg.size = msg.content.Length; lock (sendQueue) { sendQueue.Enqueue(msg); } }
private byte[] Serial(ProtoBuf.IExtensible data)//将SocketModel转化成字节数组 { using (MemoryStream m = new MemoryStream()) { byte[] buffer = null; Debug.Log("Serial----data" + data); Serializer.Serialize(m, data); m.Position = 0; int length = (int)m.Length; buffer = new byte[length]; m.Read(buffer, 0, length); return(buffer); } }
public static C2S_Proto_Type GetProtoType(ProtoBuf.IExtensible proto) { PropertyInfo property = proto.GetType().GetProperty("type_t"); if (null == property) { Console.WriteLine("Protocol {0} does not have memeber 'type-t'", proto); return(C2S_Proto_Type.Proto_Stuff_Client_Begin); } String value = property.GetValue(proto, null).ToString(); C2S_Proto_Type type = C2S_Proto_Type.Proto_Stuff_Client_Begin; Enum.TryParse(value, out type); return(type); }
public static byte[] pack_protobuf_cmd(int stype, int ctype, ProtoBuf.IExtensible msg) { int cmd_len = HEADER_SIZE; byte[] cmd_body = null; if (msg != null) { cmd_body = protobuf_serializer(msg); cmd_len += cmd_body.Length; } byte[] cmd = new byte[cmd_len]; data_viewer.write_ushort_le(cmd, 0, (ushort)stype); data_viewer.write_ushort_le(cmd, 2, (ushort)ctype); if (cmd_body != null) { data_viewer.write_bytes(cmd, HEADER_SIZE, cmd_body); } return(cmd); }
public static byte[] Encode_Protobuf(int stype, int ctype, ProtoBuf.IExtensible body) { int Header_Len = 8; byte[] byte_array = null; int body_len = 0; if (body != null) { byte_array = Serialize(body); body_len = byte_array.Length; } byte[] Encode_Bytes = new byte[Header_Len + body_len]; BytesWriter.Write_short_to_bytes(Encode_Bytes, 0, (short)stype); BytesWriter.Write_short_to_bytes(Encode_Bytes, 2, (short)ctype); if (body != null) { BytesWriter.Write_Byte_to_bytes(byte_array, 0, Encode_Bytes, 8); } return(Encode_Bytes); }
static byte[] Serialize(ProtoBuf.IExtensible data) { try { using (MemoryStream m = new MemoryStream()) { byte[] buffer = null; Serializer.Serialize(m, data); m.Position = 0; int length = (int)m.Length; buffer = new byte[length]; m.Read(buffer, 0, length); return(buffer); } } catch (Exception ex) { Debug.Log("序列化失败: " + ex.ToString()); return(null); } }
/// <summary> /// 打包Protobuf /// </summary> /// <param name="serviceType"></param> /// <param name="cmdType"></param> /// <param name="msg"></param> /// <returns></returns> public static byte[] PackageProtobuf(int serviceType, int cmdType, ProtoBuf.IExtensible msg) { var len = HeadSize; byte[] body = null; if (msg != null) { body = ProtobufSerizer(msg); len += body.Length; } var cmdPackage = new byte[len]; cmdPackage.WriteUShortLe((ushort)serviceType); cmdPackage.WriteUShortLe((ushort)cmdType, 2); if (body != null) { cmdPackage.WriteBytes(body, HeadSize); } return(cmdPackage); }
static void Main(string[] args) { if (args.Length != 2) { Console.WriteLine(@"Usage: proto_conv.exe -en <xlsx path>"); return; } else { if (args[0].Equals("-ch") || args[0].Equals("-en") || args[0].Equals("-ct") || args[0].Equals("-ko")) { } else { Console.WriteLine("语言参数错误!"); return; } string language = args[0].ToLower().Substring(1); Console.WriteLine(@"language:" + language); string inputFilename = Path.GetFullPath(args[1]); string inputDirName = Path.GetDirectoryName(inputFilename); string outputPath = inputDirName; string part_name = args[1].ToLower(); string format = ""; if (args[1].EndsWith(".xlsx")) { format = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0 XML;HDR=YES;IMEX=1;'"; } else if (args[1].EndsWith(".xls")) { format = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES;IMEX=1'"; } else { Console.WriteLine("File Type Error!"); return; } string cs = string.Format(format, inputFilename); OleDbConnection CNN = new OleDbConnection(cs); CNN.Open(); System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(protoGenWorkDir); if (di.Exists) { di.Delete(true); } di.Create(); DataTable ttable = CNN.GetSchema("Tables"); foreach (DataRow row in ttable.Rows) { if (row["Table_Type"].ToString() == "TABLE") { string tabName = row["Table_Name"].ToString(); //Console.WriteLine(tabName); if (tabName.ToLower().Contains("description") || tabName.Contains("#_FilterDatabase") || !tabName.ToLower().Contains("config")) { continue; } OleDbDataAdapter adpt = new OleDbDataAdapter("select * from [" + tabName + "]", CNN); DataTable dt = new DataTable(); adpt.Fill(dt); if (dt.Rows.Count == 0) { continue; } tabName = tabName.Replace("$", ""); //Console.WriteLine("gen proto..." + tabName); Dictionary <string, FieldInfo> fieldInfo = new Dictionary <string, FieldInfo>(); string s = GenProto(dt, tabName, fieldInfo, language); string protoFilename = di.FullName + "\\" + tabName + ".proto"; StreamWriter sw = File.CreateText(protoFilename); sw.Write(s); sw.Close(); string outFilename = inputDirName + "\\code\\" + tabName + ".cs"; string param = string.Format("-i:\"{0}\" -o:\"{1}\" -ns:m", protoFilename, outFilename); ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = protoGenPath; psi.Arguments = param; psi.WorkingDirectory = protoGenWorkDir; psi.UseShellExecute = false; Process proc = System.Diagnostics.Process.Start(psi); proc.WaitForExit(); string code = File.ReadAllText(outFilename); Assembly lib = CompileCS(code); string typename = "m." + tabName; Type t = lib.GetType(typename); PropertyInfo[] properties = t.GetProperties(); FileStream fs = new FileStream(outputPath + "\\protodata\\" + tabName + ".bytes", FileMode.Create); BinaryWriter bw = new BinaryWriter(fs); //bw.Write(t.FullName); //bw.Write((uint)dt.Rows.Count); List <ProtoBuf.IExtensible> outdata = new List <ProtoBuf.IExtensible>(); System.Reflection.ConstructorInfo ci = t.GetConstructor(Type.EmptyTypes); int rowCount = 0; foreach (DataRow dr in dt.Rows) { ProtoBuf.IExtensible o = ci.Invoke(new object[0]) as ProtoBuf.IExtensible; foreach (PropertyInfo pi in properties) { FieldInfo fi = fieldInfo[pi.Name]; string valueString = dr[fi.m_columnName].ToString(); try { object v = GetValue(fi, valueString); pi.SetValue(o, v, BindingFlags.SetProperty, null, null, null); //Console.WriteLine(string.Format(" 行: {0}, 列: {1}", rowCount + 2, fi.m_desc)); } catch (Exception ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(string.Format("{0}, 行: {1}, 列: {2}", ex.Message, rowCount + 2, fi.m_desc)); Console.ResetColor(); } } outdata.Add(o); rowCount++; //ProtoBuf.Serializer.NonGeneric.SerializeWithLengthPrefix(fs, o, ProtoBuf.PrefixStyle.Fixed32, 0); } Serializer.Serialize <List <ProtoBuf.IExtensible> >(fs, outdata); fs.Close(); } } CNN.Close(); } }
/// <summary> /// 以ProtoBuf方式发送 /// </summary> /// <param name="_protocalType"></param> /// <param name="data"></param> public void SendMsg(eProtocalCommand _protocalType, ProtoBuf.IExtensible data) { SendMsgBase(_protocalType, ProtoBuf_Serializer(data)); }
/// <summary> /// 以ProtoBuf方式发送 /// </summary> /// <param name="_protocalType"></param> /// <param name="data"></param> public void SendMsg(UInt16 _protocalType, ProtoBuf.IExtensible data) { SendMsgBase(_protocalType, SerializePb(data)); }
public void SendMsg(LobbyLogicPro _protocalType, ProtoBuf.IExtensible data) { SendMsgBase((ushort)_protocalType, ProtoBuf_Serializer(data)); }
public abstract void ExecuteMessage(ProtoBuf.IExtensible message, RpcSession session);