[2] | 1 | // %flair:license{
|
---|
[15] | 2 | // This file is part of the Flair framework distributed under the
|
---|
| 3 | // CECILL-C License, Version 1.0.
|
---|
[2] | 4 | // %flair:license}
|
---|
| 5 | /*!
|
---|
| 6 | * \file Socket.h
|
---|
| 7 | * \brief Class defining a UDP socket
|
---|
| 8 | * \author Guillaume Sanahuja, Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
| 9 | * \date 2013/11/17
|
---|
| 10 | * \version 4.0
|
---|
| 11 | */
|
---|
| 12 |
|
---|
[178] | 13 | #ifndef UDPSOCKET_H
|
---|
| 14 | #define UDPSOCKET_H
|
---|
[2] | 15 |
|
---|
| 16 | #include <Object.h>
|
---|
| 17 |
|
---|
[178] | 18 | class UdpSocket_impl;
|
---|
[2] | 19 |
|
---|
[15] | 20 | namespace flair {
|
---|
| 21 | namespace core {
|
---|
[2] | 22 |
|
---|
[178] | 23 | /*! \class UdpSocket
|
---|
[15] | 24 | *
|
---|
| 25 | * \brief Class encapsulating a UDP socket. It assumes packets are coming from
|
---|
| 26 | *only one distant host on a given port.
|
---|
| 27 | *
|
---|
| 28 | */
|
---|
[178] | 29 | class UdpSocket : public Object {
|
---|
[15] | 30 | public:
|
---|
| 31 | /*!
|
---|
| 32 | * \brief Constructor
|
---|
| 33 | *
|
---|
| 34 | * Construct the client side of the socket
|
---|
| 35 | *
|
---|
| 36 | * \param parent parent
|
---|
| 37 | * \param name name
|
---|
| 38 | * \param address server address (ex 192.168.1.1:9000)
|
---|
| 39 | * \param broadcast true if address is a broadcast address
|
---|
| 40 | */
|
---|
[178] | 41 | UdpSocket(const Object *parent, std::string name, std::string address,
|
---|
[15] | 42 | bool broadcast = false);
|
---|
[2] | 43 |
|
---|
[15] | 44 | /*!
|
---|
| 45 | * \brief Constructor
|
---|
| 46 | *
|
---|
| 47 | * Construct the server side of the socket
|
---|
| 48 | *
|
---|
| 49 | * \param parent parent
|
---|
| 50 | * \param name name
|
---|
| 51 | * \param port listening port
|
---|
| 52 | */
|
---|
[178] | 53 | UdpSocket(const Object *parent, std::string name, uint16_t port);
|
---|
[2] | 54 |
|
---|
[15] | 55 | /*!
|
---|
| 56 | * \brief Destructor
|
---|
| 57 | *
|
---|
| 58 | */
|
---|
[178] | 59 | ~UdpSocket();
|
---|
[2] | 60 |
|
---|
[15] | 61 | /*!
|
---|
| 62 | * \brief Send a message
|
---|
| 63 | *
|
---|
| 64 | * In case of a broadcast Socket, Parent()->ObjectName() is used as source of
|
---|
| 65 | *the message, this name should be unique.
|
---|
| 66 | *
|
---|
| 67 | * \param message message
|
---|
| 68 | */
|
---|
| 69 | void SendMessage(std::string message);
|
---|
[2] | 70 |
|
---|
[15] | 71 | /*!
|
---|
| 72 | * \brief Send a message
|
---|
| 73 | *
|
---|
| 74 | * \param message message
|
---|
| 75 | * \param message_len message length
|
---|
[363] | 76 | * \param dst_id id of the dst if multiple connection (quick and dirty hack for sido, only works on NRT)
|
---|
[15] | 77 | */
|
---|
[363] | 78 | void SendMessage(const char *message, size_t message_len,int dst_id=0);
|
---|
[2] | 79 |
|
---|
[15] | 80 | /*!
|
---|
| 81 | * \brief Receive a message
|
---|
| 82 | *
|
---|
| 83 | * Receive a message and wait up to timeout. \n
|
---|
| 84 | * If src and src_len are specified, the source of the message will be
|
---|
[363] | 85 | * copied in the src buffer. Source is the Flair name of sender\n
|
---|
[15] | 86 | * Note that in case of a broadcast socket, own messages are filtered and
|
---|
| 87 | * are not received.
|
---|
| 88 | *
|
---|
| 89 | * \param buf buffer to put the message
|
---|
| 90 | * \param buf_len buffer length
|
---|
| 91 | * \param timeout timeout
|
---|
| 92 | * \param src buffer to put source name
|
---|
| 93 | * \param src_len buffer length
|
---|
[363] | 94 | * \param src_id id of the src, to be used for sending if multiple connection (quick and dirty hack for sido, only works on NRT)
|
---|
[15] | 95 | *
|
---|
| 96 | * \return size of the received message
|
---|
| 97 | */
|
---|
| 98 | ssize_t RecvMessage(char *buf, size_t buf_len, Time timeout, char *src = NULL,
|
---|
[363] | 99 | size_t *src_len = NULL,int *src_id=NULL);
|
---|
[2] | 100 |
|
---|
[15] | 101 | void NetworkToHost(char *data, size_t dataSize);
|
---|
| 102 | void HostToNetwork(char *data, size_t dataSize);
|
---|
[2] | 103 |
|
---|
[15] | 104 | private:
|
---|
[178] | 105 | class UdpSocket_impl *pimpl_;
|
---|
[15] | 106 | };
|
---|
[2] | 107 |
|
---|
| 108 | } // end namespace core
|
---|
| 109 | } // end namespace flair
|
---|
| 110 |
|
---|
[178] | 111 | #endif // UDPSOCKET_H
|
---|