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