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