Line data Source code
1 :
2 : /* Copyright (c) 2010-2012, Stefan Eilemann <eile@equalizergraphics.com>
3 : *
4 : * This file is part of Collage <https://github.com/Eyescale/Collage>
5 : *
6 : * This library is free software; you can redistribute it and/or modify it under
7 : * the terms of the GNU Lesser General Public License version 2.1 as published
8 : * by the Free Software Foundation.
9 : *
10 : * This library is distributed in the hope that it will be useful, but WITHOUT
11 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 : * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 : * details.
14 : *
15 : * You should have received a copy of the GNU Lesser General Public License
16 : * along with this library; if not, write to the Free Software Foundation, Inc.,
17 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 : */
19 :
20 : #include "eventConnection.h"
21 : #include "pipeConnection.h"
22 :
23 : #include <lunchbox/scopedMutex.h>
24 :
25 : namespace co
26 : {
27 :
28 56 : EventConnection::EventConnection()
29 : #ifdef _WIN32
30 : : _event( 0 )
31 : #else
32 56 : : _set( false )
33 : #endif
34 : {
35 56 : }
36 :
37 162 : EventConnection::~EventConnection()
38 : {
39 54 : _close();
40 108 : }
41 :
42 56 : bool EventConnection::connect()
43 : {
44 56 : if( !isClosed( ))
45 0 : return false;
46 :
47 56 : _setState( STATE_CONNECTING );
48 :
49 : #ifdef _WIN32
50 : _event = CreateEvent( 0, TRUE, FALSE, 0 );
51 : #else
52 55 : _connection = new PipeConnection;
53 56 : LBCHECK( _connection->connect( ));
54 56 : _set = false;
55 : #endif
56 :
57 56 : _setState( STATE_CONNECTED );
58 55 : return true;
59 : }
60 :
61 108 : void EventConnection::_close()
62 : {
63 : #ifdef _WIN32
64 : if( _event )
65 : CloseHandle( _event );
66 : _event = 0;
67 : #else
68 216 : ConnectionPtr connection = _connection;
69 108 : _connection = 0;
70 108 : _set = false;
71 :
72 108 : if( connection )
73 54 : connection->close();
74 : #endif
75 :
76 108 : _setState( STATE_CLOSED );
77 108 : }
78 :
79 390 : void EventConnection::set()
80 : {
81 : #ifdef _WIN32
82 : SetEvent( _event );
83 : #else
84 744 : lunchbox::ScopedWrite mutex( _lock );
85 390 : if( _set )
86 36 : return;
87 :
88 354 : const char c = 42;
89 354 : _connection->acceptSync()->send( &c, 1, true );
90 354 : _set = true;
91 : #endif
92 : }
93 304 : void EventConnection::reset()
94 : {
95 : #ifdef _WIN32
96 : ResetEvent( _event );
97 : #else
98 608 : lunchbox::ScopedWrite mutex( _lock );
99 304 : if( !_set )
100 0 : return;
101 :
102 304 : _buffer.setSize( 0 );
103 304 : _connection->recvNB( &_buffer, 1 );
104 :
105 608 : BufferPtr buffer;
106 304 : _connection->recvSync( buffer );
107 304 : _set = false;
108 : #endif
109 : }
110 :
111 195 : Connection::Notifier EventConnection::getNotifier() const
112 : {
113 : #ifdef _WIN32
114 : return _event;
115 : #else
116 195 : return _connection->getNotifier();
117 : #endif
118 : }
119 :
120 66 : }
|