Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mpd
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Иван Мажукин
mpd
Commits
26e4a40c
Commit
26e4a40c
authored
Feb 19, 2017
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
output/httpd/Page: use std::shared_ptr instead of class RefCount
parent
8b193107
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
42 additions
and
139 deletions
+42
-139
HttpdClient.cxx
src/output/plugins/httpd/HttpdClient.cxx
+7
-27
HttpdClient.hxx
src/output/plugins/httpd/HttpdClient.hxx
+6
-6
HttpdInternal.hxx
src/output/plugins/httpd/HttpdInternal.hxx
+5
-6
HttpdOutputPlugin.cxx
src/output/plugins/httpd/HttpdOutputPlugin.cxx
+10
-23
IcyMetaDataServer.cxx
src/output/plugins/httpd/IcyMetaDataServer.cxx
+3
-3
IcyMetaDataServer.hxx
src/output/plugins/httpd/IcyMetaDataServer.hxx
+2
-2
Page.cxx
src/output/plugins/httpd/Page.cxx
+3
-24
Page.hxx
src/output/plugins/httpd/Page.hxx
+6
-48
No files found.
src/output/plugins/httpd/HttpdClient.cxx
View file @
26e4a40c
...
@@ -33,16 +33,6 @@
...
@@ -33,16 +33,6 @@
HttpdClient
::~
HttpdClient
()
HttpdClient
::~
HttpdClient
()
{
{
if
(
state
==
RESPONSE
)
{
if
(
current_page
!=
nullptr
)
current_page
->
Unref
();
ClearQueue
();
}
if
(
metadata
)
metadata
->
Unref
();
if
(
IsDefined
())
if
(
IsDefined
())
BufferedSocket
::
Close
();
BufferedSocket
::
Close
();
}
}
...
@@ -217,15 +207,13 @@ HttpdClient::ClearQueue()
...
@@ -217,15 +207,13 @@ HttpdClient::ClearQueue()
assert
(
state
==
RESPONSE
);
assert
(
state
==
RESPONSE
);
while
(
!
pages
.
empty
())
{
while
(
!
pages
.
empty
())
{
Page
*
page
=
pages
.
front
();
pages
.
pop
();
#ifndef NDEBUG
#ifndef NDEBUG
auto
&
page
=
pages
.
front
();
assert
(
queue_size
>=
page
->
GetSize
());
assert
(
queue_size
>=
page
->
GetSize
());
queue_size
-=
page
->
GetSize
();
queue_size
-=
page
->
GetSize
();
#endif
#endif
page
->
Unref
();
page
s
.
pop
();
}
}
assert
(
queue_size
==
0
);
assert
(
queue_size
==
0
);
...
@@ -372,8 +360,7 @@ HttpdClient::TryWrite()
...
@@ -372,8 +360,7 @@ HttpdClient::TryWrite()
metadata_fill
+=
nbytes
;
metadata_fill
+=
nbytes
;
if
(
current_position
>=
current_page
->
GetSize
())
{
if
(
current_position
>=
current_page
->
GetSize
())
{
current_page
->
Unref
();
current_page
.
reset
();
current_page
=
nullptr
;
if
(
pages
.
empty
())
if
(
pages
.
empty
())
/* all pages are sent: remove the
/* all pages are sent: remove the
...
@@ -386,7 +373,7 @@ HttpdClient::TryWrite()
...
@@ -386,7 +373,7 @@ HttpdClient::TryWrite()
}
}
void
void
HttpdClient
::
PushPage
(
Page
*
page
)
HttpdClient
::
PushPage
(
Page
Ptr
page
)
{
{
if
(
state
!=
RESPONSE
)
if
(
state
!=
RESPONSE
)
/* the client is still writing the HTTP request */
/* the client is still writing the HTTP request */
...
@@ -398,25 +385,18 @@ HttpdClient::PushPage(Page *page)
...
@@ -398,25 +385,18 @@ HttpdClient::PushPage(Page *page)
ClearQueue
();
ClearQueue
();
}
}
page
->
Ref
();
pages
.
push
(
page
);
queue_size
+=
page
->
GetSize
();
queue_size
+=
page
->
GetSize
();
pages
.
emplace
(
std
::
move
(
page
));
ScheduleWrite
();
ScheduleWrite
();
}
}
void
void
HttpdClient
::
PushMetaData
(
Page
*
page
)
HttpdClient
::
PushMetaData
(
Page
Ptr
page
)
{
{
assert
(
page
!=
nullptr
);
assert
(
page
!=
nullptr
);
if
(
metadata
)
{
metadata
=
std
::
move
(
page
);
metadata
->
Unref
();
metadata
=
nullptr
;
}
page
->
Ref
();
metadata
=
page
;
metadata_sent
=
false
;
metadata_sent
=
false
;
}
}
...
...
src/output/plugins/httpd/HttpdClient.hxx
View file @
26e4a40c
...
@@ -20,6 +20,7 @@
...
@@ -20,6 +20,7 @@
#ifndef MPD_OUTPUT_HTTPD_CLIENT_HXX
#ifndef MPD_OUTPUT_HTTPD_CLIENT_HXX
#define MPD_OUTPUT_HTTPD_CLIENT_HXX
#define MPD_OUTPUT_HTTPD_CLIENT_HXX
#include "Page.hxx"
#include "event/BufferedSocket.hxx"
#include "event/BufferedSocket.hxx"
#include "Compiler.h"
#include "Compiler.h"
...
@@ -32,7 +33,6 @@
...
@@ -32,7 +33,6 @@
#include <stddef.h>
#include <stddef.h>
class
HttpdOutput
;
class
HttpdOutput
;
class
Page
;
class
HttpdClient
final
class
HttpdClient
final
:
BufferedSocket
,
:
BufferedSocket
,
...
@@ -59,7 +59,7 @@ class HttpdClient final
...
@@ -59,7 +59,7 @@ class HttpdClient final
/**
/**
* A queue of #Page objects to be sent to the client.
* A queue of #Page objects to be sent to the client.
*/
*/
std
::
queue
<
Page
*
,
std
::
list
<
Page
*
>>
pages
;
std
::
queue
<
Page
Ptr
,
std
::
list
<
PagePtr
>>
pages
;
/**
/**
* The sum of all page sizes in #pages.
* The sum of all page sizes in #pages.
...
@@ -69,7 +69,7 @@ class HttpdClient final
...
@@ -69,7 +69,7 @@ class HttpdClient final
/**
/**
* The #page which is currently being sent to the client.
* The #page which is currently being sent to the client.
*/
*/
Page
*
current_page
;
Page
Ptr
current_page
;
/**
/**
* The amount of bytes which were already sent from
* The amount of bytes which were already sent from
...
@@ -113,7 +113,7 @@ class HttpdClient final
...
@@ -113,7 +113,7 @@ class HttpdClient final
/**
/**
* The metadata as #Page which is currently being sent to the client.
* The metadata as #Page which is currently being sent to the client.
*/
*/
Page
*
metadata
;
Page
Ptr
metadata
;
/*
/*
* The amount of bytes which were already sent from the metadata.
* The amount of bytes which were already sent from the metadata.
...
@@ -178,12 +178,12 @@ public:
...
@@ -178,12 +178,12 @@ public:
/**
/**
* Appends a page to the client's queue.
* Appends a page to the client's queue.
*/
*/
void
PushPage
(
Page
*
page
);
void
PushPage
(
Page
Ptr
page
);
/**
/**
* Sends the passed metadata.
* Sends the passed metadata.
*/
*/
void
PushMetaData
(
Page
*
page
);
void
PushMetaData
(
Page
Ptr
page
);
private
:
private
:
void
ClearQueue
();
void
ClearQueue
();
...
...
src/output/plugins/httpd/HttpdInternal.hxx
View file @
26e4a40c
...
@@ -44,7 +44,6 @@ struct ConfigBlock;
...
@@ -44,7 +44,6 @@ struct ConfigBlock;
class
EventLoop
;
class
EventLoop
;
class
ServerSocket
;
class
ServerSocket
;
class
HttpdClient
;
class
HttpdClient
;
class
Page
;
class
PreparedEncoder
;
class
PreparedEncoder
;
class
Encoder
;
class
Encoder
;
struct
Tag
;
struct
Tag
;
...
@@ -102,12 +101,12 @@ private:
...
@@ -102,12 +101,12 @@ private:
/**
/**
* The header page, which is sent to every client on connect.
* The header page, which is sent to every client on connect.
*/
*/
Page
*
header
;
Page
Ptr
header
;
/**
/**
* The metadata, which is sent to every client.
* The metadata, which is sent to every client.
*/
*/
Page
*
metadata
;
Page
Ptr
metadata
;
/**
/**
* The page queue, i.e. pages from the encoder to be
* The page queue, i.e. pages from the encoder to be
...
@@ -115,7 +114,7 @@ private:
...
@@ -115,7 +114,7 @@ private:
* pass pages from the OutputThread to the IOThread. It is
* pass pages from the OutputThread to the IOThread. It is
* protected by #mutex, and removing signals #cond.
* protected by #mutex, and removing signals #cond.
*/
*/
std
::
queue
<
Page
*
,
std
::
list
<
Page
*
>>
pages
;
std
::
queue
<
Page
Ptr
,
std
::
list
<
PagePtr
>>
pages
;
public
:
public
:
/**
/**
...
@@ -234,14 +233,14 @@ public:
...
@@ -234,14 +233,14 @@ public:
* Reads data from the encoder (as much as available) and
* Reads data from the encoder (as much as available) and
* returns it as a new #page object.
* returns it as a new #page object.
*/
*/
Page
*
ReadPage
();
Page
Ptr
ReadPage
();
/**
/**
* Broadcasts a page struct to all clients.
* Broadcasts a page struct to all clients.
*
*
* Mutext must not be locked.
* Mutext must not be locked.
*/
*/
void
BroadcastPage
(
Page
*
page
);
void
BroadcastPage
(
Page
Ptr
page
);
/**
/**
* Broadcasts data from the encoder to all clients.
* Broadcasts data from the encoder to all clients.
...
...
src/output/plugins/httpd/HttpdOutputPlugin.cxx
View file @
26e4a40c
...
@@ -90,9 +90,6 @@ HttpdOutput::HttpdOutput(EventLoop &_loop, const ConfigBlock &block)
...
@@ -90,9 +90,6 @@ HttpdOutput::HttpdOutput(EventLoop &_loop, const ConfigBlock &block)
HttpdOutput
::~
HttpdOutput
()
HttpdOutput
::~
HttpdOutput
()
{
{
if
(
metadata
!=
nullptr
)
metadata
->
Unref
();
delete
prepared_encoder
;
delete
prepared_encoder
;
}
}
...
@@ -147,13 +144,11 @@ HttpdOutput::RunDeferred()
...
@@ -147,13 +144,11 @@ HttpdOutput::RunDeferred()
const
std
::
lock_guard
<
Mutex
>
protect
(
mutex
);
const
std
::
lock_guard
<
Mutex
>
protect
(
mutex
);
while
(
!
pages
.
empty
())
{
while
(
!
pages
.
empty
())
{
Page
*
page
=
pages
.
front
(
);
Page
Ptr
page
=
std
::
move
(
pages
.
front
()
);
pages
.
pop
();
pages
.
pop
();
for
(
auto
&
client
:
clients
)
for
(
auto
&
client
:
clients
)
client
.
PushPage
(
page
);
client
.
PushPage
(
page
);
page
->
Unref
();
}
}
/* wake up the client that may be waiting for the queue to be
/* wake up the client that may be waiting for the queue to be
...
@@ -204,7 +199,7 @@ HttpdOutput::OnAccept(int fd, SocketAddress address, gcc_unused int uid)
...
@@ -204,7 +199,7 @@ HttpdOutput::OnAccept(int fd, SocketAddress address, gcc_unused int uid)
}
}
}
}
Page
*
Page
Ptr
HttpdOutput
::
ReadPage
()
HttpdOutput
::
ReadPage
()
{
{
if
(
unflushed_input
>=
65536
)
{
if
(
unflushed_input
>=
65536
)
{
...
@@ -235,7 +230,7 @@ HttpdOutput::ReadPage()
...
@@ -235,7 +230,7 @@ HttpdOutput::ReadPage()
if
(
size
==
0
)
if
(
size
==
0
)
return
nullptr
;
return
nullptr
;
return
Page
::
Copy
(
buffer
,
size
);
return
std
::
make_shared
<
Page
>
(
buffer
,
size
);
}
}
inline
void
inline
void
...
@@ -282,8 +277,7 @@ HttpdOutput::Close()
...
@@ -282,8 +277,7 @@ HttpdOutput::Close()
clients
.
clear_and_dispose
(
DeleteDisposer
());
clients
.
clear_and_dispose
(
DeleteDisposer
());
});
});
if
(
header
!=
nullptr
)
header
.
reset
();
header
->
Unref
();
delete
encoder
;
delete
encoder
;
}
}
...
@@ -326,13 +320,12 @@ HttpdOutput::Delay() const
...
@@ -326,13 +320,12 @@ HttpdOutput::Delay() const
}
}
void
void
HttpdOutput
::
BroadcastPage
(
Page
*
page
)
HttpdOutput
::
BroadcastPage
(
Page
Ptr
page
)
{
{
assert
(
page
!=
nullptr
);
assert
(
page
!=
nullptr
);
mutex
.
lock
();
mutex
.
lock
();
pages
.
push
(
page
);
pages
.
emplace
(
std
::
move
(
page
));
page
->
Ref
();
mutex
.
unlock
();
mutex
.
unlock
();
DeferredMonitor
::
Schedule
();
DeferredMonitor
::
Schedule
();
...
@@ -346,7 +339,7 @@ HttpdOutput::BroadcastFromEncoder()
...
@@ -346,7 +339,7 @@ HttpdOutput::BroadcastFromEncoder()
while
(
!
pages
.
empty
())
while
(
!
pages
.
empty
())
cond
.
wait
(
mutex
);
cond
.
wait
(
mutex
);
Page
*
page
;
Page
Ptr
page
;
while
((
page
=
ReadPage
())
!=
nullptr
)
while
((
page
=
ReadPage
())
!=
nullptr
)
pages
.
push
(
page
);
pages
.
push
(
page
);
...
@@ -418,19 +411,14 @@ HttpdOutput::SendTag(const Tag &tag)
...
@@ -418,19 +411,14 @@ HttpdOutput::SendTag(const Tag &tag)
used as the new "header" page, which is sent to all
used as the new "header" page, which is sent to all
new clients */
new clients */
Page
*
page
=
ReadPage
();
auto
page
=
ReadPage
();
if
(
page
!=
nullptr
)
{
if
(
page
!=
nullptr
)
{
if
(
header
!=
nullptr
)
header
=
std
::
move
(
page
);
header
->
Unref
();
header
=
page
;
BroadcastPage
(
page
);
BroadcastPage
(
page
);
}
}
}
else
{
}
else
{
/* use Icy-Metadata */
/* use Icy-Metadata */
if
(
metadata
!=
nullptr
)
metadata
->
Unref
();
static
constexpr
TagType
types
[]
=
{
static
constexpr
TagType
types
[]
=
{
TAG_ALBUM
,
TAG_ARTIST
,
TAG_TITLE
,
TAG_ALBUM
,
TAG_ARTIST
,
TAG_TITLE
,
TAG_NUM_OF_ITEM_TYPES
TAG_NUM_OF_ITEM_TYPES
...
@@ -451,9 +439,8 @@ HttpdOutput::CancelAllClients()
...
@@ -451,9 +439,8 @@ HttpdOutput::CancelAllClients()
const
std
::
lock_guard
<
Mutex
>
protect
(
mutex
);
const
std
::
lock_guard
<
Mutex
>
protect
(
mutex
);
while
(
!
pages
.
empty
())
{
while
(
!
pages
.
empty
())
{
Page
*
page
=
pages
.
front
(
);
Page
Ptr
page
=
std
::
move
(
pages
.
front
()
);
pages
.
pop
();
pages
.
pop
();
page
->
Unref
();
}
}
for
(
auto
&
client
:
clients
)
for
(
auto
&
client
:
clients
)
...
...
src/output/plugins/httpd/IcyMetaDataServer.cxx
View file @
26e4a40c
...
@@ -19,7 +19,6 @@
...
@@ -19,7 +19,6 @@
#include "config.h"
#include "config.h"
#include "IcyMetaDataServer.hxx"
#include "IcyMetaDataServer.hxx"
#include "Page.hxx"
#include "tag/Tag.hxx"
#include "tag/Tag.hxx"
#include "util/FormatString.hxx"
#include "util/FormatString.hxx"
#include "util/AllocatedString.hxx"
#include "util/AllocatedString.hxx"
...
@@ -82,7 +81,7 @@ icy_server_metadata_string(const char *stream_title, const char* stream_url)
...
@@ -82,7 +81,7 @@ icy_server_metadata_string(const char *stream_title, const char* stream_url)
return
icy_metadata
;
return
icy_metadata
;
}
}
Page
*
Page
Ptr
icy_server_metadata_page
(
const
Tag
&
tag
,
const
TagType
*
types
)
icy_server_metadata_page
(
const
Tag
&
tag
,
const
TagType
*
types
)
{
{
const
char
*
tag_items
[
TAG_NUM_OF_ITEM_TYPES
];
const
char
*
tag_items
[
TAG_NUM_OF_ITEM_TYPES
];
...
@@ -113,5 +112,6 @@ icy_server_metadata_page(const Tag &tag, const TagType *types)
...
@@ -113,5 +112,6 @@ icy_server_metadata_page(const Tag &tag, const TagType *types)
if
(
icy_string
.
IsNull
())
if
(
icy_string
.
IsNull
())
return
nullptr
;
return
nullptr
;
return
Page
::
Copy
(
icy_string
.
c_str
(),
uint8_t
(
icy_string
[
0
])
*
16
+
1
);
return
std
::
make_shared
<
Page
>
(
icy_string
.
c_str
(),
uint8_t
(
icy_string
[
0
])
*
16
+
1
);
}
}
src/output/plugins/httpd/IcyMetaDataServer.hxx
View file @
26e4a40c
...
@@ -20,10 +20,10 @@
...
@@ -20,10 +20,10 @@
#ifndef MPD_ICY_META_DATA_SERVER_HXX
#ifndef MPD_ICY_META_DATA_SERVER_HXX
#define MPD_ICY_META_DATA_SERVER_HXX
#define MPD_ICY_META_DATA_SERVER_HXX
#include "Page.hxx"
#include "tag/Type.h"
#include "tag/Type.h"
struct
Tag
;
struct
Tag
;
class
Page
;
template
<
typename
T
>
class
AllocatedString
;
template
<
typename
T
>
class
AllocatedString
;
AllocatedString
<
char
>
AllocatedString
<
char
>
...
@@ -31,7 +31,7 @@ icy_server_metadata_header(const char *name,
...
@@ -31,7 +31,7 @@ icy_server_metadata_header(const char *name,
const
char
*
genre
,
const
char
*
url
,
const
char
*
genre
,
const
char
*
url
,
const
char
*
content_type
,
int
metaint
);
const
char
*
content_type
,
int
metaint
);
Page
*
Page
Ptr
icy_server_metadata_page
(
const
Tag
&
tag
,
const
TagType
*
types
);
icy_server_metadata_page
(
const
Tag
&
tag
,
const
TagType
*
types
);
#endif
#endif
src/output/plugins/httpd/Page.cxx
View file @
26e4a40c
...
@@ -24,29 +24,8 @@
...
@@ -24,29 +24,8 @@
#include <string.h>
#include <string.h>
#include <stdlib.h>
#include <stdlib.h>
Page
*
Page
::
Page
(
const
void
*
data
,
size_t
size
)
Page
::
Create
(
size_t
size
)
:
buffer
(
size
)
{
{
return
new
Page
(
size
);
memcpy
(
&
buffer
.
front
(),
data
,
size
);
}
Page
*
Page
::
Copy
(
const
void
*
data
,
size_t
size
)
{
assert
(
data
!=
nullptr
);
Page
*
page
=
Create
(
size
);
memcpy
(
&
page
->
buffer
.
front
(),
data
,
size
);
return
page
;
}
bool
Page
::
Unref
()
{
bool
unused
=
ref
.
Decrement
();
if
(
unused
)
delete
this
;
return
unused
;
}
}
src/output/plugins/httpd/Page.hxx
View file @
26e4a40c
...
@@ -17,17 +17,13 @@
...
@@ -17,17 +17,13 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
*/
/** \file
*
* This is a library which manages reference counted buffers.
*/
#ifndef MPD_PAGE_HXX
#ifndef MPD_PAGE_HXX
#define MPD_PAGE_HXX
#define MPD_PAGE_HXX
#include "util/RefCount.hxx"
#include "util/AllocatedArray.hxx"
#include "util/AllocatedArray.hxx"
#include <memory>
#include <stddef.h>
#include <stddef.h>
#include <stdint.h>
#include <stdint.h>
...
@@ -37,54 +33,14 @@
...
@@ -37,54 +33,14 @@
* instances hold references to one buffer.
* instances hold references to one buffer.
*/
*/
class
Page
{
class
Page
{
/**
* The number of references to this buffer. This library uses
* atomic functions to access it, i.e. no locks are required.
* As soon as this attribute reaches zero, the buffer is
* freed.
*/
RefCount
ref
;
AllocatedArray
<
uint8_t
>
buffer
;
AllocatedArray
<
uint8_t
>
buffer
;
p
rotected
:
p
ublic
:
explicit
Page
(
size_t
_size
)
:
buffer
(
_size
)
{}
explicit
Page
(
size_t
_size
)
:
buffer
(
_size
)
{}
explicit
Page
(
AllocatedArray
<
uint8_t
>
&&
_buffer
)
explicit
Page
(
AllocatedArray
<
uint8_t
>
&&
_buffer
)
:
buffer
(
std
::
move
(
_buffer
))
{}
:
buffer
(
std
::
move
(
_buffer
))
{}
~
Page
()
=
default
;
Page
(
const
void
*
data
,
size_t
size
);
/**
* Allocates a new #Page object, without filling the data
* element.
*/
static
Page
*
Create
(
size_t
size
);
public
:
/**
* Creates a new #page object, and copies data from the
* specified buffer. It is initialized with a reference count
* of 1.
*
* @param data the source buffer
* @param size the size of the source buffer
*/
static
Page
*
Copy
(
const
void
*
data
,
size_t
size
);
/**
* Increases the reference counter.
*/
void
Ref
()
{
ref
.
Increment
();
}
/**
* Decreases the reference counter. If it reaches zero, the #page is
* freed.
*
* @return true if the #page has been freed
*/
bool
Unref
();
size_t
GetSize
()
const
{
size_t
GetSize
()
const
{
return
buffer
.
size
();
return
buffer
.
size
();
...
@@ -95,4 +51,6 @@ public:
...
@@ -95,4 +51,6 @@ public:
}
}
};
};
typedef
std
::
shared_ptr
<
Page
>
PagePtr
;
#endif
#endif
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment