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
75ba961e
Commit
75ba961e
authored
Oct 19, 2013
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Client: move message functions into the class
parent
c2d5ce0c
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
64 additions
and
96 deletions
+64
-96
Makefile.am
Makefile.am
+1
-1
Client.hxx
src/Client.hxx
+24
-5
ClientSubscribe.cxx
src/ClientSubscribe.cxx
+30
-27
ClientSubscribe.hxx
src/ClientSubscribe.hxx
+0
-54
MessageCommands.cxx
src/MessageCommands.cxx
+9
-9
No files found.
Makefile.am
View file @
75ba961e
...
...
@@ -141,7 +141,7 @@ src_mpd_SOURCES = \
src/ClientRead.cxx
\
src/ClientWrite.cxx
\
src/ClientMessage.cxx src/ClientMessage.hxx
\
src/ClientSubscribe.cxx
src/ClientSubscribe.hxx
\
src/ClientSubscribe.cxx
\
src/ClientFile.cxx src/ClientFile.hxx
\
src/Listen.cxx src/Listen.hxx
\
src/LogInit.cxx src/LogInit.hxx
\
...
...
src/Client.hxx
View file @
75ba961e
...
...
@@ -87,11 +87,6 @@ public:
}
gcc_pure
bool
IsSubscribed
(
const
char
*
channel_name
)
const
{
return
subscriptions
.
find
(
channel_name
)
!=
subscriptions
.
end
();
}
gcc_pure
bool
IsExpired
()
const
{
return
!
FullyBufferedSocket
::
IsDefined
();
}
...
...
@@ -132,6 +127,30 @@ public:
void
IdleAdd
(
unsigned
flags
);
bool
IdleWait
(
unsigned
flags
);
enum
class
SubscribeResult
{
/** success */
OK
,
/** invalid channel name */
INVALID
,
/** already subscribed to this channel */
ALREADY
,
/** too many subscriptions */
FULL
,
};
gcc_pure
bool
IsSubscribed
(
const
char
*
channel_name
)
const
{
return
subscriptions
.
find
(
channel_name
)
!=
subscriptions
.
end
();
}
SubscribeResult
Subscribe
(
const
char
*
channel
);
bool
Unsubscribe
(
const
char
*
channel
);
void
UnsubscribeAll
();
bool
PushMessage
(
const
ClientMessage
&
msg
);
private
:
/* virtual methods from class BufferedSocket */
virtual
InputResult
OnSocketInput
(
void
*
data
,
size_t
length
)
override
;
...
...
src/ClientSubscribe.cxx
View file @
75ba961e
...
...
@@ -18,72 +18,75 @@
*/
#include "config.h"
#include "ClientSubscribe.hxx"
#include "ClientInternal.hxx"
#include "Idle.hxx"
#include <assert.h>
#include <string.h>
enum
client_subscribe_result
client_subscribe
(
Client
&
client
,
const
char
*
channel
)
bool
Unsubscribe
(
const
char
*
channel
);
void
UnsubscribeAll
();
bool
PushMessage
(
const
ClientMessage
&
msg
);
Client
::
SubscribeResult
Client
::
Subscribe
(
const
char
*
channel
)
{
assert
(
channel
!=
nullptr
);
if
(
!
client_message_valid_channel_name
(
channel
))
return
C
LIENT_SUBSCRIBE_
INVALID
;
return
C
lient
::
SubscribeResult
::
INVALID
;
if
(
client
.
num_subscriptions
>=
CLIENT_MAX_SUBSCRIPTIONS
)
return
C
LIENT_SUBSCRIBE_
FULL
;
if
(
num_subscriptions
>=
CLIENT_MAX_SUBSCRIPTIONS
)
return
C
lient
::
SubscribeResult
::
FULL
;
auto
r
=
client
.
subscriptions
.
insert
(
channel
);
auto
r
=
subscriptions
.
insert
(
channel
);
if
(
!
r
.
second
)
return
C
LIENT_SUBSCRIBE_
ALREADY
;
return
C
lient
::
SubscribeResult
::
ALREADY
;
++
client
.
num_subscriptions
;
++
num_subscriptions
;
idle_add
(
IDLE_SUBSCRIPTION
);
return
C
LIENT_SUBSCRIBE_
OK
;
return
C
lient
::
SubscribeResult
::
OK
;
}
bool
client_unsubscribe
(
Client
&
client
,
const
char
*
channel
)
Client
::
Unsubscribe
(
const
char
*
channel
)
{
const
auto
i
=
client
.
subscriptions
.
find
(
channel
);
if
(
i
==
client
.
subscriptions
.
end
())
const
auto
i
=
subscriptions
.
find
(
channel
);
if
(
i
==
subscriptions
.
end
())
return
false
;
assert
(
client
.
num_subscriptions
>
0
);
assert
(
num_subscriptions
>
0
);
client
.
subscriptions
.
erase
(
i
);
--
client
.
num_subscriptions
;
subscriptions
.
erase
(
i
);
--
num_subscriptions
;
idle_add
(
IDLE_SUBSCRIPTION
);
assert
((
client
.
num_subscriptions
==
0
)
==
client
.
subscriptions
.
empty
());
assert
((
num_subscriptions
==
0
)
==
subscriptions
.
empty
());
return
true
;
}
void
client_unsubscribe_all
(
Client
&
client
)
Client
::
UnsubscribeAll
(
)
{
client
.
subscriptions
.
clear
();
client
.
num_subscriptions
=
0
;
subscriptions
.
clear
();
num_subscriptions
=
0
;
}
bool
client_push_message
(
Client
&
client
,
const
ClientMessage
&
msg
)
Client
::
PushMessage
(
const
ClientMessage
&
msg
)
{
if
(
client
.
messages
.
size
()
>=
CLIENT_MAX_MESSAGES
||
!
client
.
IsSubscribed
(
msg
.
GetChannel
()))
if
(
messages
.
size
()
>=
CLIENT_MAX_MESSAGES
||
!
IsSubscribed
(
msg
.
GetChannel
()))
return
false
;
if
(
client
.
messages
.
empty
())
client
.
IdleAdd
(
IDLE_MESSAGE
);
if
(
messages
.
empty
())
IdleAdd
(
IDLE_MESSAGE
);
client
.
messages
.
push_back
(
msg
);
messages
.
push_back
(
msg
);
return
true
;
}
src/ClientSubscribe.hxx
deleted
100644 → 0
View file @
c2d5ce0c
/*
* Copyright (C) 2003-2013 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef MPD_CLIENT_SUBSCRIBE_HXX
#define MPD_CLIENT_SUBSCRIBE_HXX
#include "Compiler.h"
class
Client
;
class
ClientMessage
;
enum
client_subscribe_result
{
/** success */
CLIENT_SUBSCRIBE_OK
,
/** invalid channel name */
CLIENT_SUBSCRIBE_INVALID
,
/** already subscribed to this channel */
CLIENT_SUBSCRIBE_ALREADY
,
/** too many subscriptions */
CLIENT_SUBSCRIBE_FULL
,
};
enum
client_subscribe_result
client_subscribe
(
Client
&
client
,
const
char
*
channel
);
bool
client_unsubscribe
(
Client
&
client
,
const
char
*
channel
);
void
client_unsubscribe_all
(
Client
&
client
);
bool
client_push_message
(
Client
&
client
,
const
ClientMessage
&
msg
);
#endif
src/MessageCommands.cxx
View file @
75ba961e
...
...
@@ -19,7 +19,6 @@
#include "config.h"
#include "MessageCommands.hxx"
#include "ClientSubscribe.hxx"
#include "Client.hxx"
#include "ClientList.hxx"
#include "Instance.hxx"
...
...
@@ -37,28 +36,29 @@ handle_subscribe(Client &client, gcc_unused int argc, char *argv[])
{
assert
(
argc
==
2
);
switch
(
client
_subscribe
(
client
,
argv
[
1
]))
{
case
C
LIENT_SUBSCRIBE_
OK
:
switch
(
client
.
Subscribe
(
argv
[
1
]))
{
case
C
lient
:
:
SubscribeResult
::
OK
:
return
COMMAND_RETURN_OK
;
case
C
LIENT_SUBSCRIBE_
INVALID
:
case
C
lient
:
:
SubscribeResult
::
INVALID
:
command_error
(
client
,
ACK_ERROR_ARG
,
"invalid channel name"
);
return
COMMAND_RETURN_ERROR
;
case
C
LIENT_SUBSCRIBE_
ALREADY
:
case
C
lient
:
:
SubscribeResult
::
ALREADY
:
command_error
(
client
,
ACK_ERROR_EXIST
,
"already subscribed to this channel"
);
return
COMMAND_RETURN_ERROR
;
case
C
LIENT_SUBSCRIBE_
FULL
:
case
C
lient
:
:
SubscribeResult
::
FULL
:
command_error
(
client
,
ACK_ERROR_EXIST
,
"subscription list is full"
);
return
COMMAND_RETURN_ERROR
;
}
/* unreachable */
return
COMMAND_RETURN_OK
;
assert
(
false
);
gcc_unreachable
();
}
enum
command_return
...
...
@@ -66,7 +66,7 @@ handle_unsubscribe(Client &client, gcc_unused int argc, char *argv[])
{
assert
(
argc
==
2
);
if
(
client
_unsubscribe
(
client
,
argv
[
1
]))
if
(
client
.
Unsubscribe
(
argv
[
1
]))
return
COMMAND_RETURN_OK
;
else
{
command_error
(
client
,
ACK_ERROR_NO_EXIST
,
...
...
@@ -124,7 +124,7 @@ handle_send_message(Client &client,
bool
sent
=
false
;
const
ClientMessage
msg
(
argv
[
1
],
argv
[
2
]);
for
(
const
auto
&
c
:
*
instance
->
client_list
)
if
(
c
lient_push_message
(
*
c
,
msg
))
if
(
c
->
PushMessage
(
msg
))
sent
=
true
;
if
(
sent
)
...
...
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