Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

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