Rev 453 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 450 | arune | 1 | using System; |
| 2 | using System.Net; |
||
| 3 | using System.Net.Sockets; |
||
| 4 | using System.Text; |
||
| 5 | using System.Threading; |
||
| 6 | using System.Collections; |
||
| 7 | using System.IO; |
||
| 8 | |||
| 9 | public class DaemonConnection { |
||
| 10 | private TcpClient tc; |
||
| 11 | private bool running; |
||
| 453 | arune | 12 | // private string host; |
| 13 | // private int port; |
||
| 450 | arune | 14 | private NetworkStream ns; |
| 15 | |||
| 16 | const byte DEBUG_LEVEL = 2; //0, 1, 2, 3 |
||
| 17 | |||
| 18 | private Queue canPackets = Queue.Synchronized(new Queue()); |
||
| 19 | |||
| 20 | public DaemonConnection() { |
||
| 21 | } |
||
| 22 | |||
| 23 | public void flushData() { |
||
| 24 | canPackets.Clear(); |
||
| 25 | } |
||
| 26 | |||
| 27 | public bool init(string host, int port) { |
||
| 28 | //connect |
||
| 29 | bool success = false; |
||
| 30 | try { |
||
| 31 | tc = new TcpClient(); |
||
| 32 | tc.Connect(host, port); |
||
| 33 | ns = tc.GetStream(); |
||
| 34 | Console.WriteLine("Connected to host " + host + " on port " + port); |
||
| 35 | success = true; |
||
| 36 | running = true; |
||
| 37 | |||
| 38 | Thread t = new Thread(thread); |
||
| 39 | t.Start(); |
||
| 40 | |||
| 41 | } catch { |
||
| 42 | Console.WriteLine("Unknown error when connecting to host " + host + " on port " + port); |
||
| 43 | } |
||
| 44 | return success; |
||
| 45 | } |
||
| 46 | |||
| 47 | public bool getData(out CanPacket cp) { |
||
| 48 | cp = null; |
||
| 49 | if (canPackets.Count>0) { |
||
| 50 | cp = (CanPacket)canPackets.Dequeue(); |
||
| 51 | return true; |
||
| 52 | } |
||
| 53 | return false; |
||
| 54 | } |
||
| 55 | |||
| 56 | public void thread() { |
||
| 57 | byte[] data = new byte[1024]; |
||
| 58 | int recv; |
||
| 59 | while (running) { |
||
| 453 | arune | 60 | Thread.Sleep(1); |
| 450 | arune | 61 | //Console.Write("."); |
| 62 | if (ns.DataAvailable) { |
||
| 63 | recv = ns.Read(data, 0, data.Length); |
||
| 64 | if (recv == 0) |
||
| 65 | break; |
||
| 66 | |||
| 67 | string tcpdata = Encoding.ASCII.GetString(data, 0, recv); |
||
| 1397 | arune | 68 | string [] split = null; |
| 69 | split = tcpdata.Split( '\n' ); |
||
| 70 | for (int i = 0; i < split.Length; i++) |
||
| 71 | { |
||
| 72 | if (split[i].Trim().Length>3) { |
||
| 73 | CanPacket cp = new CanPacket(split[i].Trim().Substring(4)); |
||
| 74 | canPackets.Enqueue(cp); |
||
| 75 | //Console.WriteLine(cp.toRawString()); |
||
| 76 | } |
||
| 450 | arune | 77 | } |
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | ns.Close(); |
||
| 82 | tc.Close(); |
||
| 83 | } |
||
| 84 | |||
| 85 | public void stop() { |
||
| 86 | running = false; |
||
| 87 | } |
||
| 88 | |||
| 89 | public void sendCanPacket(CanPacket cp) { |
||
| 90 | if (running) { |
||
| 91 | try { |
||
| 92 | byte[] data = new byte[1024]; |
||
| 93 | |||
| 94 | data = Encoding.ASCII.GetBytes(cp.toRawString()+"\n"); |
||
| 95 | ns.Write(data, 0, data.Length); |
||
| 96 | } |
||
| 97 | catch (Exception) { |
||
| 98 | Console.WriteLine("Unknown error occured when trying to send data to host"); |
||
| 99 | running = false; |
||
| 100 | } |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | } |