Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
•All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vsnet_socketset.h
Go to the documentation of this file.
1 #ifndef VSNET_SOCKETSET_H
2 #define VSNET_SOCKETSET_H
3 
4 #include <config.h>
5 
6 /*
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21 
22 /*
23  * netUI - Network Interface - written by Stephane Vaxelaire <svax@free.fr>
24  */
25 
26 #include <config.h>
27 #include <iostream>
28 #include <errno.h>
29 #include <set>
30 
31 #include <boost/shared_ptr.hpp>
32 #include <boost/weak_ptr.hpp>
33 
34 #include "vsnet_headers.h"
35 #include "networking/const.h"
36 
37 #include "vsnet_thread.h"
38 #include "vsnet_pipe.h"
39 #include "vsnet_debug.h"
40 
41 struct ServerSocket;
42 class VsnetSocketBase;
43 
44 namespace VsnetDownload
45 {
46 namespace Client
47 {
48 class Manager;
49 };
50 namespace Server
51 {
52 class Manager;
53 };
54 };
55 
56 class SocketSet : public VSThread
57 {
58  typedef std::set< VsnetSocketBase* >Set;
59 
60  Set _autoset;
61 
62 #ifndef USE_NO_THREAD
63  VSPipe _thread_wakeup;
64 #endif
65  VSMutex _thread_mx;
66  VSCond _thread_cond;
67 #ifndef USE_NO_THREAD
68  bool _thread_end;
69 #endif
70 
71  bool _blockmain;
72  int _blockmain_pending;
73  fd_set _blockmain_set;
74  VSMutex _blockmain_mx;
75  VSCond _blockmain_cond;
76 
77  boost::weak_ptr< VsnetDownload::Client::Manager >_client_mgr;
78  boost::weak_ptr< VsnetDownload::Server::Manager >_server_mgr;
79 
80 public: SocketSet( bool blockmainthread = false );
81  ~SocketSet();
82 
83  bool addDownloadManager( boost::shared_ptr< VsnetDownload::Client::Manager >mgr );
84  bool addDownloadManager( boost::shared_ptr< VsnetDownload::Server::Manager >mgr );
85 
88  void set( VsnetSocketBase *s );
89 
91  void unset( VsnetSocketBase *s );
92 
94  int wait( timeval *tv = NULL );
95  void add_pending( int fd );
96  void rem_pending( int fd );
97 
103  void waste_time( long sec, long usec );
104 
105  virtual void run();
106 
107  void wakeup();
108 
110  void predestroy();
111 private:
112  int private_select( timeval *timeout );
113  void private_addset( int fd, fd_set &fds, int &maxfd );
114  void private_wakeup();
115 
116 #if defined (VSNET_DEBUG) || defined (__APPLE__)
117  void private_test_dump_active_sets( int maxfd,
118  fd_set &read_before,
119  fd_set &read_after,
120  fd_set &write_before,
121  fd_set &write_after );
122 #endif
123 #ifdef VSNET_DEBUG
124  void private_test_dump_request_sets( timeval *timeout );
125 #endif
126 
127 private: SocketSet( const SocketSet& ); //forbidden copy constructor
128  SocketSet& operator=( const SocketSet& ); //forbidden assignment operator
129 };
130 
131 std::ostream&operator<<( std::ostream &ostr, const timeval &tv );
132 
133 #endif /* VSNET_SOCKETSET_H */
134