Index: trunk/FACT++/src/fadctrl.cc
===================================================================
--- trunk/FACT++/src/fadctrl.cc	(revision 10811)
+++ trunk/FACT++/src/fadctrl.cc	(revision 10816)
@@ -1,2 +1,3 @@
+#include <boost/bind.hpp>
 #include <boost/bind.hpp>
 #include <boost/array.hpp>
@@ -107,4 +108,6 @@
         }
 
+        // FIXME FIXME FIXME. The data block could have the same size!!!!!
+        // !!!!!!!!!!!!!!!!!!!
         if (bytes_received == sizeof(FAD::EventHeader))
         {
@@ -130,7 +133,10 @@
             fBuffer.resize(fEventHeader.fPackageLength-sizeof(FAD::EventHeader)/2);
             AsyncRead(ba::buffer(fBuffer));
+            AsyncWait(fInTimeout, 50, &Connection::HandleReadTimeout);
 
             return;
         }
+
+        fInTimeout.cancel();
 
         if (ntohs(fBuffer.back())!=FAD::kDelimiterEnd)
@@ -198,4 +204,38 @@
     }
 
+    void HandleReadTimeout(const bs::error_code &error)
+    {
+        if (error && error!=ba::error::basic_errors::operation_aborted)
+        {
+            ostringstream str;
+            str << "Read timeout of " << URL() << ": " << error.message() << " (" << error << ")";// << endl;
+            Error(str);
+
+            PostClose();
+            return;
+
+        }
+
+        if (!is_open())
+        {
+            // For example: Here we could schedule a new accept if we
+            // would not want to allow two connections at the same time.
+            return;
+        }
+
+        if (error==ba::error::basic_errors::operation_aborted)
+            return;
+
+        // Check whether the deadline has passed. We compare the deadline
+        // against the current time since a new asynchronous operation
+        // may have moved the deadline before this actor had a chance
+        // to run.
+        if (fInTimeout.expires_at() > ba::deadline_timer::traits_type::now())
+            return;
+
+        Error("Timeout reading data from "+URL());
+        PostClose();
+    }
+
     // This is called when a connection was established
     void ConnectionEstablished()
@@ -213,6 +253,6 @@
 //            CmdSetRoi(i, 100);
 
-        Cmd(ConnectionFAD::kCmdTriggerLine, true);
-        Cmd(ConnectionFAD::kCmdSingleTrigger);
+        Cmd(FAD::kCmdTriggerLine, true);
+        Cmd(FAD::kCmdSingleTrigger);
     }
 
@@ -291,43 +331,4 @@
 
 public:
-    enum Enable_t
-    {
-        kCmdDrsEnable       = 0x0600,  // CMD_DENABLE/CMD_DISABLE
-        kCmdDwrite          = 0x0800,  // CMD_DWRITE_RUN/CMD_DWRITE_STOP
-        kCmdSclk            = 0x1000,  // CMD_SCLK_ON/OFF
-        kCmdSrclk           = 0x1500,  // CMD_SRCLK_ON/OFF
-        kCmdTriggerLine     = 0x1800,  // CMD_TRIGGERS_ON/CMD_TRIGGERS_OFF
-        //kCmdContTrigger  = 0x1f00,
-        kCmdContTriggerOff  = 0x2000,
-        kCmdRun             = 0x2200,  // CMD_Start/Stop
-        kCmdResetTriggerId  = 0x2A00,  //
-        kCmdSocket          = 0x3000,  // CMD_mode_command/CMD_mode_all_sockets
-        kCmdSingleTrigger   = 0xA000,  // CMD_Trigger
-        kCmdContTriggerOn   = 0xB000,
-    };
-
-private:
-    enum
-    {
-        kCmdWrite           = 0x0500,         // write to Config-RAM
-        kCmdWriteRoi        = kCmdWrite|0x00, // Baseaddress ROI-Values
-        kCmdWriteDac        = kCmdWrite|0x24, // Baseaddress DAC-Values
-
-        kCmdWriteRate       = kCmdWrite|0x2c, // Continous trigger rate
-        kCmdWriteRunNumber  = kCmdWrite|0x2d, // 
-
-        /*
-         kCmdRead            = 0x0a00,         // read from Config-RAM
-         kCmdReadRoi         = kCmdRead|0x00,  // Baseaddress ROI-Values
-         kCmdReadDac         = kCmdRead|0x24,  // Baseaddress DAC-Values
-         */
-
-        kCmdPhaseIncrease   = 0x1200,         // CMD_PS_DIRINC
-        kCmdPhaseDecrease   = 0x1300,         // CMD_PS_DIRDEC
-        kCmdPhaseApply      = 0x1400,         // CMD_PS_DO
-        kCmdPhaseReset      = 0x1700,         // CMD_PS_RESET
-    };
-
-public:
     ConnectionFAD(ba::io_service& ioservice, MessageImp &imp) :
     Connection(ioservice, imp()),
@@ -338,5 +339,5 @@
     }
 
-    void Cmd(Enable_t cmd, bool on=true)
+    void Cmd(FAD::Enable cmd, bool on=true)
     {
         PostCmd(cmd + (on ? 0 : 0x100));
@@ -348,7 +349,7 @@
     void CmdPhaseShift(int16_t val)
     {
-        vector<uint16_t> cmd(abs(val)+2, kCmdPhaseApply);
-        cmd[0] = kCmdPhaseReset;
-        cmd[1] = val<0 ? kCmdPhaseDecrease : kCmdPhaseIncrease;
+        vector<uint16_t> cmd(abs(val)+2, FAD::kCmdPhaseApply);
+        cmd[0] = FAD::kCmdPhaseReset;
+        cmd[1] = val<0 ? FAD::kCmdPhaseDecrease : FAD::kCmdPhaseIncrease;
         PostCmd(cmd);
     }
@@ -359,5 +360,5 @@
             return false;
 
-        PostCmd(kCmdWriteRate, val);//uint8_t(1000./val/12.5));
+        PostCmd(FAD::kCmdWriteRate, val);//uint8_t(1000./val/12.5));
         //PostCmd(kCmdContTriggerRate, uint8_t(80/val));
 
@@ -369,5 +370,5 @@
         // Allowed addr:  [0, MAX_ADDR]
         // Allowed value: [0, MAX_VAL]
-        PostCmd(kCmdWrite + addr, val);
+        PostCmd(FAD::kCmdWrite + addr, val);
     }
 
