Subversion Repositories HomeAutomation

Rev

Rev 434 | Blame | Last modification | View Log | SVN | RSS feed

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