ba71fe613c5029a7392363e3d331f1b4e24a5117
[mediaserv.git] / Server / StreamConnection.hh
1 // $Id$
2 //
3 // Copyright (C) 2006 
4
5 #ifndef HH_StreamConnection_
6 #define HH_StreamConnection_ 1
7
8 // Custom includes
9 #include <boost/utility.hpp>
10 #include <boost/intrusive_ptr.hpp>
11
12 #include "Utils/MicroTime.hh"
13 #include "Utils/intrusive_refcount.hh"
14 #include "Scheduler/Scheduler.hh"
15 #include "Socket/ClientSocketHandle.hh"
16 #include "Socket/CommunicationPolicy.hh"
17 #include "Socket/FramingPolicy.hh"
18 #include "Socket/ReadWritePolicy.hh"
19
20 //#include "StreamConnection.mpp"
21 ///////////////////////////////hh.p////////////////////////////////////////
22
23 namespace g0dil {
24 namespace mediaserv {
25
26     class StreamConnection : public satcom::lib::intrusive_refcount
27     {
28     public:
29         ///////////////////////////////////////////////////////////////////////////
30         // Types
31
32         typedef satcom::lib::ClientSocketHandle<
33             satcom::lib::MakeSocketPolicy<satcom::lib::ConnectedCommunicationPolicy,
34                                           satcom::lib::StreamFramingPolicy,
35                                           satcom::lib::WriteablePolicy>::policy> ClientHandle;
36
37         typedef boost::intrusive_ptr<StreamConnection> ptr;
38         typedef boost::function<void ()> Callback;
39
40         static const unsigned packetSize = 1400;
41
42         ///////////////////////////////////////////////////////////////////////////
43         ///\name Structors and default members
44         ///@{
45
46         StreamConnection(int fileFd, unsigned bytesPerSecond, ClientHandle client,
47                          unsigned bufferMSecs, Callback callback);
48         ~StreamConnection();
49
50         ///@}
51         ///////////////////////////////////////////////////////////////////////////
52
53         void start();
54         unsigned bytesSent();
55
56     protected:
57
58     private:
59         void registerCallback();
60         void unregisterCallback();
61         void registerTimeout(unsigned timeout);
62         unsigned targetBytes();
63         void fillBuffer();
64         
65         void callback(ClientHandle client, satcom::lib::Scheduler::EventId);
66         void timeout();
67         
68
69         int fileFd_;
70         unsigned bytesPerSecond_;
71         ClientHandle client_;
72         unsigned bufferMSecs_;
73         Callback callback_;
74
75         unsigned bytesWritten_;
76         satcom::lib::MicroTime start_;
77         unsigned bufferSize_;
78         char buffer_[packetSize];
79         bool terminate_;
80     };
81
82 }}
83
84 ///////////////////////////////hh.e////////////////////////////////////////
85 #include "StreamConnection.cci"
86 //#include "StreamConnection.ct"
87 //#include "StreamConnection.cti"
88 //#include "StreamConnection.mpp"
89 #endif
90
91 \f
92 // Local Variables:
93 // mode: c++
94 // End: