public void TriggerEvent <T, U, V, W>(string eventType, T arg1, U arg2, V arg3, W arg4) { Delegate delegate2; if (this.m_theRouter.TryGetValue(eventType, out delegate2)) { Delegate[] invocationList = delegate2.GetInvocationList(); for (int i = 0; i < invocationList.Length; i++) { Action <T, U, V, W> action = invocationList[i] as Action <T, U, V, W>; if (action == null) { throw new EventException(string.Format("TriggerEvent {0} error: types of parameters are not match.", eventType)); } try { action(arg1, arg2, arg3, arg4); } catch (Exception exception) { LoggerHelper.Except(exception, null); } } } }
/// <summary> /// 触发事件, 带4个参数触发 /// </summary> /// <param name="eventType"></param> /// <param name="handler"></param> public void TriggerEvent <T, U, V, W>(string eventType, T arg1, U arg2, V arg3, W arg4) { Delegate d; if (!m_theRouter.TryGetValue(eventType, out d)) { return; } var callbacks = d.GetInvocationList(); for (int i = 0; i < callbacks.Length; i++) { Action <T, U, V, W> callback = callbacks[i] as Action <T, U, V, W>; if (callback == null) { throw new EventException(string.Format("TriggerEvent {0} error: types of parameters are not match.", eventType)); } try { callback(arg1, arg2, arg3, arg4); } catch (Exception ex) { LoggerHelper.Except(ex); } } }
/*初始化服务器*/ private void StartListening() { IPEndPoint localEndPoint = new IPEndPoint(ipAddress, portNum); // 创建TCP/IP socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //绑定Socket到本地端口和监听输入连接 try { listener.Bind(localEndPoint); listener.Listen(100); LoggerHelper.Debug("Waiting for a connection..."); while (m_isRunning) { allDone.Reset(); //结束回调,只能连接一台客户端 listener.BeginAccept(new AsyncCallback(AcceptCallback), listener); //手动阻塞 allDone.WaitOne(); } } catch (Exception e) { LoggerHelper.Except(e); } }
public static String LoadText(String fileName) { try { if (SystemSwitch.ReleaseMode) { return(FileAccessManager.LoadText(fileName)); } else { if (SystemConfig.IsEditor) { return(Utils.LoadFile(String.Concat(SystemConfig.DataPath, "/Resources/", fileName))); } else { return(Utils.LoadResource(fileName)); } } } catch (Exception ex) { LoggerHelper.Except(ex); return(""); } }
public static bool Init( #if UNITY_IPHONE System.Action callback #endif ) { try { var result = LoadCfgInfo( #if UNITY_IPHONE callback #endif ); #if UNITY_IPHONE if (!result) { return(false); } #endif } catch (Exception ex) { LoggerHelper.Except(ex); } try { LoadConfig(); } catch (Exception ex) { LoggerHelper.Except(ex); } return(true); }
private void DoReceive() { //读取流 do { bytesRead = 0; try { int size = MAX_BUFFER_SIZE - m_nRecvBufferSize; if (size > 0) { bytesRead = m_socket.Receive(m_recvBuffer, m_nRecvBufferSize, size, SocketFlags.None); m_nRecvBufferSize += bytesRead; if (bytesRead == 0) {//读的长度为0 bytesRead = 1; } } else { bytesRead = 1;//缓存不够时继续循环,后面会对缓存数据进行处理 LoggerHelper.Warning("buffer not enough"); } } catch (ObjectDisposedException) { // 网络流已被关闭,结束接收数据 LoggerHelper.Error("tcp close"); } catch (IOException ioex) { //捕获WSACancelBlockingCall()导致的异常。 //原因:强迫终止一个在进行的阻塞调用。 //可直接捕获忽略,应该不会有不良影响。 //LoggerHelper.Error("WSACancelBlockCall"); LoggerHelper.Except(ioex); } catch (Exception ex) { LoggerHelper.Except(ex); } //LoggerHelper.Debug("DataReceive: " + bytesRead); SplitPackets(); } while (bytesRead > 0); lock (m_connectCheckerLocker) if (m_ConnectChecker > 0) { m_ConnectChecker--; LoggerHelper.Debug("Disconnected.m_ConnectChecker: " + m_ConnectChecker); } else { LoggerHelper.Error("receive bytes " + bytesRead); TimerHeap.AddTimer(1000, 0, OnNetworkDisconnected); } LoggerHelper.Debug("DataReceiveComplete"); }
public static void Init(Action <bool> callback) { try { LoadCfgInfo(callback); } catch (Exception exception) { LoggerHelper.Except(exception, null); } }
public static string LoadText(string fileName) { try { return(SystemSwitch.ReleaseMode ? FileAccessManager.LoadText(fileName) : Utils.LoadResource(fileName)); } catch (Exception exception) { LoggerHelper.Except(exception, null); return(""); } }
/// <summary> /// Unpacks the files. /// </summary> /// <param name="file">The file.</param> /// <returns>if succeed return true,otherwise false.</returns> public static bool UnpackFiles(string file, string dir) { try { if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } ZipInputStream s = new ZipInputStream(File.OpenRead(file)); ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { string directoryName = Path.GetDirectoryName(theEntry.Name); string fileName = Path.GetFileName(theEntry.Name); if (directoryName != String.Empty) { Directory.CreateDirectory(dir + directoryName); } if (fileName != String.Empty) { FileStream streamWriter = File.Create(dir + theEntry.Name); int size = 2048; byte[] data = new byte[2048]; while (true) { size = s.Read(data, 0, data.Length); if (size > 0) { streamWriter.Write(data, 0, size); } else { break; } } streamWriter.Close(); } } s.Close(); return(true); } catch (Exception ex) { LoggerHelper.Except(ex); return(false); } }
private static List <T> LoadXMLText <T>(string text) { Exception exception; List <T> list = new List <T>(); try { if (string.IsNullOrEmpty(text)) { return(list); } Type type = typeof(T); Dictionary <int, Dictionary <string, string> > dictionary = XMLParser.LoadIntMap(XMLParser.LoadXML(text), text); PropertyInfo[] properties = type.GetProperties(~BindingFlags.Static); foreach (KeyValuePair <int, Dictionary <string, string> > pair in dictionary) { object obj2 = type.GetConstructor(Type.EmptyTypes).Invoke(null); foreach (PropertyInfo info in properties) { if (info.Name == "id") { info.SetValue(obj2, pair.Key, null); } else { try { if (pair.Value.ContainsKey(info.Name)) { object obj3 = Utils.GetValue(pair.Value[info.Name], info.PropertyType); info.SetValue(obj2, obj3, null); } } catch (Exception exception1) { exception = exception1; LoggerHelper.Debug(string.Concat(new object[] { "LoadXML error: ", pair.Value[info.Name], " ", info.PropertyType }), true, 0); LoggerHelper.Except(exception, null); } } } list.Add((T)obj2); } } catch (Exception exception2) { exception = exception2; LoggerHelper.Except(exception, null); LoggerHelper.Error("error text: \n" + text, true); } return(list); }
private void DoReceive() { do { this.bytesRead = 0; try { int size = 0xffff - this.m_nRecvBufferSize; if (size > 0) { this.bytesRead = this.m_socket.Receive(this.m_recvBuffer, this.m_nRecvBufferSize, size, SocketFlags.None); this.m_nRecvBufferSize += this.bytesRead; if (this.bytesRead == 0) { this.bytesRead = 1; } } else { this.bytesRead = 1; LoggerHelper.Warning("buffer not enough", true); } } catch (ObjectDisposedException) { LoggerHelper.Error("tcp close", true); } catch (IOException exception) { LoggerHelper.Except(exception, null); } catch (Exception exception2) { LoggerHelper.Except(exception2, null); } this.SplitPackets(); }while (this.bytesRead > 0); lock (this.m_connectCheckerLocker) { if (this.m_ConnectChecker > 0) { this.m_ConnectChecker--; LoggerHelper.Debug("Disconnected.m_ConnectChecker: " + this.m_ConnectChecker, true, 0); } else { LoggerHelper.Error("receive bytes " + this.bytesRead, true); TimerHeap.AddTimer(0x3e8, 0, this.OnNetworkDisconnected); } } LoggerHelper.Debug("DataReceiveComplete", true, 0); }
public static bool UnpackFiles(string file, string dir) { try { ZipEntry entry; if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } ZipInputStream stream = new ZipInputStream(File.OpenRead(file)); while ((entry = stream.GetNextEntry()) != null) { bool flag2; string directoryName = Path.GetDirectoryName(entry.Name); string fileName = Path.GetFileName(entry.Name); if (directoryName != string.Empty) { Directory.CreateDirectory(dir + directoryName); } if (!(fileName != string.Empty)) { continue; } FileStream stream2 = File.Create(dir + entry.Name); int count = 0x800; byte[] buffer = new byte[0x800]; goto Label_00D0; Label_009D: count = stream.Read(buffer, 0, buffer.Length); if (count > 0) { stream2.Write(buffer, 0, count); } else { goto Label_00D5; } Label_00D0: flag2 = true; goto Label_009D; Label_00D5: stream2.Close(); } stream.Close(); return(true); } catch (Exception exception) { LoggerHelper.Except(exception, null); return(false); } }
public static void PackFiles(string filename, string directory, string fileFilter) { try { new FastZip { CreateEmptyDirectories = true }.CreateZip(filename, directory, false, fileFilter); } catch (Exception exception) { LoggerHelper.Except(exception, null); } }
/**服务器关闭回调**/ private void closeCallback() { try { if (OnNetworkDisconnected != null) { DriverLib.Invoke(OnNetworkDisconnected); } } catch (Exception ex) { LoggerHelper.Except(ex, "服务器[" + ip + ":" + port + "]关闭回调出错!"); } }
public static SecurityElement LoadXML(string xml) { try { SecurityParser parser = new SecurityParser(); parser.LoadXml(xml); return(parser.ToXml()); } catch (Exception exception) { LoggerHelper.Except(exception, null); return(null); } }
/// <summary> /// Create a zip archive. /// </summary> /// <param name="filename">The filename.</param> /// <param name="directory">The directory to zip.</param> public static void PackFiles(string filename, string directory, string fileFilter) { try { FastZip fz = new FastZip(); fz.CreateEmptyDirectories = true; fz.CreateZip(filename, directory, false, fileFilter); fz = null; } catch (Exception ex) { LoggerHelper.Except(ex); } }
private static List <T> LoadXMLText <T>(string text) { List <T> list = new List <T>(); try { if (String.IsNullOrEmpty(text)) { return(list); } Type type = typeof(T); var xml = XMLParser.LoadXML(text); Dictionary <Int32, Dictionary <String, String> > map = XMLParser.LoadIntMap(xml, text); var props = type.GetProperties(~System.Reflection.BindingFlags.Static); foreach (var item in map) { var obj = type.GetConstructor(Type.EmptyTypes).Invoke(null); foreach (var prop in props) { if (prop.Name == "id") { prop.SetValue(obj, item.Key, null); } else { try { if (item.Value.ContainsKey(prop.Name)) { var value = Utils.GetValue(item.Value[prop.Name], prop.PropertyType); prop.SetValue(obj, value, null); } } catch (Exception ex) { //LoggerHelper.Debug("LoadXML error: " + item.Value[prop.Name] + " " + prop.PropertyType); LoggerHelper.Except(ex); } } } list.Add((T)obj); } } catch (Exception ex) { LoggerHelper.Except(ex); //LoggerHelper.Error("error text: \n" + text); } return(list); }
public bool LoadMap(string fileName, out Dictionary <string, Dictionary <string, string> > map) { try { SecurityElement xml = Load(fileName); map = LoadMap(xml); return(true); } catch (Exception exception) { LoggerHelper.Except(exception, null); map = null; return(false); } }
/// <summary> /// 从指定的 URL 加载 map 数据。 /// </summary> /// <param name="fileName">文件的 URL,该文件包含要加载的 XML 文档</param> /// <param name="map">map 数据</param> /// <returns>是否加载成功</returns> public Boolean LoadMap(String fileName, out Dictionary <String, Dictionary <String, String> > map) { try { var xml = Load(fileName); map = LoadMap(xml); return(true); } catch (Exception ex) { LoggerHelper.Except(ex); map = null; return(false); } }
public static void LoadServerList() { try { List <ServerInfo> servers; string sReadPath = Application.streamingAssetsPath + "/Resources/localserver.xml"; if (File.Exists(sReadPath)) { //LoggerHelper.Debug("File Exists: sReadPath is :" + sReadPath); servers = LoadXML <ServerInfo>(sReadPath); } else if (File.Exists(LocalServerListPath))//如果存在 { //LoggerHelper.Debug("File Exists: LocalServerListPath is :" + LocalServerListPath); servers = LoadXML <ServerInfo>(LocalServerListPath); } else { //LoggerHelper.Debug("File NOT Exists"); //LoggerHelper.Debug("File Exists:SERVER_LIST_URL_KEY" + SERVER_LIST_URL_KEY); var url = GetCfgInfoUrl(SERVER_LIST_URL_KEY); if (!String.IsNullOrEmpty(url)) { DownloadMgr.Instance.DownloadFile(url, ServerListPath, ServerListPath + "bak"); } servers = LoadXML <ServerInfo>(ServerListPath); } if (servers.Count != 0) { ServersList = servers; } for (int i = 0; i < ServersList.Count; i++) { if (ServersList[i].id == Instance.SelectedServer) { SelectedServerIndex = i; break; } } } catch (Exception ex) { LoggerHelper.Except(ex); } }
public static string BuildFileMd5(string filename) { string str = null; try { using (FileStream stream = File.OpenRead(filename)) { str = FormatMD5(MD5.Create().ComputeHash(stream)); } } catch (Exception exception) { LoggerHelper.Except(exception, null); } return(str); }
/// <summary> /// 从RecvBuffer 中切分出多个Packets, 不足一个 Packet 的部分, 存留在 Buffer 中留待下次Split /// </summary> private void SplitPackets() { try { int offset = 0; while (m_nRecvBufferSize > m_headLengthConverter.VTypeLength) { try { int nLength = (int)(uint)m_headLengthConverter.Decode(m_recvBuffer, ref offset); if (m_nRecvBufferSize >= nLength) { int realLength = nLength - m_headLengthConverter.VTypeLength - RESERVE_SIZE; offset += RESERVE_SIZE; byte[] packet = new byte[realLength]; Buffer.BlockCopy(m_recvBuffer, offset, packet, 0, realLength); lock (m_recvQueueLocker)//此处理为独立线程处理,需加锁,否则会出现丢包 { m_recvQueue.Enqueue(packet); } m_nRecvBufferSize -= nLength; offset += realLength; } else { offset -= m_headLengthConverter.VTypeLength;//m_headLengthConverter.Decode()自带偏移,需要调整偏移 break; } } catch (Exception ex) { LoggerHelper.Except(ex); break; } } // 整理 RecvBuffer, 将buffer 内容前移 Buffer.BlockCopy(m_recvBuffer, offset, m_recvBuffer, 0, m_nRecvBufferSize); } catch (Exception ex) { LoggerHelper.Except(ex); LoggerHelper.Critical("SplitPackets error."); Close(); } }
private void SplitPackets() { Exception exception; try { int index = 0; while (this.m_nRecvBufferSize > this.m_headLengthConverter.VTypeLength) { try { int num2 = (int)((uint)this.m_headLengthConverter.Decode(this.m_recvBuffer, ref index)); if (this.m_nRecvBufferSize >= num2) { int count = (num2 - this.m_headLengthConverter.VTypeLength) - 2; index += 2; byte[] dst = new byte[count]; Buffer.BlockCopy(this.m_recvBuffer, index, dst, 0, count); lock (this.m_recvQueueLocker) { this.m_recvQueue.Enqueue(dst); } this.m_nRecvBufferSize -= num2; index += count; continue; } index -= this.m_headLengthConverter.VTypeLength; } catch (Exception exception1) { exception = exception1; LoggerHelper.Except(exception, null); } break; } Buffer.BlockCopy(this.m_recvBuffer, index, this.m_recvBuffer, 0, this.m_nRecvBufferSize); } catch (Exception exception2) { exception = exception2; LoggerHelper.Except(exception, null); LoggerHelper.Critical("SplitPackets error.", true); this.Close(); } }
private static void LoadConfig() { try { List <LocalSetting> list = LoadXML <LocalSetting>(ConfigPath); if ((list == null) || (list.Count == 0)) { InitConfig(); } else { Instance = list[0]; } } catch (Exception exception) { InitConfig(); LoggerHelper.Except(exception, null); } }
public static string LoadText(string fileName) { try { if (SystemSwitch.ReleaseMode) { return(FileAccessManager.LoadText(fileName)); } if (SystemConfig.IsEditor) { return(Utils.LoadFile(SystemConfig.DataPath + "/Resources/" + fileName)); } return(Utils.LoadResource(fileName)); } catch (Exception exception) { LoggerHelper.Except(exception, null); return(""); } }
private static void LoadConfig() { try { var instance = LoadXML <LocalSetting>(ConfigPath); if (instance == null || instance.Count == 0) { InitConfig(); } else { Instance = instance[0]; } } catch (Exception ex) { InitConfig(); LoggerHelper.Except(ex); } }
public static void LoadServerList() { //LoggerHelper.Debug ("------SystemConfig.LoadServerList()------"); try { List <ServerInfo> servers; //LoggerHelper.Debug("path="+LocalServerListPath); if (File.Exists(LocalServerListPath))//如果存在 { servers = LoadXML <ServerInfo>(LocalServerListPath); } else { var url = GetCfgInfoUrl(SERVER_LIST_URL_KEY); if (!String.IsNullOrEmpty(url)) { DownloadMgr.Instance.DownloadFile(url, ServerListPath, ServerListPath + "bak"); } servers = LoadXML <ServerInfo>(ServerListPath); } if (servers.Count != 0) { ServersList = servers; } for (int i = 0; i < ServersList.Count; i++) { if (ServersList[i].id == Instance.SelectedServer) { SelectedServerIndex = i; break; } } } catch (Exception ex) { LoggerHelper.Except(ex); } }
/// <summary> /// 生成文件的md5(冯委) /// </summary> /// <param name="filename"></param> /// <returns></returns> public static String BuildFileMd5(String filename) { String filemd5 = null; try { using (var fileStream = File.OpenRead(filename)) { //UnityEditor.AssetDatabase var md5 = MD5.Create(); var fileMD5Bytes = md5.ComputeHash(fileStream);//计算指定Stream 对象的哈希值 //fileStream.Close();//流数据比较大,手动卸载 //fileStream.Dispose(); //由以连字符分隔的十六进制对构成的String,其中每一对表示value 中对应的元素;例如“F-2C-4A” filemd5 = FormatMD5(fileMD5Bytes); } } catch (System.Exception ex) { LoggerHelper.Except(ex); } return(filemd5); }
public static void LoadServerList() { try { List <ServerInfo> list; if (File.Exists(LocalServerListPath)) { list = LoadXML <ServerInfo>(LocalServerListPath); } else { string cfgInfoUrl = GetCfgInfoUrl("serverlist"); if (!string.IsNullOrEmpty(cfgInfoUrl)) { DownloadMgr.Instance.DownloadFile(cfgInfoUrl, ServerListPath, ServerListPath + "bak"); } list = LoadXML <ServerInfo>(ServerListPath); } if (list.Count != 0) { ServersList = list; } for (int i = 0; i < ServersList.Count; i++) { if (ServersList[i].id == Instance.SelectedServer) { SelectedServerIndex = i; return; } } } catch (Exception exception) { LoggerHelper.Except(exception, null); } }
public static bool Init() { Exception exception; try { bool flag = LoadCfgInfo(); } catch (Exception exception1) { exception = exception1; LoggerHelper.Except(exception, null); } try { LoadConfig(); } catch (Exception exception2) { exception = exception2; LoggerHelper.Except(exception, null); } return(true); }