// Update UI ListView for call_log public void UIUpdater_CallLog(String strOperation, CID_Record cid) { // Clear listview if (strOperation.Equals("clear")) { mut.WaitOne(); listView1.Items.Clear(); mut.ReleaseMutex(); return; } ListViewItem lvi = new ListViewItem(); var item = new ListViewItem(new[] { cid.id, cid.date, cid.time, cid.nmbr, cid.name, cid.customerName, cid.address, cid.city, cid.state, }); if (strOperation.Equals("add_item")) { mut.WaitOne(); listView1.Items.Add(item); mut.ReleaseMutex(); } if (strOperation.Equals("insert_item")) { mut.WaitOne(); listView1.Items.Insert(0, item); mut.ReleaseMutex(); } }
public void TCPQueryThread(string CMD) { if (serverIP.Equals("")) { return; } TcpClient tcpClient = new TcpClient(serverIP, tcpServerPort); NetworkStream stream = tcpClient.GetStream(); byte[] protocal_cmd = new byte[1024]; byte[] cmd = System.Text.Encoding.ASCII.GetBytes(CMD); //CMD = "GET_CALL_LOG" // Build protocal protocal_cmd[0] = 0x01; //start_byte protocal_cmd[1] = 0xab; //flag_bype protocal_cmd[1023] = 0x0d; //end_byte if (CMD.Equals(GET_CALL_LOG)) { this.BeginInvoke(callLogUpdater, "clear", null); try { // Command body, from protocal[2]... for (var i = 0; i < cmd.Length; i++) { protocal_cmd[i + 2] = cmd[i]; } // Send protoal_head stream.Write(protocal_cmd, 0, protocal_cmd.Length); Console.WriteLine("TCP GET_CALL_LOG sent..."); // Read response Byte[] data = new Byte[1024]; Int32 bytesRead = 0; Int32 offset = 0; Int32 bytesRemain = 1024; // Receive json packages, each sized 1024 bytes while (true) { while (true) { // Try to read the specified length of data bytesRead = stream.Read(data, offset, bytesRemain); if (bytesRead == bytesRemain) { break; } bytesRemain = bytesRemain - bytesRead; offset = offset + bytesRead; } if (data[0] != 0x01 || data[1] != 0xab || data[1023] != 0x0d || data[1022] == 0x04) // ASCII 0x04 End of Transmission { stream.Close(); tcpClient.Close(); Console.WriteLine("End of transmission."); return; // Invalid data } // Verify the json size UInt32 jsonSize; Byte[] intArray = new Byte[4]; for (var i = 0; i < 4; i++) { intArray[i] = data[2 + i]; //data[2] ... data[4] stores json length } // ARM is big endian, Most x86 architecture CPU is Little Endian, but converted by golang //if (BitConverter.IsLittleEndian) // Array.Reverse(intArray); jsonSize = BitConverter.ToUInt32(intArray, 0); Console.WriteLine("Received json size:" + jsonSize.ToString()); String strJson = Encoding.ASCII.GetString(data, 6, Convert.ToInt32(jsonSize)); Console.WriteLine(strJson); dynamic JSonObj = JObject.Parse(strJson); CID_Record cid = new CID_Record(); cid.id = (String)JSonObj["id"]; cid.phoneLine = (String)JSonObj["line"]; cid.date = (String)JSonObj["date"]; cid.time = (String)JSonObj["time"]; cid.nmbr = (String)JSonObj["nmbr"]; cid.name = (String)JSonObj["name"]; cid.customerRecord = (String)JSonObj["customer_record"]; cid.lastInvoice = (String)JSonObj["last_invoice_date"]; cid.customerName = (String)JSonObj["company_name"]; cid.address = (String)JSonObj["address"]; cid.city = (String)JSonObj["city"]; cid.state = (String)JSonObj["state"]; cid.customCol1 = (String)JSonObj["custom_col1"]; cid.customCol2 = (String)JSonObj["custom_col2"]; cid.customCol3 = (String)JSonObj["custom_col3"]; cid.customCol4 = (String)JSonObj["custom_col4"]; cid.customCol5 = (String)JSonObj["custom_col5"]; // Formating string if (cid.date.Length == 4) { cid.date = cid.date.Insert(2, "-"); } if (cid.time.Length == 4) { cid.time = cid.time.Insert(2, ":"); } if (cid.nmbr.Length == 10) { cid.nmbr = cid.nmbr.Insert(6, "-").Insert(3, "-"); } Console.WriteLine(cid.nmbr); this.BeginInvoke(callLogUpdater, "add_item", cid); // String responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes); //Console.WriteLine("Received: {0}", responseData); } } catch (SocketException ex) { Console.WriteLine(ex.ToString()); } } stream.Close(); tcpClient.Close(); }
public void TCPConnThread(object obj) { Socket clientSock = obj as Socket; int length = 1024; byte[] protocal_head = new byte[length]; int offset = 0; int bytesRead = 0; try { while ((bytesRead = clientSock.Receive(protocal_head, offset, length, SocketFlags.None)) != 0) { if (bytesRead == length) { break; } length = length - bytesRead; offset = offset + bytesRead; } if (protocal_head[0] != 0x01 || protocal_head[1] != 0xab || protocal_head[1023] != 0x0d ) { clientSock.Close(); return; } int data_size = protocal_head[2] * 1024; byte[] data_buffer = new byte[data_size]; //how many data package will be needed //MessageBox.Show("Data size:" + data_size.ToString()); length = data_size; offset = 0; bytesRead = 0; while ((bytesRead = clientSock.Receive(data_buffer, offset, length, SocketFlags.None)) != 0) { if (bytesRead == length) { break; } length = length - bytesRead; offset = offset + bytesRead; } /* * Sample: * {"address":"","city":"","company_name":"","last_invoice_date":"","phone_line":"1","name":"KEVIN HAUNG","nmbr":"134700000000","state":""} */ String _strJson = Encoding.ASCII.GetString(data_buffer, 0, data_size); //MessageBox.Show(_strJson); dynamic JSonObj = JObject.Parse(_strJson); Console.WriteLine("call in: " + JSonObj); String strNmbr = (String)JSonObj["nmbr"]; strNmbr = strNmbr.Trim(); switch (strNmbr.Length) { case 10: strNmbr = strNmbr.Insert(6, "-").Insert(3, "-"); break; case 11: strNmbr = strNmbr.Substring(1).Insert(6, "-").Insert(3, "-"); break; default: break; } // For record storage CID_Record cid = new CID_Record(); cid.id = " *"; String strName = (String)JSonObj["name"]; cid.name = strName.Trim(); cid.nmbr = strNmbr; cid.customerName = (String)JSonObj["company_name"]; cid.address = (String)JSonObj["address"]; cid.city = (String)JSonObj["city"]; cid.state = (String)JSonObj["state"]; cid.lastInvoice = (String)JSonObj["last_invoice_date"]; cid.date = (String)JSonObj["date"]; // DateTime.Now.ToString("MMM dd,"); cid.time = (String)JSonObj["time"]; // DateTime.Now.ToString("hh:mm tt"); if (cid.date.Length == 4) { cid.date = cid.date.Insert(2, "-"); } if (cid.time.Length == 4) { cid.time = cid.time.Insert(2, ":"); } if (cid.nmbr.Equals("P")) { cid.nmbr = "Private"; } if (cid.name.Equals("O")) { cid.name = "Out of range"; } mut.WaitOne(); if (currentRecordIndex >= maxRecordQty) { currentRecordIndex = 0; bIsFull = true; } cid_records[currentRecordIndex] = cid; this.BeginInvoke(notifier, currentRecordIndex); currentRecordIndex++; mut.ReleaseMutex(); this.BeginInvoke(callLogUpdater, "insert_item", cid); clientSock.Close(); return; } catch (Exception ex) { MessageBox.Show(ex.ToString()); clientSock.Close(); } }