Panda3D
connectionWriter.h
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file connectionWriter.h
10  * @author drose
11  * @date 2000-02-08
12  */
13 
14 #ifndef CONNECTIONWRITER_H
15 #define CONNECTIONWRITER_H
16 
17 #include "pandabase.h"
18 #include "datagramQueue.h"
19 #include "connection.h"
20 #include "pointerTo.h"
21 #include "thread.h"
22 #include "pvector.h"
23 
24 class ConnectionManager;
25 class NetAddress;
26 
27 /**
28  * This class handles threaded delivery of datagrams to various TCP or UDP
29  * sockets.
30  *
31  * A ConnectionWriter may define an arbitrary number of threads (0 or more) to
32  * write its datagrams to sockets. The number of threads is specified at
33  * construction time and cannot be changed.
34  */
35 class EXPCL_PANDA_NET ConnectionWriter {
36 PUBLISHED:
37  explicit ConnectionWriter(ConnectionManager *manager, int num_threads,
38  const std::string &thread_name = std::string());
40 
41  void set_max_queue_size(int max_size);
42  int get_max_queue_size() const;
43  int get_current_queue_size() const;
44 
45  BLOCKING bool send(const Datagram &datagram,
46  const PT(Connection) &connection,
47  bool block = false);
48 
49  BLOCKING bool send(const Datagram &datagram,
50  const PT(Connection) &connection,
51  const NetAddress &address,
52  bool block = false);
53 
54  bool is_valid_for_udp(const Datagram &datagram) const;
55 
56  ConnectionManager *get_manager() const;
57  bool is_immediate() const;
58  int get_num_threads() const;
59 
60  void set_raw_mode(bool mode);
61  bool get_raw_mode() const;
62 
63  void set_tcp_header_size(int tcp_header_size);
64  int get_tcp_header_size() const;
65 
66  void shutdown();
67 
68 protected:
69  void clear_manager();
70 
71 private:
72  void thread_run(int thread_index);
73  bool send_datagram(const NetDatagram &datagram);
74 
75 protected:
76  ConnectionManager *_manager;
77 
78 private:
79  bool _raw_mode;
80  int _tcp_header_size;
81  DatagramQueue _queue;
82  bool _shutdown;
83 
84  class WriterThread : public Thread {
85  public:
86  WriterThread(ConnectionWriter *writer, const std::string &thread_name,
87  int thread_index);
88  virtual void thread_main();
89 
90  ConnectionWriter *_writer;
91  int _thread_index;
92  };
93 
94  typedef pvector< PT(WriterThread) > Threads;
95  Threads _threads;
96 
97  bool _immediate;
98 
99  friend class ConnectionManager;
100  friend class WriterThread;
101 };
102 
103 #endif
A specific kind of Datagram, especially for sending across or receiving from a network.
Definition: netDatagram.h:40
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
The primary interface to the low-level networking layer in this package.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:42
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This class handles threaded delivery of datagrams to various TCP or UDP sockets.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A thread; that is, a lightweight process.
Definition: thread.h:46
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Represents a single TCP or UDP socket for input or output.
Definition: connection.h:29
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
A thread-safe, FIFO queue of NetDatagrams.
Definition: datagramQueue.h:29
Represents a network address to which UDP packets may be sent or to which a TCP socket may be bound.
Definition: netAddress.h:25