UdpDataProtocol.h 3.4 KB
Newer Older
jcaceres's avatar
jcaceres committed
1
2
//*****************************************************************
/*
jcaceres's avatar
jcaceres committed
3
  JackTrip: A System for High-Quality Audio Network Performance
jcaceres's avatar
jcaceres committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  over the Internet

  Copyright (c) 2008 Juan-Pablo Caceres, Chris Chafe.
  SoundWIRE group at CCRMA, Stanford University.
  
  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation
  files (the "Software"), to deal in the Software without
  restriction, including without limitation the rights to use,
  copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following
  conditions:
  
  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  OTHER DEALINGS IN THE SOFTWARE.
*/
//*****************************************************************

/**
jcaceres's avatar
jcaceres committed
33
 * \file UdpDataProtocol.h
jcaceres's avatar
jcaceres committed
34
35
36
37
 * \author Juan-Pablo Caceres
 * \date June 2008
 */

jcaceres's avatar
jcaceres committed
38
#ifndef __UDPDATAPROTOCOL_H__
39
40
41
42
43
#define __UDPDATAPROTOCOL_H__

#include <QThread>
#include <QUdpSocket>
#include <QHostAddress>
jcaceres's avatar
jcaceres committed
44

jcaceres's avatar
jcaceres committed
45
#include "DataProtocol.h"
46
#include "types.h"
jcaceres's avatar
jcaceres committed
47

48

49
50
/** \brief UDP implementation of DataProtocol class
 */
51
class UdpDataProtocol : public DataProtocol
jcaceres's avatar
jcaceres committed
52
53
{
public:
54

55
56
57
  /** \brief The class constructor 
   * \param runmode Sets the run mode, use either SENDER or RECEIVER
   */
jcacerec's avatar
jcacerec committed
58
  UdpDataProtocol(JackTrip* jacktrip, const runModeT runmode);
59
  
60
61
  /** \brief The class constructor 
   * \param runmode Sets the run mode, use either SENDER or RECEIVER
62
   * \param peerHostOrIP IPv4 number or host name
63
   */
64
  //UdpDataProtocol(const runModeT runmode, const char* peerHostOrIP);
65
66
67

  /** \brief The class destructor
   */
68
69
  virtual ~UdpDataProtocol() {};  

jcaceres's avatar
more    
jcaceres committed
70

71
72
  void setPeerAddress(char* peerHostOrIP);

jcaceres's avatar
more    
jcaceres committed
73
74
75
76
77
78
79
80
  /** \brief Receives a packet. It blocks until a packet is received
   *
   * This function makes sure we recieve a complete packet
   * of size n
   * \param buf Buffer to store the recieved packet
   * \param n size of packet to receive
   * \return number of bytes read, -1 on error
   */
81
  virtual int receivePacket(char* buf, const size_t n);
jcaceres's avatar
more    
jcaceres committed
82
83
84
85
86
87
88
89
90
  
  /** \brief Sends a packet
   *
   * This function meakes sure we send a complete packet
   * of size n
   * \param buff Buffer to send
   * \param n size of packet to receive
   * \return number of bytes read, -1 on error
   */
91
  virtual int sendPacket(const char* buf, const size_t n);
jcaceres's avatar
more    
jcaceres committed
92
  
93
94
95
  virtual void getPeerAddressFromFirstPacket(QHostAddress& peerHostAddress,
					     uint16_t& port);

96
97
  virtual void run();

jcaceres's avatar
jcaceres committed
98

jcaceres's avatar
jcaceres committed
99
private:
jcaceres's avatar
jcaceres committed
100

jcaceres's avatar
jcaceres committed
101
102
103
  /** \brief Binds the UDP socket to the available address and specified port
   */
  void bindSocket();
jcaceres's avatar
jcaceres committed
104

105
106
107
  int mLocalPort; ///< Local Port number to Bind
  int mPeerPort; ///< Peer Port number to Bind
  const runModeT mRunMode; ///< Run mode, either SENDER or RECEIVER
108

jcaceres's avatar
jcaceres committed
109
  //void setBindSocket();
110
111
112
113

  QUdpSocket mUdpSocket; ///< The UDP socket
  QHostAddress mPeerAddress; ///< The Peer Address

jcaceres's avatar
jcaceres committed
114
115
116
};

#endif