void ProcessExcel(string path) { AssertThat.IsNotNullOrEmpty(path); var excelApp = new Application(); var workbooks = excelApp.Workbooks.Open(path); try { int pageCount = workbooks.Sheets.Count; AssertThat.IsTrue(pageCount > 1, "Excel's page count MUST > 1"); var sheet = workbooks.Sheets[2]; AssertThat.IsNotNull(sheet, "Excel's sheet is null"); Worksheet worksheet = sheet as Worksheet; AssertThat.IsNotNull(sheet, "Excel's worksheet is null"); var usedRange = worksheet.UsedRange; var name = Path.GetFileNameWithoutExtension(path); new ProtoGenerator(name, usedRange).Process(); } catch (Exception e) { Console.WriteLine(e); throw; } finally { workbooks.Close(); excelApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp); } }
public void Send(byte[] bytes) { try { Socket.BeginSend(bytes, 0, bytes.Length, SocketFlags.None, x => { try { var socket = x.AsyncState as Socket; AssertThat.IsNotNull(socket); int length = socket.EndSend(x); //Todo: because this is udp protocol, this is no sence if (length != bytes.Length) { } } catch (Exception e) { throw new Exception(e.ToString()); } }, Socket); } catch (Exception e) { throw new Exception(e.ToString()); } }
private void EndReceive(IAsyncResult ar) { lock (ReceiveBuffer) { if (IsConnected) //User disconnect tcpConnection proactively { int length = 0; try { var socket = ar.AsyncState as System.Net.Sockets.Socket; AssertThat.IsNotNull(socket, "Socket is null when end receive"); length = socket.EndReceive(ar); } catch (Exception e) { throw new Exception(e.ToString()); } ReceiveBuffer.MoveWritePosition(length); var bytes = ReceiveBuffer.Read(ReceiveBuffer.WritePosition); ReceiveBuffer.ResetIndex(); SocketReceiveEvent(bytes); if (length > 0) { Receive(); } else { DisconnectedEvnet(); } } } }
/// <summary> /// Connect to server /// </summary> /// <param name="ip"></param> /// <param name="port"></param> public void Connect(IPAddress ip, int port) { AssertThat.IsNotNull(ip); var iep = new IPEndPoint(ip, port); Connect(iep); }
public void Connect(IPEndPoint iep) { AssertThat.IsNotNull(iep); Socket = new Socket(iep.AddressFamily, SocketType.Dgram, ProtocolType.Udp); try { Socket.BeginConnect(iep, x => { try { var socket = x.AsyncState as Socket; AssertThat.IsNotNull(socket); if (!Socket.Connected) { throw new Exception("Connect faild"); } socket.EndConnect(x); StartReceive(); } catch (Exception e) { throw new Exception(e.ToString()); } }, Socket); } catch (Exception e) { throw new Exception(e.ToString()); } }
private void EndReceive(IAsyncResult ar) { //User disconnect connection proactively if (!IsConnected) { return; } int length = 0; try { var socket = ar.AsyncState as Socket; AssertThat.IsNotNull(socket); length = socket.EndReceive(ar); } catch (Exception e) { throw new Exception(e.ToString()); } ReceiveBuffer.MoveWritePosition(length); var bytes = ReceiveBuffer.Read(ReceiveBuffer.HowManyCanRead); SocketReceiveEvent(bytes); if (length > 0) { Receive(); } }
/// <summary> /// Connect to server /// </summary> /// <param name="ip"></param> /// <param name="port"></param> public void Connect(IPAddress ip, int port) { lock (_locker) { AssertThat.IsNotNull(ip, "ip is null"); var iep = new IPEndPoint(ip, port); Connect(iep); } }
/// <summary> /// 帧更新 /// </summary> // Update is called once per frame void Update() { AssertThat.IsNotNull(LuaEnv, "Lua env is null"); LuaEnv.Tick(); if (luaUpdate != null) { luaUpdate(Time.deltaTime); } }
private void EndSend(IAsyncResult ar) { var socket = ar.AsyncState as Socket; AssertThat.IsNotNull(socket); int length = socket.EndSend(ar); sendBuffer.Reader.MovePosition(length); Send(); }
private void ProcessData(string path) { AssertThat.IsTrue(File.Exists(path), "Excel file can not find"); var name = Path.GetFileNameWithoutExtension(path); var excelApp = new Application(); var workbooks = excelApp.Workbooks.Open(path); try { var sheet = workbooks.Sheets[1]; AssertThat.IsNotNull(sheet, "Excel's sheet is null"); Worksheet worksheet = sheet as Worksheet; AssertThat.IsNotNull(sheet, "Excel's worksheet is null"); var usedRange = worksheet.UsedRange; int rowCount = usedRange.Rows.Count; int colCount = usedRange.Columns.Count; for (int i = 4; i <= rowCount; i++) { var excel_Type = _excelIns.GetType(); var dataProp = excel_Type.GetProperty("Data"); var dataIns = dataProp.GetValue(_excelIns); var dataType = dataProp.PropertyType; var ins = _assembly.CreateInstance("HiProtobuf." + name); var addMethod = dataType.GetMethod("Add", new Type[] { typeof(int), ins.GetType() }); int id = (int)((Range)usedRange.Cells[i, 1]).Value2; addMethod.Invoke(dataIns, new[] { id, ins }); for (int j = 1; j <= colCount; j++) { var variableType = ((Range)usedRange.Cells[2, j]).Text.ToString(); var variableName = ((Range)usedRange.Cells[3, j]).Text.ToString(); var variableValue = ((Range)usedRange.Cells[i, j]).Text.ToString(); var insType = ins.GetType(); var fieldName = variableName + "_"; FieldInfo insField = insType.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance); var value = GetVariableValue(variableType, variableValue); insField.SetValue(ins, value); } } Serialize(_excelIns); } catch (Exception e) { Console.WriteLine(e); throw; } finally { workbooks.Close(); excelApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp); } }
/// <summary> /// Connect to server /// </summary> public void ConnectTOServer(IPEndPoint iep) { lock (locker) { AssertThat.IsFalse(IsConnected, "Already Connected"); AssertThat.IsNotNull(iep, "iep is null"); ConnectingEvent(); try { Socket = new Socket(iep.AddressFamily, SocketType.Stream, ProtocolType.Tcp); } catch (Exception e) { throw new Exception(e.ToString()); } //Start connect try { Socket.BeginConnect(iep, delegate(IAsyncResult ar) { try { var socket = ar.AsyncState as Socket; AssertThat.IsNotNull(socket); socket.EndConnect(ar); if (!IsConnected) { throw new Exception("Connect faild"); } ConnectedEvent(); Receive(); } catch (Exception e) { Console.WriteLine("服务器连接失败"); Thread.Sleep(10000); ConnectTOServer(iPEndPoint); return; throw new Exception(e.ToString()); } }, Socket); } catch (Exception e) { throw new Exception(e.ToString()); } } }
/// <summary> /// This can quick regist all message /// </summary> public static void Init() { var ass = Assembly.GetExecutingAssembly(); var types = ass.GetTypes(); var baseType = typeof(BinaryMsgBase); for (int i = 0; i < types.Length; i++) { if (types[i].IsSubclassOf(baseType)) { var ins = Activator.CreateInstance(types[i]) as BinaryMsgBase; AssertThat.IsNotNull(ins); ins.Regist(); } } }
private void EndReceive(IAsyncResult ar) { var socket = ar.AsyncState as Socket; AssertThat.IsNotNull(socket); int length = socket.EndReceive(ar); receiveBuffer.Writer.MovePosition(length); var bytes = receiveBuffer.ReadAllBytes(); SocketReceiveEvent(bytes); if (length > 0) { Receive(); } }
/// <summary> /// Connect to server /// </summary> public void Connect(IPEndPoint iep) { lock (_locker) { AssertThat.IsFalse(IsConnected, "Already Connected"); AssertThat.IsNotNull(iep, "iep is null"); ConnectingEvent(); try { Socket = new System.Net.Sockets.Socket(iep.AddressFamily, SocketType.Stream, ProtocolType.Tcp); } catch (Exception e) { throw new Exception(e.ToString()); } try { Socket.BeginConnect(iep, delegate(IAsyncResult ar) { try { var socket = ar.AsyncState as System.Net.Sockets.Socket; AssertThat.IsNotNull(socket, "Socket is null when end connect"); socket.EndConnect(ar); if (IsConnected) { ConnectedEvent(); Receive(); } else { AssertThat.Fail("Connect faild"); } } catch (Exception e) { throw new Exception(e.ToString()); } }, Socket); } catch (Exception e) { throw new Exception(e.ToString()); } } }
void ProcessExcel(string path) { AssertThat.IsNotNullOrEmpty(path); var fileInfo = new FileInfo(path); ExcelPackage.LicenseContext = LicenseContext.NonCommercial; using (ExcelPackage excelPackage = new ExcelPackage(fileInfo)) { var count = excelPackage.Workbook.Worksheets.Count; var worksheet = excelPackage.Workbook.Worksheets[0]; AssertThat.IsNotNull(worksheet, "Excel's sheet is null"); var rowCount = worksheet.Dimension.Rows; var columnCount = worksheet.Dimension.Columns; var name = Path.GetFileNameWithoutExtension(path); new ProtoGenerater(name, rowCount, columnCount, worksheet).Process(); } }
private void ReceiveEnd(IAsyncResult ar) { try { var socket = ar.AsyncState as Socket; AssertThat.IsNotNull(socket); int length = socket.EndReceive(ar); byte[] bytes = new byte[length]; Array.Copy(buffer, 0, bytes, 0, bytes.Length); ReceiveEvent(bytes); StartReceive(); } catch (Exception e) { throw new Exception(e.ToString()); } }
private void EndSend(IAsyncResult ar) { int length = 0; try { var socket = ar.AsyncState as Socket; AssertThat.IsNotNull(socket); length = socket.EndSend(ar); } catch (Exception e) { Console.WriteLine(e); throw; } byte[] sendBytes = new byte[length]; Array.Copy(SendBuffer.Array, SendBuffer.ReadPosition, sendBytes, 0, sendBytes.Length); SocketSendEvent(sendBytes); SendBuffer.MoveReadPosition(length); Send(); }
private void EndReceive(IAsyncResult ar) { int length = 0; try { var socket = ar.AsyncState as Socket; AssertThat.IsNotNull(socket); length = socket.EndReceive(ar); } catch (Exception e) { Console.WriteLine(e); throw; } ReceiveBuffer.MoveWritePosition(length); var bytes = ReceiveBuffer.Read(); SocketReceiveEvent(bytes); Receive(); }
public void Connect(IPEndPoint iep) { lock (locker) { if (IsConnected) { throw new Exception("Already Connected"); } AssertThat.IsNotNull(iep); ConnectingEvent(); Socket = new Socket(iep.AddressFamily, SocketType.Stream, ProtocolType.Tcp); try { Socket.BeginConnect(iep, delegate(IAsyncResult ar) { try { var socket = ar.AsyncState as Socket; AssertThat.IsNotNull(socket); socket.EndConnect(ar); if (!IsConnected) { throw new Exception("Connect faild"); } ConnectedEvent(); Receive(); } catch (Exception e) { throw new Exception(e.ToString()); } }, Socket); } catch (Exception e) { throw new Exception(e.ToString()); } } }
void ProcessExcel(string excelFilePath) { AssertThat.IsNotNullOrEmpty(excelFilePath); var excelApp = new Application(); var workbooks = excelApp.Workbooks.Open(excelFilePath); try { var sheet = workbooks.Sheets[1]; AssertThat.IsNotNull(sheet, "Excel's sheet is null"); Worksheet worksheet = sheet as Worksheet; AssertThat.IsNotNull(sheet, "Excel's worksheet is null"); var usedRange = worksheet.UsedRange; //int rowCount = usedRange.Rows.Count; //int colCount = usedRange.Columns.Count; //for (int i = 1; i <= rowCount; i++) //{ // for (int j = 1; j <= colCount; j++) // { // var value = ((Range)usedRange.Cells[i, j]).Value2; // var str = value.ToString(); // } //} var protoName = Path.GetFileNameWithoutExtension(excelFilePath); new ProtoGenerator(protoName, usedRange).Generate(); } catch (Exception e) { Console.WriteLine(e); throw; } finally { workbooks.Close(); excelApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp); } }
private void EndSend(IAsyncResult ar) { lock (SendBuffer) { if (IsConnected)//User disconnect tcpConnection proactively { int length = 0; try { var socket = ar.AsyncState as System.Net.Sockets.Socket; AssertThat.IsNotNull(socket, "Socket is null when end send"); length = socket.EndSend(ar); } catch (Exception e) { throw new Exception(e.ToString()); } byte[] sendBytes = SendBuffer.Read(length); SendBuffer.ResetIndex(); SocketSendEvent(sendBytes); } } }
private void EndSend(IAsyncResult ar) { //User disconnect connection proactively if (!IsConnected) { return; } int length = 0; try { var socket = ar.AsyncState as Socket; AssertThat.IsNotNull(socket); length = socket.EndSend(ar); } catch (Exception e) { throw new Exception(e.ToString()); } byte[] sendBytes = new byte[length]; Array.Copy(SendBuffer.Array, SendBuffer.ReadPosition, sendBytes, 0, sendBytes.Length); SocketSendEvent(sendBytes); SendBuffer.MoveReadPosition(length); }
protected override void ExistsCore(Func <decimal, bool> comparer, string messageTemplate, string additionalMessage) { AssertThat.IsNotNull(ExecutionResult, additionalMessage); }
private void ProcessData(string path, DataInfo.Data clzData) { AssertThat.IsTrue(File.Exists(path), "Excel file can not find"); var excelApp = new Application(); var workbooks = excelApp.Workbooks.Open(path); try { int pageCount = workbooks.Sheets.Count; AssertThat.IsTrue(pageCount > 2, "Excel's page count MUST > 2"); for (int pageIndex = 3; pageIndex <= pageCount; pageIndex++) { var sheet = workbooks.Sheets[pageIndex]; AssertThat.IsNotNull(sheet, "Excel's sheet is null"); Worksheet worksheet = sheet as Worksheet; AssertThat.IsNotNull(sheet, "Excel's worksheet is null"); var usedRange = worksheet.UsedRange; int rowCount = usedRange.Rows.Count; int colCount = usedRange.Columns.Count; if (rowCount < 2) { Log.Info($"{clzData.ExcelName}.xlsx的第{pageIndex}页无数据,跳过..."); continue; } for (int rowIndex = 2; rowIndex <= rowCount; rowIndex++) { var excel_Type = _listClzIns.GetType(); var dataProp = excel_Type.GetProperty("List"); var dataIns = dataProp.GetValue(_listClzIns); var dataType = dataProp.PropertyType; var ins = _assembly.CreateInstance($"{clzData.PkgName}.{clzData.DataClzName}"); var addMethod = dataType.GetMethod("Add", new[] { ins.GetType() }); for (int columnIndex = 1; columnIndex <= colCount; columnIndex++) { string variableName = ((Range)usedRange.Cells[1, columnIndex]).Text.ToString(); if (string.IsNullOrEmpty(variableName)) { continue; } var variableType = clzData.VarType[variableName]; var variableValue = ((Range)usedRange.Cells[rowIndex, columnIndex]).Text.ToString(); var insType = ins.GetType(); var fieldNameSplit = variableName.Split('_');// .Replace("_", "") + "_"; string fieldName = ""; for (int i = 0; i < fieldNameSplit.Length; i++) { fieldName += fieldNameSplit[i].Substring(0, 1).ToUpper() + fieldNameSplit[i].Substring(1); } fieldName = fieldName.Substring(0, 1).ToLower() + fieldName.Substring(1) + "_"; FieldInfo insField = insType.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance); if (insField == null) { Log.Info($"{clzData.DataClzName}中找不到列{variableName}对应的属性{fieldName}"); continue; } var value = GetVariableValue(variableType, variableValue); insField.SetValue(ins, value); } addMethod.Invoke(dataIns, new[] { ins }); } Log.Info($"{clzData.ExcelName}.xlsx的第{pageIndex}页有{rowCount - 1}条数据,导出完毕"); } Serialize(_listClzIns); } catch (Exception e) { Console.WriteLine(e); throw; } finally { workbooks.Close(); excelApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp); } }
public void IsNotNull_NotNull_NothingHappens() { AssertThat.IsNotNull(new object()); }
public void IsNotNull_Null_ThrowsAssertionException() { AssertThat.IsNotNull(null); }
/// <summary> /// Add plugin to extend logic /// </summary> /// <param name="plugin"></param> public void AddPlugin(IPlugin plugin) { AssertThat.IsNotNull(plugin); plugin.Connection = this; plugins.Add(plugin.Name, plugin); }
private void ProcessData(object protoDataInstance, string excelPath) { AssertThat.IsTrue(File.Exists(excelPath), "Failed to find the Excel file: " + excelPath); var protoName = Path.GetFileNameWithoutExtension(excelPath); var excelApp = new Application(); var workbooks = excelApp.Workbooks.Open(excelPath); try { var sheet = workbooks.Sheets[1]; AssertThat.IsNotNull(sheet, "Excel's sheet is null"); Worksheet worksheet = sheet as Worksheet; AssertThat.IsNotNull(sheet, "Excel's worksheet is null"); var usedRange = worksheet.UsedRange; int rowCount = usedRange.Rows.Count; int colCount = usedRange.Columns.Count; var protoDataType = protoDataInstance.GetType(); // the excel data type must have a property called "Data" // get the "Data" PropertyInfo from the protoType. var dataMapPropInfo = protoDataType.GetProperty("Data"); // get the "Data" property value from the protoObj instance. // "Data" property value is a reference to a dictionary of cjProtobuf.<protoName> var dataMapProp = dataMapPropInfo.GetValue(protoDataInstance); // this is equivalent to var dataMapProp = _protoObj.Data // the type of "Data" property is Dictionary<int, cjProtobuf.<protoName>> var dataMapType = dataMapPropInfo.PropertyType; var dataEntry = _assembly.CreateInstance("cjProtobuf." + protoName); var addMethod = dataMapType.GetMethod("Add", new Type[] { typeof(int), dataEntry.GetType() }); // the excel data section always starts from the fourth row for (int i = 4; i <= rowCount; i++) { int tid = (int)((Range)usedRange.Cells[i, 1]).Value2; addMethod.Invoke(dataMapProp, new[] { tid, dataEntry }); // this is equivalent to dataMapProp.Add(tid, dataEntry) for (int j = 1; j <= colCount; j++) { var variableType = ((Range)usedRange.Cells[2, j]).Text.ToString(); var variableName = ((Range)usedRange.Cells[3, j]).Text.ToString(); var variableValueStr = ((Range)usedRange.Cells[i, j]).Text.ToString(); var dataEntryType = dataEntry.GetType(); var fieldName = variableName + "_"; // the field name follows protobuf convention FieldInfo fieldInfo = dataEntryType.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance); var variableValue = ParseVariableValue(variableType, variableValueStr); fieldInfo.SetValue(dataEntry, variableValue); } } _protoDataMap.Add(protoName, dataMapProp); Serialize(protoDataInstance); } catch (Exception e) { Console.WriteLine(e); throw; } finally { workbooks.Close(); excelApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp); } }
public void AddPlugin(IPlugin plugin) { AssertThat.IsNotNull(plugin); plugins.Add(plugin.Name, plugin); }
/// <summary> /// Add plugin to extend logic /// </summary> /// <param name="plugin"></param> public void AddPlugin(IPlugin plugin) { AssertThat.IsNotNull(plugin, "Plugin is null"); plugin.TcpConnection = this; _plugins.Add(plugin.Name, plugin); }