public static void Post(string str) { ExcelDataReceiver data = new ExcelDataReceiver(); if (str != "") { string content = str.Replace(codeDollar, "$"); content = content.Replace(codePound, "#"); content = content.Replace(codePrefLeft, "\'"); content = content.Replace(codePrefRight, "\""); content = content.Replace(codePrefBlank, "\\"); if (content.Substring(0, 1) == codeCommand) { if (content.Length == 1 || (content.Length >= 6 && content.Substring(1) == "close")) { DispatchQueue.MainQueue.DispatchAsync(() => { NSNotificationCenter.DefaultCenter.PostNotificationName(notificationExcelClosed, data, null); }); } else { Console.WriteLine("Excel: {0}", content.Substring(1)); } return; } string[] lines = content.Split('\n'); foreach (string strInfo in lines) { if (strInfo != "") { ExcelCellInfo cellInfo = new ExcelCellInfo(strInfo); if (cellInfo.row > 0 && cellInfo.col > 0) { data.dataList.Add(cellInfo); } } } } string notificationName = data.dataList.Count > 0 ? notificationExcelChanged : notificationExcelEmpty; DispatchQueue.MainQueue.DispatchAsync(() => { NSNotificationCenter.DefaultCenter.PostNotificationName(notificationName, data, null); }); }
private static void DataReadCallback(IAsyncResult ar) { String content = String.Empty; // Retrieve the state object and the handler socket // from the asynchronous state object. StateObject state = (StateObject)ar.AsyncState; Socket handler = state.workSocket; // Read data from the client socket. int bytesRead = handler.EndReceive(ar); if (bytesRead > 0) { // There might be more data, so store the data received so far. state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead)); // Check for end-of-file tag. If it is not there, read // more data. content = state.sb.ToString(); int indx = content.IndexOf(codeEOF); if (indx > -1) { content = content.Substring(0, indx); DispatchQueue.DefaultGlobalQueue.DispatchAsync(() => { ExcelDataReceiver.Post(content); }); handler.Close(); } else { // Not all data received. Get more. handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(DataReadCallback), state); } } }
//------------------------------------- private static void OnExcelTableChanged(NSNotification notification) { ExcelDataReceiver data = notification.Object as ExcelDataReceiver; if (data.dataList.Count == 0) { if (printDebugOutput) { Console.Out.WriteLine("No data exist on Excel"); } CreateNewSession(); return; } if (observExcelEmpty != null) { NSNotificationCenter.DefaultCenter.RemoveObserver(observExcelEmpty); } observExcelEmpty = null; if (takeFromExcel) // Session data is invalid. Compare Excel data and Input data { takeFromExcel = false; if (ExcelCellsArray.CompareListAndArray(data.dataList, CellsArrayInput, minRowInput, minColInput)) { if (printDebugOutput) { Console.Out.WriteLine("Restore closed session"); } UseInput(); } else { if (printDebugOutput) { Console.Out.WriteLine("Excel and input data are different"); } StopSessionObservers(); ClearSessionData(); DispatchQueue.MainQueue.DispatchAsync(() => { CreateNewSession(); }); } return; } if (firstRun) // First run after session data setted. Compare it with Excel data { timeFirstRespond = DateTime.Now.Ticks; long elapsedTime = (timeFirstRespond - timeOpenExcel) / TimeSpan.TicksPerMillisecond; if (printDebugOutput) { Console.Out.WriteLine("Session first respond time= {0} sec", elapsedTime / 1000.0); } firstRun = false; if (ExcelCellsArray.CompareListAndArray(data.dataList, CellsArray, minRow, minCol) != true) { if (printDebugOutput) { Console.Out.WriteLine("Excel communication error"); } StopSession(); } return; } List <ExcelCellInfo> cellsAdded = new List <ExcelCellInfo> (); List <ExcelCellInfo> cellsChanged = new List <ExcelCellInfo> (); int maxRow = minRow + numRow - 1; int maxCol = minCol + numCol - 1; foreach (ExcelCellInfo cellInfo in data.dataList) { int iR = cellInfo.row; int iC = cellInfo.col; if (iR < minRow || maxRow < iR || iC < minCol || maxCol < iC) { cellsAdded.Add(cellInfo); } else { ExcelCell cell = CellsArray [iR - minRow, iC - minCol]; if (cell.content != cellInfo.content || cell.formula != cellInfo.formula || cell.format != cellInfo.format || cell.prefix != cellInfo.prefix) { if (cell.content == "" && cell.format == "" && cell.formula == "") { cellsAdded.Add(cellInfo); } else { cellsChanged.Add(cellInfo); cell.content = cellInfo.content; cell.formula = cellInfo.formula; cell.format = cellInfo.format; cell.prefix = cellInfo.prefix; } } } } CellsList = null; CellsList = ExcelCellsArray.ToCellList(CellsArray, minRow, minCol); CellsArray = null; if (cellsAdded.Count > 0) { foreach (ExcelCellInfo cellInfo in cellsAdded) { CellsList.Add(cellInfo); } } CellsList.Sort(); CellsArray = ExcelCellsArray.RebuildSessionArray(CellsList, out minRow, out minCol, out numRow, out numCol); if (printDebugOutput && cellsAdded.Count + cellsChanged.Count > 0) { System.Console.Out.WriteLine("------beg----------"); foreach (ExcelCellInfo cellInfo in cellsAdded) { Console.Out.Write("Added : "); cellInfo.Print(); } foreach (ExcelCellInfo cellInfo in cellsChanged) { Console.Out.Write("Changed: "); cellInfo.Print(); } Console.Out.WriteLine("------end---------- total {0} cells\n", cellsAdded.Count + cellsChanged.Count); } data.Dispose(); }