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