1 | // %flair:license{
|
---|
2 | // This file is part of the Flair framework distributed under the
|
---|
3 | // CECILL-C License, Version 1.0.
|
---|
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 |
|
---|
13 | #ifndef SOCKET_H
|
---|
14 | #define SOCKET_H
|
---|
15 |
|
---|
16 | #include <unistd.h>
|
---|
17 | #include <stdint.h>
|
---|
18 | #include <Object.h>
|
---|
19 |
|
---|
20 | class Socket_impl;
|
---|
21 |
|
---|
22 | namespace flair
|
---|
23 | {
|
---|
24 | namespace core
|
---|
25 | {
|
---|
26 |
|
---|
27 | /*! \class Socket
|
---|
28 | *
|
---|
29 | * \brief Class encapsulating a UDP socket. It assumes packets are coming from only one distant host on a given port.
|
---|
30 | *
|
---|
31 | */
|
---|
32 | class Socket: public Object {
|
---|
33 | public:
|
---|
34 | /*!
|
---|
35 | * \brief Constructor
|
---|
36 | *
|
---|
37 | * Construct the client side of the socket
|
---|
38 | *
|
---|
39 | * \param parent parent
|
---|
40 | * \param name name
|
---|
41 | * \param address server address (ex 192.168.1.1:9000)
|
---|
42 | * \param broadcast true if address is a broadcast address
|
---|
43 | */
|
---|
44 | Socket(const Object* parent, std::string name,std::string address,bool broadcast=false);
|
---|
45 |
|
---|
46 | /*!
|
---|
47 | * \brief Constructor
|
---|
48 | *
|
---|
49 | * Construct the server side of the socket
|
---|
50 | *
|
---|
51 | * \param parent parent
|
---|
52 | * \param name name
|
---|
53 | * \param port listening port
|
---|
54 | */
|
---|
55 | Socket(const Object* parent, std::string name,uint16_t port);
|
---|
56 |
|
---|
57 | /*!
|
---|
58 | * \brief Destructor
|
---|
59 | *
|
---|
60 | */
|
---|
61 | ~Socket();
|
---|
62 |
|
---|
63 | /*!
|
---|
64 | * \brief Send a message
|
---|
65 | *
|
---|
66 | * In case of a broadcast Socket, Parent()->ObjectName() is used as source of the message, this name should be unique.
|
---|
67 | *
|
---|
68 | * \param message message
|
---|
69 | */
|
---|
70 | void SendMessage(std::string message);
|
---|
71 |
|
---|
72 | /*!
|
---|
73 | * \brief Send a message
|
---|
74 | *
|
---|
75 | * \param message message
|
---|
76 | * \param message_len message length
|
---|
77 | */
|
---|
78 | void SendMessage(const char* message,size_t message_len);
|
---|
79 |
|
---|
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
|
---|
85 | * copied in the src buffer. \n
|
---|
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
|
---|
94 | *
|
---|
95 | * \return size of the received message
|
---|
96 | */
|
---|
97 | ssize_t RecvMessage(char* buf,size_t buf_len,Time timeout,char* src=NULL,size_t* src_len=NULL);
|
---|
98 |
|
---|
99 | void NetworkToHost(char *data,size_t dataSize);
|
---|
100 | void HostToNetwork(char *data,size_t dataSize);
|
---|
101 |
|
---|
102 | private:
|
---|
103 | class Socket_impl* pimpl_;
|
---|
104 | };
|
---|
105 |
|
---|
106 | } // end namespace core
|
---|
107 | } // end namespace flair
|
---|
108 |
|
---|
109 | #endif // SOCKET_H
|
---|