@@ -377,5 +378,5 @@
             return false;
 
-        PostCmd(kCmdWriteDac + addr, val);
+        PostCmd(FAD::kCmdWriteDac + addr, val);
         return true;
     }
@@ -390,8 +391,8 @@
 
         if (addr<0)
-            for (int i=0; i<FAD::kMaxRoiAddr; i++)
-                PostCmd(kCmdWriteRoi + i, val);
+            for (int i=0; i<=FAD::kMaxRoiAddr; i++)
+                PostCmd(FAD::kCmdWriteRoi + i, val);
         else
-            PostCmd(kCmdWriteRoi + addr, val);
+            PostCmd(FAD::kCmdWriteRoi + addr, val);
 
         return true;
@@ -712,5 +713,5 @@
     }
 
-    int Cmd(ConnectionFAD::Enable_t command)
+    int Cmd(FAD::Enable command)
     {
         for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
@@ -720,5 +721,5 @@
     }
 
-    int CmdEnable(const EventImp &evt, ConnectionFAD::Enable_t command)
+    int CmdEnable(const EventImp &evt, FAD::Enable command)
     {
         if (!CheckEventSize(evt.GetSize(), "CmdEnable", 1))
@@ -806,5 +807,5 @@
         for (int nn=0; nn<n; nn++)
             for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
-                i->second.second->Cmd(ConnectionFAD::kCmdSingleTrigger);
+                i->second.second->Cmd(FAD::kCmdSingleTrigger);
 
         return T::GetCurrentState();
@@ -827,5 +828,5 @@
 
         for (BoardList::iterator i=fBoards.begin(); i!=fBoards.end(); i++)
-            i->second.second->Cmd(ConnectionFAD::kCmdRun, start);
+            i->second.second->Cmd(FAD::kCmdRun, start);
 
         return T::GetCurrentState();
@@ -1218,20 +1219,20 @@
         // FAD Commands
         T::AddEvent("ENABLE_SRCLK", "B:1")
-            (boost::bind(&StateMachineFAD::CmdEnable, this, _1, ConnectionFAD::kCmdSrclk))
+            (boost::bind(&StateMachineFAD::CmdEnable, this, _1, FAD::kCmdSrclk))
             ("Set SRCLK");
         T::AddEvent("ENABLE_SCLK", "B:1")
-            (boost::bind(&StateMachineFAD::CmdEnable, this, _1, ConnectionFAD::kCmdSclk))
+            (boost::bind(&StateMachineFAD::CmdEnable, this, _1, FAD::kCmdSclk))
             ("Set SCLK");
         T::AddEvent("ENABLE_DRS", "B:1")
-            (boost::bind(&StateMachineFAD::CmdEnable, this, _1, ConnectionFAD::kCmdDrsEnable))
+            (boost::bind(&StateMachineFAD::CmdEnable, this, _1, FAD::kCmdDrsEnable))
             ("Switch Domino wave");
         T::AddEvent("ENABLE_DWRITE", "B:1")
-            (boost::bind(&StateMachineFAD::CmdEnable, this, _1, ConnectionFAD::kCmdDwrite))
+            (boost::bind(&StateMachineFAD::CmdEnable, this, _1, FAD::kCmdDwrite))
             ("Set Dwrite (possibly high / always low)");
         T::AddEvent("SET_DEBUG_MODE", "B:1")
-            (boost::bind(&StateMachineFAD::CmdEnable, this, _1, ConnectionFAD::kCmdSocket))
+            (boost::bind(&StateMachineFAD::CmdEnable, this, _1, FAD::kCmdSocket))
             ("Set debug mode (yes: dump events through command socket, no=dump events through other sockets)");
         T::AddEvent("ENABLE_TRIGGER_LINE", "B:1")
-            (boost::bind(&StateMachineFAD::CmdEnable, this, _1, ConnectionFAD::kCmdTriggerLine))
+            (boost::bind(&StateMachineFAD::CmdEnable, this, _1, FAD::kCmdTriggerLine))
             ("Incoming triggers can be accepted/will not be accepted");
         T::AddEvent("SET_TRIGGER_RATE", "I:1")
@@ -1255,12 +1256,12 @@
 
         T::AddEvent("CONTINOUS_TRIGGER_ON")
-            (boost::bind(&StateMachineFAD::Cmd, this, ConnectionFAD::kCmdContTriggerOn))
+            (boost::bind(&StateMachineFAD::Cmd, this, FAD::kCmdContTriggerOn))
             ("");
         T::AddEvent("CONTINOUS_TRIGGER_OFF")
-            (boost::bind(&StateMachineFAD::Cmd, this, ConnectionFAD::kCmdContTriggerOff))
+            (boost::bind(&StateMachineFAD::Cmd, this, FAD::kCmdContTriggerOff))
             ("");
 
         T::AddEvent("RESET_TRIGGER_ID")
-            (boost::bind(&StateMachineFAD::Cmd, this, ConnectionFAD::kCmdResetTriggerId))
+            (boost::bind(&StateMachineFAD::Cmd, this, FAD::kCmdResetTriggerId))
             ("");
 
