Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 434 | 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 TcpServer { |
||
| 10 | private TcpListener listener; |
||
| 11 | private bool running; |
||
| 12 | private int port; |
||
| 13 | private ArrayList clients; |
||
| 14 | const byte DEBUG_LEVEL = 2; //0, 1, 2, 3 |
||
| 15 | |||
| 444 | arune | 16 | private Queue tcpPackets = Queue.Synchronized(new Queue()); |
| 17 | |||
| 434 | arune | 18 | public TcpServer(int port) { |
| 19 | running = true; |
||
| 20 | listener = new TcpListener(port); |
||
| 21 | listener.Start(); |
||
| 22 | this.port = port; |
||
| 23 | clients = new ArrayList(); |
||
| 24 | } |
||
| 444 | arune | 25 | |
| 26 | public bool getData(out string data) { |
||
| 27 | data = null; |
||
| 28 | if (tcpPackets.Count>0) { |
||
| 29 | data = (string)tcpPackets.Dequeue(); |
||
| 30 | return true; |
||
| 31 | } |
||
| 32 | return false; |
||
| 33 | } |
||
| 434 | arune | 34 | |
| 35 | public void thread() { |
||
| 36 | if (DEBUG_LEVEL>1) { Console.WriteLine("Waiting for clients on port {0}", port); } |
||
| 37 | while (running) { |
||
| 452 | arune | 38 | Thread.Sleep(1); |
| 436 | arune | 39 | foreach (Connection client in clients) { |
| 40 | //... hämta data från client... |
||
| 41 | //om ny data så skicka till alla i clients utom client |
||
| 444 | arune | 42 | string tcpdata; |
| 43 | bool hasData = client.getData(out tcpdata); |
||
| 436 | arune | 44 | if (hasData) { |
| 444 | arune | 45 | |
| 46 | //lägg data på kö så demonen kan hämta för vidare process |
||
| 47 | tcpPackets.Enqueue(tcpdata); |
||
| 48 | |||
| 49 | //skicka data till övriga klienter, senare ska datat parsas innan så man bara skickar ut can-paket |
||
| 436 | arune | 50 | foreach (Connection sendclient in clients) { |
| 51 | if (sendclient != client) { |
||
| 444 | arune | 52 | sendclient.sendDataDummy(tcpdata); |
| 434 | arune | 53 | } |
| 54 | } |
||
| 436 | arune | 55 | } |
| 434 | arune | 56 | } |
| 436 | arune | 57 | |
| 58 | foreach (Connection client in clients) { |
||
| 59 | if (!client.isConnected()) { |
||
| 60 | clients.Remove(client); |
||
| 61 | if (DEBUG_LEVEL>1) { Console.WriteLine("Client dropped"); } |
||
| 62 | break; |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | if (running && listener.Pending()) { |
||
| 434 | arune | 67 | if (DEBUG_LEVEL>1) { Console.WriteLine("Client connected on port {0}", port); } |
| 68 | TcpClient handler = listener.AcceptTcpClient(); |
||
| 69 | Connection newconn = new Connection(handler); |
||
| 70 | clients.Add(newconn); |
||
| 71 | Thread t = new Thread(newconn.thread); |
||
| 72 | t.Start(); |
||
| 73 | } |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | public void stop() { |
||
| 78 | running = false; |
||
| 79 | foreach (Connection client in clients) { |
||
| 80 | client.stop(); |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | public void sendCanPacket(CanPacket cp) { |
||
| 85 | foreach (Connection client in clients) { |
||
| 86 | client.sendData(cp); |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | } |
||
| 91 | |||
| 92 | class Connection { |
||
| 93 | private TcpClient client; |
||
| 94 | private NetworkStream ns; |
||
| 95 | private bool connected; |
||
| 96 | |||
| 444 | arune | 97 | private Queue tcpPackets = Queue.Synchronized(new Queue()); |
| 434 | arune | 98 | |
| 99 | public Connection(TcpClient client) { |
||
| 100 | this.client = client; |
||
| 436 | arune | 101 | ns = client.GetStream(); |
| 102 | //this.id = id; |
||
| 434 | arune | 103 | } |
| 104 | |||
| 444 | arune | 105 | public bool getData(out string data) { |
| 434 | arune | 106 | data = null; |
| 444 | arune | 107 | if (tcpPackets.Count>0) { |
| 108 | data = (string)tcpPackets.Dequeue(); |
||
| 434 | arune | 109 | return true; |
| 110 | } |
||
| 111 | return false; |
||
| 112 | } |
||
| 113 | |||
| 114 | public void thread() { |
||
| 115 | //läs och lagra så att en funktion kan läsa från mig |
||
| 116 | byte[] data = new byte[1024]; |
||
| 117 | int recv; |
||
| 118 | connected = true; |
||
| 119 | while (connected) { |
||
| 452 | arune | 120 | Thread.Sleep(1); |
| 434 | arune | 121 | if (ns.DataAvailable) { |
| 122 | recv = ns.Read(data, 0, data.Length); |
||
| 123 | if (recv == 0) |
||
| 124 | break; |
||
| 125 | |||
| 126 | string tcpdata = Encoding.ASCII.GetString(data, 0, recv); |
||
| 127 | //string [] split = null; |
||
| 128 | //split = tcpdata.Split(" "); |
||
| 129 | |||
| 130 | //if (split.Length ) { |
||
| 444 | arune | 131 | tcpPackets.Enqueue(tcpdata); |
| 434 | arune | 132 | //} |
| 133 | //Console.WriteLine(System.Text.Encoding.ASCII.GetString(data)); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | connected = false; |
||
| 137 | ns.Close(); |
||
| 138 | client.Close(); |
||
| 139 | } |
||
| 140 | |||
| 141 | public bool isConnected() { |
||
| 142 | return connected; |
||
| 143 | } |
||
| 144 | |||
| 145 | public void stop() { |
||
| 146 | connected = false; |
||
| 147 | } |
||
| 148 | |||
| 149 | public void sendData(CanPacket cp) { |
||
| 150 | if (connected) { |
||
| 151 | try { |
||
| 152 | byte[] data = new byte[1024]; |
||
| 153 | |||
| 154 | data = Encoding.ASCII.GetBytes(cp.toRawString()+"\n"); |
||
| 155 | ns.Write(data, 0, data.Length); |
||
| 156 | } |
||
| 157 | catch (Exception) { |
||
| 158 | connected = false; |
||
| 159 | } |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | public void sendDataDummy(string senddata) { |
||
| 164 | if (connected) { |
||
| 165 | try { |
||
| 166 | byte[] data = new byte[1024]; |
||
| 167 | |||
| 168 | data = Encoding.ASCII.GetBytes(senddata+"\n"); |
||
| 169 | ns.Write(data, 0, data.Length); |
||
| 170 | } |
||
| 171 | catch (Exception) { |
||
| 172 | connected = false; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | } |