2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG.
\r
6 * CasparCG is free software: you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation, either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * CasparCG is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
\r
25 #include "..\utils\thread.h"
\r
26 #include "..\utils\lockable.h"
\r
27 #include "ProtocolStrategy.h"
\r
28 #include "..\controller.h"
\r
29 #include "..\utils\databuffer.h"
\r
34 class SerialPort : public caspar::IController
\r
36 class SerialPortClientInfo;
\r
37 typedef std::tr1::shared_ptr<SerialPortClientInfo> SerialPortClientInfoPtr;
\r
38 friend class SerialPortClientInfo;
\r
41 typedef std::tr1::shared_ptr<Listener> ListenerPtr;
\r
42 friend class Listener;
\r
45 typedef std::tr1::shared_ptr<Writer> WriterPtr;
\r
46 friend class Writer;
\r
48 SerialPort(const SerialPort&);
\r
49 SerialPort& operator=(const SerialPort&);
\r
52 explicit SerialPort(DWORD baudRate, BYTE parity, BYTE dataBits, BYTE stopBits, const tstring& portName);
\r
53 virtual ~SerialPort();
\r
58 void SetProtocolStrategy(ProtocolStrategyPtr pPS) {
\r
59 pProtocolStrategy_ = pPS;
\r
64 void Write(const tstring&);
\r
66 ProtocolStrategyPtr pProtocolStrategy_;
\r
71 utils::Thread listenerThread_;
\r
72 ListenerPtr pListener_;
\r
74 utils::Thread writerThread_;
\r
77 SerialPortClientInfoPtr pClientInfo_;
\r
81 class Listener : public utils::IRunnable
\r
85 Listener(const Listener&);
\r
86 Listener& operator=(const Listener&);
\r
89 explicit Listener(SerialPort* pSerialPort, ProtocolStrategyPtr pPS);
\r
90 virtual ~Listener();
\r
92 void Run(HANDLE stopEvent);
\r
93 bool OnUnhandledException(const std::exception& ex) throw();
\r
96 void ProcessStatusEvent(DWORD eventMask);
\r
98 bool OnRead(int numBytesRead);
\r
100 static const int WaitTimeout;
\r
101 static const int InputBufferSize;
\r
103 SerialPort* pPort_;
\r
104 ProtocolStrategyPtr pProtocolStrategy_;
\r
106 utils::Event overlappedReadEvent_;
\r
107 OVERLAPPED overlappedRead_;
\r
108 bool bWaitingOnRead_;
\r
110 char* pInputBuffer_;
\r
112 utils::DataBuffer<wchar_t> wideRecvBuffer_;
\r
117 class Writer : public utils::IRunnable, private utils::LockableObject
\r
120 Writer(HANDLE port);
\r
125 void push_back(const tstring&);
\r
127 void Run(HANDLE stopEvent);
\r
128 bool OnUnhandledException(const std::exception& ex) throw();
\r
131 utils::Event newStringEvent_;
\r
132 std::queue<tstring> sendQueue_;
\r
138 class SerialPortClientInfo : public caspar::IO::ClientInfo
\r
140 SerialPort* pSerialPort_;
\r
142 SerialPortClientInfo(SerialPort* pSP) : pSerialPort_(pSP) {
\r
144 virtual ~SerialPortClientInfo()
\r
147 void Send(const tstring& data);
\r
153 typedef std::tr1::shared_ptr<SerialPort> SerialPortPtr;
\r