Rev 436 | Rev 452 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 436 | Rev 444 | ||
|---|---|---|---|
| Line 10... | Line 10... | ||
| 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()) { |
| Line 54... | Line 64... | ||
| 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() { |
| Line 83... | Line 91... | ||
| 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 | ||
| Line 124... | Line 126... | ||
| 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; |