public RuleSupNet() { inputCashe = new SyncCashe(); outputCashe = new SyncCashe(); SyncCasheWriter.OutPutCashe = outputCashe; SyncCasheReader.InputCashe = inputCashe; //temp PurviewMgr.IsMainHost = true; // SocketMgr 的管理交给UI控制 //SocketMgr.Initial(); SocketMgr.SetInputCahes(inputCashe); //SocketMgr.ConnectToServer(); //SocketMgr.StartReceiveThread(); }
static public void SetInputCahes(SyncCashe inputCashe) { sInputCashe = inputCashe; }
static public void ReceivePackge() { try { while (true) { if (client.Connected) { byte[] buffer = new byte[stPkgHead.Size]; Stream netStream = client.GetStream(); if (netStream.CanRead) { // 这里要确保每次读一个整包 netStream.Read(buffer, 0, stPkgHead.Size); stPkgHead pkg = (stPkgHead)BytesToStuct(buffer, typeof(stPkgHead)); Console.WriteLine("ReceiveDataPkg:"); Console.WriteLine("Sytle : " + pkg.iSytle); Console.WriteLine("Size : " + pkg.dataSize); int recvLength = 0; byte[] readData = new byte[pkg.dataSize]; while (recvLength < pkg.dataSize) { recvLength += netStream.Read(readData, recvLength, pkg.dataSize - recvLength); } if (recvLength != pkg.dataSize) { throw new Exception("危险:收到包的长度不等于实际包长"); } // 退出信号 if (pkg.iSytle == (int)PACKAGE_SYTLE.EXIT) { Console.WriteLine("read close info"); break; } // 数据 else if (pkg.iSytle == (int)PACKAGE_SYTLE.DATA) { //byte[] readData = new byte[pkg.dataSize]; //netStream.Read(readData, 0, pkg.dataSize); SyncCashe cashe = sInputCashe; MemoryStream memStream = new MemoryStream(readData); //StreamReader SR = new StreamReader(memStream); BinaryReader BR = new BinaryReader(memStream); try { //string temp = ""; // 物体状态同步数据 //temp = SR.ReadLine(); int StaCount = BR.ReadInt32();//int.Parse(temp); for (int i = 0; i < StaCount; i++) { ObjStatusSyncInfo info = new ObjStatusSyncInfo(); //temp = SR.ReadLine(); //Console.WriteLine("ObjName:" + temp); info.objMgPath = BR.ReadString();//temp; //temp = SR.ReadLine(); //Console.WriteLine("StatusName: " + temp); info.statusName = BR.ReadString();//temp; //temp = SR.ReadLine(); //Console.WriteLine("ValueCount: " + temp); int valueCount = BR.ReadInt32(); //int.Parse(temp); info.values = new object[valueCount]; for (int j = 0; j < valueCount; j++) { info.values[j] = ReadObjFromStream(BR); } // 添加Info到缓冲区 Monitor.Enter(cashe); cashe.ObjStaInfoList.Add(info); Monitor.Exit(cashe); //Console.WriteLine("Read Pkg Success!"); } // 事件同步数据 //temp = SR.ReadLine(); int EventCount = BR.ReadInt32(); //int.Parse(temp); for (int i = 0; i < EventCount; i++) { ObjEventSyncInfo info = new ObjEventSyncInfo(); //temp = SR.ReadLine(); info.objMgPath = BR.ReadString();//temp; //temp = SR.ReadLine(); info.EventName = BR.ReadString();// temp; //temp = SR.ReadLine(); int valueCount = BR.ReadInt32();// int.Parse(temp); info.values = new object[valueCount]; for (int j = 0; j < valueCount; j++) { info.values[j] = ReadObjFromStream(BR); } // 添加Info到缓冲区 Monitor.Enter(cashe); cashe.ObjEventInfoList.Add(info); Monitor.Exit(cashe); } // 物体管理同步数据 //temp = SR.ReadLine(); int ObjMgCount = BR.ReadInt32(); //int.Parse(temp); for (int i = 0; i < ObjMgCount; i++) { ObjMgSyncInfo info = new ObjMgSyncInfo(); //temp = SR.ReadLine(); info.objPath = BR.ReadString();//temp; //temp = SR.ReadLine(); info.objMgKind = BR.ReadInt32(); //int.Parse(temp); //temp = SR.ReadLine(); info.objType = BR.ReadString();//temp; //temp = SR.ReadLine(); int valueCount = BR.ReadInt32();// int.Parse(temp); info.args = new object[valueCount]; for (int j = 0; j < valueCount; j++) { info.args[j] = ReadObjFromStream(BR); } // 添加Info到缓冲区 Monitor.Enter(cashe); cashe.ObjMgInfoList.Add(info); Monitor.Exit(cashe); } // 用户自定义数据 //temp = BR.ReadLine(); int UserDefinCount = BR.ReadInt32();// int.Parse(temp); for (int i = 0; i < UserDefinCount; i++) { UserDefineInfo info = new UserDefineInfo(); //temp = SR.ReadLine(); info.infoName = BR.ReadString(); // temp; //temp = SR.ReadLine(); info.infoID = BR.ReadString(); //temp; //temp = SR.ReadLine(); int valueCount = BR.ReadInt32();//int.Parse(temp); info.args = new object[valueCount]; for (int j = 0; j < valueCount; j++) { info.args[j] = ReadObjFromStream(BR); } Monitor.Enter(cashe); cashe.UserDefineInfoList.Add(info); Monitor.Exit(cashe); } } catch (Exception ex) { Console.WriteLine(ex); } finally { memStream.Close(); ////需要清空当前包中剩余内容 //int left = stPakage.TotolSize / sizeof(char) - (int)SR.BaseStream.Position; //char[] temp = new char[left]; //SR.Read(temp, 0, left); } } else { if (pkg.dataSize < 1024 * 10 && pkg.dataSize >= 0) { byte[] temp = new byte[pkg.dataSize]; readData.CopyTo(temp, 0); if (OnReceivePkg != null) { OnReceivePkg(pkg, temp); } } else { Log.Write("包头有问题: type: " + pkg.iSytle + " , size: " + pkg.dataSize); Console.WriteLine("包头有问题: type: " + pkg.iSytle + " , size: " + pkg.dataSize); //throw new Exception("包头有问题啊,我日"); // 包头很可能有问题 } } } } } } catch (SocketException ex) { Console.WriteLine(ex.ToString()); } finally { client.Close(); client = null; } }
static public void SendGameLogicPackge(SyncCashe cash) { try { if (client.Connected) { Stream netStream = client.GetStream(); if (netStream.CanWrite) { // Head stPkgHead head = new stPkgHead(); head.iSytle = (int)PACKAGE_SYTLE.DATA; MemoryStream MStream = new MemoryStream(); //StreamWriter SW = new StreamWriter(MStream); BinaryWriter BW = new BinaryWriter(MStream); // 状态同步数据 BW.Write(cash.ObjStaInfoList.Count); //SW.WriteLine(cash.ObjStaInfoList.Count); foreach (ObjStatusSyncInfo info in cash.ObjStaInfoList) { //SW.WriteLine(info.objMgPath); BW.Write(info.objMgPath); BW.Write(info.statusName); BW.Write(info.values.Length); foreach (object obj in info.values) { WriteObjToStream(BW, obj); } } // 事件同步数据 BW.Write(cash.ObjEventInfoList.Count); foreach (ObjEventSyncInfo info in cash.ObjEventInfoList) { BW.Write(info.objMgPath); BW.Write(info.EventName); BW.Write(info.values.Length); foreach (object obj in info.values) { WriteObjToStream(BW, obj); } } // 物体管理同步数据 BW.Write(cash.ObjMgInfoList.Count); foreach (ObjMgSyncInfo info in cash.ObjMgInfoList) { BW.Write(info.objPath); BW.Write(info.objMgKind); BW.Write(info.objType); BW.Write(info.args.Length); foreach (object obj in info.args) { WriteObjToStream(BW, obj); } } // 用户自定义数据 BW.Write(cash.UserDefineInfoList.Count); foreach (UserDefineInfo info in cash.UserDefineInfoList) { BW.Write(info.infoName); BW.Write(info.infoID); BW.Write(info.args.Length); foreach (object obj in info.args) { WriteObjToStream(BW, obj); } } BW.Flush(); int dataLength = (int)MStream.Length; //int dataLength = (int)SW.BaseStream.Length; byte[] temp = new byte[dataLength]; MStream.Position = 0; MStream.Read(temp, 0, dataLength); head.dataSize = temp.Length; netStream.Write(StructToBytes(head), 0, stPkgHead.Size); netStream.Write(temp, 0, head.dataSize); // 释放资源 BW.Close(); MStream.Close(); Console.WriteLine("Send " + head.iSytle + " " + head.dataSize); } } } catch (Exception ex) { Console.WriteLine(ex.ToString()); return; } }