Rev 434 | Rev 444 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 434 | Rev 436 | ||
|---|---|---|---|
| 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 | //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(); |
| Line 77... | Line 83... | ||
| 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(); |
| Line 95... | Line 109... | ||
| 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); |
| Line 111... | Line 124... | ||
| 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 | } |