SSDP/src/include/Network/SSDP/Server.h
Go to the documentation of this file.
1 /****
2  * Server.h
3  *
4  * Copyright 2019 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the Sming SSDP Library
7  *
8  * This library is free software: you can redistribute it and/or modify it under the terms of the
9  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with this library.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  ****/
19 
20 #pragma once
21 
22 #include <Network/UdpConnection.h>
23 #include "MessageQueue.h"
24 #include <Data/CString.h>
25 
26 #define UPNP_VERSION_IS(ver) (F(MACROQUOTE(ver)) == MACROQUOTE(UPNP_VERSION))
27 
28 namespace SSDP
29 {
30 DECLARE_FSTR(BASE_SERVER_ID)
31 DECLARE_FSTR(defaultProductNameAndVersion);
32 
33 String getServerId(const String& productNameAndVersion);
34 
38 using ReceiveDelegate = Delegate<void(BasicMessage& message)>;
39 
47 using SendDelegate = Delegate<void(Message& msg, MessageSpec& ms)>;
48 
62 class Server : private UdpConnection
63 {
64 public:
65  static constexpr uint8_t multicastTtl{2};
66 
67  Server() : messageQueue(MessageDelegate(&Server::onMessage, this))
68  {
69  }
70 
76  bool begin(ReceiveDelegate receiveCallback, SendDelegate sendCallback);
77 
81  void end();
82 
87  bool isActive()
88  {
89  return active;
90  }
91 
95  bool sendMessage(const Message& msg);
96 
104 
108  void setProduct(const String& name, const String& version)
109  {
110  String s;
111  s += name;
112  s += '/';
113  s += version;
114  productNameAndVersion = s;
115  }
116 
117 public:
119 
120 protected:
121  void onReceive(pbuf* buf, IpAddress remoteIP, uint16_t remotePort) override;
122 
123 private:
124  /*
125  * Need a separate UDP connection for sending requests
126  */
127  class UdpOut : public UdpConnection
128  {
129  protected:
130  void onReceive(pbuf* buf, IpAddress remoteIP, uint16_t remotePort) override;
131  };
132 
133  void onTimer();
134  void onMessage(MessageSpec* ms);
135 
136  ReceiveDelegate receiveDelegate{nullptr};
137  SendDelegate sendDelegate{nullptr};
138  UdpOut out;
139  bool active{false};
140  CString productNameAndVersion;
141 };
142 
143 extern Server server;
144 
145 } // namespace SSDP
Class to manage a NUL-terminated C-style string When storing persistent strings in RAM the regular St...
Definition: CString.h:27
A class to make it easier to handle and pass around IP addresses.
Definition: IpAddress.h:45
Handles incoming messages.
Definition: SSDP/src/include/Network/SSDP/Message.h:62
Queue of objects managed by a single timer.
Definition: MessageQueue.h:37
Defines the information used to create an outgoing message.
Definition: MessageSpec.h:75
Message using regular HTTP header management class.
Definition: SSDP/src/include/Network/SSDP/Message.h:72
Listens for incoming messages and manages queue of outgoing messages.
Definition: SSDP/src/include/Network/SSDP/Server.h:63
Server()
Definition: SSDP/src/include/Network/SSDP/Server.h:67
void end()
Stop SSDP server.
MessageQueue messageQueue
Definition: SSDP/src/include/Network/SSDP/Server.h:118
bool buildMessage(Message &msg, MessageSpec &ms)
Construct a message from the given template spec.
bool begin(ReceiveDelegate receiveCallback, SendDelegate sendCallback)
Called from UPnP library to start SSDP server.
void setProduct(const String &name, const String &version)
Set product name and version contained in SSDP message USER-AGENT field.
Definition: SSDP/src/include/Network/SSDP/Server.h:108
void onReceive(pbuf *buf, IpAddress remoteIP, uint16_t remotePort) override
bool isActive()
Determine if server is running.
Definition: SSDP/src/include/Network/SSDP/Server.h:87
bool sendMessage(const Message &msg)
Send a message immediately.
The String class.
Definition: WString.h:137
Definition: UdpConnection.h:28
Definition: SSDP/src/include/Network/SSDP/Message.h:32
DECLARE_FSTR(SSDP_DISCOVER)
String getServerId(const String &productNameAndVersion)
Server server
Delegate< void(Message &msg, MessageSpec &ms)> SendDelegate
Callback type for sending outgoing message.
Definition: SSDP/src/include/Network/SSDP/Server.h:47