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