1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
//! Commands are the top-level IPC structure used in the protocol.

use std::{
    ffi::CString,
    io::{BufRead, Write},
};

mod auth;
mod client_info;
mod lookup;
mod module_info;
mod playback_stream;
mod playback_stream_events;
mod record_stream;
mod sample_info;
mod server_info;
mod set_client_name;
mod sink_info;
mod sink_input_info;
mod source_info;
mod source_output_info;
mod stat;
mod subscribe;
mod timing_info;
mod upload_stream;

pub use auth::*;
pub use client_info::*;
pub use lookup::*;
pub use module_info::*;
pub use playback_stream::*;
pub use playback_stream_events::*;
pub use record_stream::*;
pub use sample_info::*;
pub use server_info::*;
pub use set_client_name::*;
pub use sink_info::*;
pub use sink_input_info::*;
pub use source_info::*;
pub use source_output_info::*;
pub use stat::*;
pub use subscribe::*;
pub use timing_info::*;
pub use upload_stream::*;

use super::{serde::*, ProtocolError, PulseError};

use enum_primitive_derive::Primitive;

/// A tag describing a command payload.
#[allow(missing_docs)]
#[repr(u8)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Primitive)]
pub enum CommandTag {
    /* Generic commands */
    Error = 0,
    Timeout = 1, /* pseudo command */
    Reply = 2,   /* actually used for command replies */

    /* CLIENT->SERVER */
    CreatePlaybackStream = 3, /* Payload changed in v9, v12 (0.9.0, 0.9.8) */
    DeletePlaybackStream = 4,
    CreateRecordStream = 5, /* Payload changed in v9, v12 (0.9.0, 0.9.8) */
    DeleteRecordStream = 6,
    Exit = 7,
    Auth = 8,
    SetClientName = 9,
    LookupSink = 10,
    LookupSource = 11,
    DrainPlaybackStream = 12,
    Stat = 13,
    GetPlaybackLatency = 14,
    CreateUploadStream = 15,
    DeleteUploadStream = 16,
    FinishUploadStream = 17,
    PlaySample = 18,
    RemoveSample = 19,

    GetServerInfo = 20,
    GetSinkInfo = 21,
    GetSinkInfoList = 22,
    GetSourceInfo = 23,
    GetSourceInfoList = 24,
    GetModuleInfo = 25,
    GetModuleInfoList = 26,
    GetClientInfo = 27,
    GetClientInfoList = 28,
    GetSinkInputInfo = 29,     /* Payload changed in v11 (0.9.7) */
    GetSinkInputInfoList = 30, /* Payload changed in v11 (0.9.7) */
    GetSourceOutputInfo = 31,
    GetSourceOutputInfoList = 32,
    GetSampleInfo = 33,
    GetSampleInfoList = 34,
    Subscribe = 35,

    SetSinkVolume = 36,
    SetSinkInputVolume = 37,
    SetSourceVolume = 38,

    SetSinkMute = 39,
    SetSourceMute = 40,

    CorkPlaybackStream = 41,
    FlushPlaybackStream = 42,
    TriggerPlaybackStream = 43,

    SetDefaultSink = 44,
    SetDefaultSource = 45,

    SetPlaybackStreamName = 46,
    SetRecordStreamName = 47,

    KillClient = 48,
    KillSinkInput = 49,
    KillSourceOutput = 50,

    LoadModule = 51,
    UnloadModule = 52,

    /* Obsolete */
    AddAutoloadObsolete = 53,
    RemoveAutoloadObsolete = 54,
    GetAutoloadInfoObsolete = 55,
    GetAutoloadInfoListObsolete = 56,

    GetRecordLatency = 57,
    CorkRecordStream = 58,
    FlushRecordStream = 59,
    PrebufPlaybackStream = 60,

    /* SERVER->CLIENT */
    Request = 61,
    Overflow = 62,
    Underflow = 63,
    PlaybackStreamKilled = 64,
    RecordStreamKilled = 65,
    SubscribeEvent = 66,

    /* A few more client->server commands */

    /* Supported since protocol v10 (0.9.5) */
    MoveSinkInput = 67,
    MoveSourceOutput = 68,

    /* Supported since protocol v11 (0.9.7) */
    SetSinkInputMute = 69,

    SuspendSink = 70,
    SuspendSource = 71,

    /* Supported since protocol v12 (0.9.8) */
    SetPlaybackStreamBufferAttr = 72,
    SetRecordStreamBufferAttr = 73,

    UpdatePlaybackStreamSampleRate = 74,
    UpdateRecordStreamSampleRate = 75,

    /* SERVER->CLIENT */
    PlaybackStreamSuspended = 76,
    RecordStreamSuspended = 77,
    PlaybackStreamMoved = 78,
    RecordStreamMoved = 79,

    /* Supported since protocol v13 (0.9.11) */
    UpdateRecordStreamProplist = 80,
    UpdatePlaybackStreamProplist = 81,
    UpdateClientProplist = 82,
    RemoveRecordStreamProplist = 83,
    RemovePlaybackStreamProplist = 84,
    RemoveClientProplist = 85,

    /* SERVER->CLIENT */
    Started = 86,

    /* Supported since protocol v14 (0.9.12) */
    Extension = 87,

    /* Supported since protocol v15 (0.9.15) */
    GetCardInfo = 88,
    GetCardInfoList = 89,
    SetCardProfile = 90,

    ClientEvent = 91,
    PlaybackStreamEvent = 92,
    RecordStreamEvent = 93,

    /* SERVER->CLIENT */
    PlaybackBufferAttrChanged = 94,
    RecordBufferAttrChanged = 95,

    /* Supported since protocol v16 (0.9.16) */
    SetSinkPort = 96,
    SetSourcePort = 97,

    /* Supported since protocol v22 (1.0) */
    SetSourceOutputVolume = 98,
    SetSourceOutputMute = 99,

    /* Supported since protocol v27 (3.0) */
    SetPortLatencyOffset = 100,

    /* Supported since protocol v30 (6.0) */
    /* BOTH DIRECTIONS */
    EnableSrbchannel = 101,
    DisableSrbchannel = 102,

    /* Supported since protocol v31 (9.0)
     * BOTH DIRECTIONS */
    RegisterMemfdShmid = 103,
}

/// A marker trait for reply data.
pub trait CommandReply: TagStructRead + TagStructWrite {}

#[derive(Debug, Clone, PartialEq)]
#[allow(missing_docs)]
pub enum Command {
    /// An error reply to some other command.
    Error(PulseError),
    Timeout,
    Exit,

    // A reply to some other command. If this is returned by [`Command::read_tag_prefixed`], the payload has yet to be read.
    Reply,

    // Authentication request (and protocol handshake).
    Auth(AuthParams),

    // Updates client properties (not just the name).
    SetClientName(Props),

    // Create and delete streams.
    CreatePlaybackStream(PlaybackStreamParams),
    DeletePlaybackStream(u32),
    CreateRecordStream(RecordStreamParams),
    DeleteRecordStream(u32),
    DrainPlaybackStream(u32),
    GetPlaybackLatency(LatencyParams),
    GetRecordLatency(LatencyParams),
    CreateUploadStream(UploadStreamParams),
    DeleteUploadStream(u32),
    FinishUploadStream(u32),

    // So-called introspection commands, to read back the state of the server.
    Stat,
    GetServerInfo,
    GetSinkInfo(GetSinkInfo),
    GetSinkInfoList,
    GetSourceInfo(GetSourceInfo),
    GetSourceInfoList,
    GetModuleInfo(u32),
    GetModuleInfoList,
    GetClientInfo(u32),
    GetClientInfoList,
    GetSinkInputInfo(u32),
    GetSinkInputInfoList,
    GetSourceOutputInfo(u32),
    GetSourceOutputInfoList,
    GetSampleInfo(u32),
    GetSampleInfoList,
    Subscribe(SubscriptionMask),
    LookupSink(CString),
    LookupSource(CString),

    // Events from the server to the client.
    Request(Request),
    Overflow(u32),
    Underflow(Underflow),
    PlaybackStreamKilled(u32),
    RecordStreamKilled(u32),
    Started(u32),
    PlaybackBufferAttrChanged(PlaybackBufferAttrChanged),
    SubscribeEvent(SubscriptionEvent),
}

impl Command {
    /// Read a command message from a tagstruct. A result of [`Command::Reply`]
    /// indicates that the payload has yet to be read.
    pub fn read_tag_prefixed<R: BufRead>(
        r: &mut R,
        protocol_version: u16,
    ) -> Result<(u32, Self), ProtocolError> {
        let mut ts = TagStructReader::new(r, protocol_version);
        let (command, seq) = (ts.read_enum()?, ts.read_u32()?);

        let cmd = match command {
            CommandTag::Error => Err(ProtocolError::ServerError(ts.read_enum()?)),
            CommandTag::Timeout => Err(ProtocolError::Timeout),
            CommandTag::Reply => Ok(Command::Reply),

            CommandTag::Exit => Ok(Command::Exit),
            CommandTag::Auth => Ok(Command::Auth(ts.read()?)),
            CommandTag::SetClientName => Ok(Command::SetClientName(ts.read()?)),

            CommandTag::CreatePlaybackStream => Ok(Command::CreatePlaybackStream(ts.read()?)),
            CommandTag::DeletePlaybackStream => Ok(Command::DeletePlaybackStream(ts.read_u32()?)),
            CommandTag::CreateRecordStream => Ok(Command::CreateRecordStream(ts.read()?)),
            CommandTag::DeleteRecordStream => Ok(Command::DeleteRecordStream(ts.read_u32()?)),
            CommandTag::LookupSink => Ok(Command::LookupSink(ts.read_string_non_null()?)),
            CommandTag::LookupSource => Ok(Command::LookupSource(ts.read_string_non_null()?)),
            CommandTag::DrainPlaybackStream => Ok(Command::DrainPlaybackStream(ts.read_u32()?)),
            CommandTag::Stat => Ok(Command::Stat),
            CommandTag::GetPlaybackLatency => Ok(Command::GetPlaybackLatency(ts.read()?)),
            CommandTag::CreateUploadStream => Ok(Command::CreateUploadStream(ts.read()?)),
            CommandTag::DeleteUploadStream => Ok(Command::DeleteUploadStream(ts.read_u32()?)),
            CommandTag::FinishUploadStream => Ok(Command::FinishUploadStream(ts.read_u32()?)),
            CommandTag::PlaySample => Err(ProtocolError::Unimplemented(command)),
            CommandTag::RemoveSample => Err(ProtocolError::Unimplemented(command)),

            CommandTag::GetServerInfo => Ok(Command::GetServerInfo),
            CommandTag::GetSinkInfo => Ok(Command::GetSinkInfo(ts.read()?)),
            CommandTag::GetSinkInfoList => Ok(Command::GetSinkInfoList),
            CommandTag::GetSourceInfo => Ok(Command::GetSourceInfo(ts.read()?)),
            CommandTag::GetSourceInfoList => Ok(Command::GetSourceInfoList),
            CommandTag::GetModuleInfo => Ok(Command::GetModuleInfo(ts.read_u32()?)),
            CommandTag::GetModuleInfoList => Ok(Command::GetModuleInfoList),
            CommandTag::GetClientInfo => Ok(Command::GetClientInfo(ts.read_u32()?)),
            CommandTag::GetClientInfoList => Ok(Command::GetClientInfoList),
            CommandTag::GetSinkInputInfo => Ok(Command::GetSinkInputInfo(ts.read_u32()?)),
            CommandTag::GetSinkInputInfoList => Ok(Command::GetSinkInputInfoList),
            CommandTag::GetSourceOutputInfo => Ok(Command::GetSourceOutputInfo(ts.read_u32()?)),
            CommandTag::GetSourceOutputInfoList => Ok(Command::GetSourceOutputInfoList),
            CommandTag::GetSampleInfo => Ok(Command::GetSampleInfo(ts.read_u32()?)),
            CommandTag::GetSampleInfoList => Ok(Command::GetSampleInfoList),
            CommandTag::Subscribe => Ok(Command::Subscribe(ts.read()?)),
            CommandTag::SubscribeEvent => Ok(Command::SubscribeEvent(ts.read()?)),

            CommandTag::Request => Ok(Command::Request(ts.read()?)),
            CommandTag::Overflow => Ok(Command::Overflow(ts.read_u32()?)),
            CommandTag::Underflow => Ok(Command::Underflow(ts.read()?)),
            CommandTag::PlaybackStreamKilled => Ok(Command::PlaybackStreamKilled(ts.read_u32()?)),
            CommandTag::RecordStreamKilled => Ok(Command::RecordStreamKilled(ts.read_u32()?)),
            CommandTag::Started => Ok(Command::Started(ts.read_u32()?)),
            CommandTag::PlaybackBufferAttrChanged => {
                Ok(Command::PlaybackBufferAttrChanged(ts.read()?))
            }

            CommandTag::SetSinkVolume => Err(ProtocolError::Unimplemented(command)),
            CommandTag::SetSinkInputVolume => Err(ProtocolError::Unimplemented(command)),
            CommandTag::SetSourceVolume => Err(ProtocolError::Unimplemented(command)),
            CommandTag::SetSinkMute => Err(ProtocolError::Unimplemented(command)),
            CommandTag::SetSourceMute => Err(ProtocolError::Unimplemented(command)),
            CommandTag::CorkPlaybackStream => Err(ProtocolError::Unimplemented(command)),
            CommandTag::FlushPlaybackStream => Err(ProtocolError::Unimplemented(command)),
            CommandTag::TriggerPlaybackStream => Err(ProtocolError::Unimplemented(command)),
            CommandTag::SetDefaultSink => Err(ProtocolError::Unimplemented(command)),
            CommandTag::SetDefaultSource => Err(ProtocolError::Unimplemented(command)),
            CommandTag::SetPlaybackStreamName => Err(ProtocolError::Unimplemented(command)),
            CommandTag::SetRecordStreamName => Err(ProtocolError::Unimplemented(command)),
            CommandTag::KillClient => Err(ProtocolError::Unimplemented(command)),
            CommandTag::KillSinkInput => Err(ProtocolError::Unimplemented(command)),
            CommandTag::KillSourceOutput => Err(ProtocolError::Unimplemented(command)),
            CommandTag::LoadModule => Err(ProtocolError::Unimplemented(command)),
            CommandTag::UnloadModule => Err(ProtocolError::Unimplemented(command)),
            CommandTag::AddAutoloadObsolete => Err(ProtocolError::Unimplemented(command)),
            CommandTag::RemoveAutoloadObsolete => Err(ProtocolError::Unimplemented(command)),
            CommandTag::GetAutoloadInfoObsolete => Err(ProtocolError::Unimplemented(command)),
            CommandTag::GetAutoloadInfoListObsolete => Err(ProtocolError::Unimplemented(command)),
            CommandTag::GetRecordLatency => Err(ProtocolError::Unimplemented(command)),
            CommandTag::CorkRecordStream => Err(ProtocolError::Unimplemented(command)),
            CommandTag::FlushRecordStream => Err(ProtocolError::Unimplemented(command)),
            CommandTag::PrebufPlaybackStream => Err(ProtocolError::Unimplemented(command)),
            CommandTag::MoveSinkInput => Err(ProtocolError::Unimplemented(command)),
            CommandTag::MoveSourceOutput => Err(ProtocolError::Unimplemented(command)),
            CommandTag::SetSinkInputMute => Err(ProtocolError::Unimplemented(command)),
            CommandTag::SuspendSink => Err(ProtocolError::Unimplemented(command)),
            CommandTag::SuspendSource => Err(ProtocolError::Unimplemented(command)),
            CommandTag::SetPlaybackStreamBufferAttr => Err(ProtocolError::Unimplemented(command)),
            CommandTag::SetRecordStreamBufferAttr => Err(ProtocolError::Unimplemented(command)),
            CommandTag::UpdatePlaybackStreamSampleRate => {
                Err(ProtocolError::Unimplemented(command))
            }
            CommandTag::UpdateRecordStreamSampleRate => Err(ProtocolError::Unimplemented(command)),
            CommandTag::PlaybackStreamSuspended => Err(ProtocolError::Unimplemented(command)),
            CommandTag::RecordStreamSuspended => Err(ProtocolError::Unimplemented(command)),
            CommandTag::PlaybackStreamMoved => Err(ProtocolError::Unimplemented(command)),
            CommandTag::RecordStreamMoved => Err(ProtocolError::Unimplemented(command)),
            CommandTag::UpdateRecordStreamProplist => Err(ProtocolError::Unimplemented(command)),
            CommandTag::UpdatePlaybackStreamProplist => Err(ProtocolError::Unimplemented(command)),
            CommandTag::UpdateClientProplist => Err(ProtocolError::Unimplemented(command)),
            CommandTag::RemoveRecordStreamProplist => Err(ProtocolError::Unimplemented(command)),
            CommandTag::RemovePlaybackStreamProplist => Err(ProtocolError::Unimplemented(command)),
            CommandTag::RemoveClientProplist => Err(ProtocolError::Unimplemented(command)),
            CommandTag::Extension => Err(ProtocolError::Unimplemented(command)),
            CommandTag::GetCardInfo => Err(ProtocolError::Unimplemented(command)),
            CommandTag::GetCardInfoList => Err(ProtocolError::Unimplemented(command)),
            CommandTag::SetCardProfile => Err(ProtocolError::Unimplemented(command)),
            CommandTag::ClientEvent => Err(ProtocolError::Unimplemented(command)),
            CommandTag::PlaybackStreamEvent => Err(ProtocolError::Unimplemented(command)),
            CommandTag::RecordStreamEvent => Err(ProtocolError::Unimplemented(command)),

            CommandTag::RecordBufferAttrChanged => Err(ProtocolError::Unimplemented(command)),

            CommandTag::SetSinkPort => Err(ProtocolError::Unimplemented(command)),
            CommandTag::SetSourcePort => Err(ProtocolError::Unimplemented(command)),
            CommandTag::SetSourceOutputVolume => Err(ProtocolError::Unimplemented(command)),
            CommandTag::SetSourceOutputMute => Err(ProtocolError::Unimplemented(command)),
            CommandTag::SetPortLatencyOffset => Err(ProtocolError::Unimplemented(command)),
            CommandTag::EnableSrbchannel => Err(ProtocolError::Unimplemented(command)),
            CommandTag::DisableSrbchannel => Err(ProtocolError::Unimplemented(command)),
            CommandTag::RegisterMemfdShmid => Err(ProtocolError::Unimplemented(command)),
        }?;

        Ok((seq, cmd))
    }

    /// Write a command message as a tagstruct. In the case of a [`Command::Reply`], the payload must be written separately.
    pub fn write_tag_prefixed<W: Write>(
        &self,
        seq: u32,
        w: &mut W,
        protocol_version: u16,
    ) -> Result<(), ProtocolError> {
        let mut ts = TagStructWriter::new(w, protocol_version);

        ts.write_u32(self.tag() as u32)?;
        ts.write_u32(seq)?;
        ts.write(self)?;

        Ok(())
    }

    /// The matching tag for this command.
    pub fn tag(&self) -> CommandTag {
        match self {
            Command::Error(_) => CommandTag::Error,
            Command::Timeout => CommandTag::Timeout,
            Command::Exit => CommandTag::Exit,
            Command::Reply => CommandTag::Reply,

            Command::Auth(_) => CommandTag::Auth,
            Command::SetClientName(_) => CommandTag::SetClientName,
            Command::CreatePlaybackStream(_) => CommandTag::CreatePlaybackStream,
            Command::DeletePlaybackStream(_) => CommandTag::DeletePlaybackStream,
            Command::CreateRecordStream(_) => CommandTag::CreateRecordStream,
            Command::DeleteRecordStream(_) => CommandTag::DeleteRecordStream,
            Command::DrainPlaybackStream(_) => CommandTag::DrainPlaybackStream,
            Command::GetPlaybackLatency(_) => CommandTag::GetPlaybackLatency,
            Command::GetRecordLatency(_) => CommandTag::GetRecordLatency,
            Command::CreateUploadStream(_) => CommandTag::CreateUploadStream,
            Command::DeleteUploadStream(_) => CommandTag::DeleteUploadStream,
            Command::FinishUploadStream(_) => CommandTag::FinishUploadStream,

            Command::Stat => CommandTag::Stat,
            Command::GetServerInfo => CommandTag::GetServerInfo,
            Command::GetSinkInfo(_) => CommandTag::GetSinkInfo,
            Command::GetSinkInfoList => CommandTag::GetSinkInfoList,
            Command::GetSourceInfo(_) => CommandTag::GetSourceInfo,
            Command::GetSourceInfoList => CommandTag::GetSourceInfoList,
            Command::GetClientInfo(_) => CommandTag::GetClientInfo,
            Command::GetClientInfoList => CommandTag::GetClientInfoList,
            // Command::GetCardInfoList => CommandTag::GetCardInfoList,
            Command::GetModuleInfo(_) => CommandTag::GetModuleInfo,
            Command::GetModuleInfoList => CommandTag::GetModuleInfoList,
            Command::GetSinkInputInfo(_) => CommandTag::GetSinkInputInfo,
            Command::GetSinkInputInfoList => CommandTag::GetSinkInputInfoList,
            Command::GetSourceOutputInfo(_) => CommandTag::GetSourceOutputInfo,
            Command::GetSourceOutputInfoList => CommandTag::GetSourceOutputInfoList,
            Command::GetSampleInfo(_) => CommandTag::GetSampleInfo,
            Command::GetSampleInfoList => CommandTag::GetSampleInfoList,
            Command::Subscribe(_) => CommandTag::Subscribe,
            Command::SubscribeEvent(_) => CommandTag::SubscribeEvent,
            Command::LookupSink(_) => CommandTag::LookupSink,
            Command::LookupSource(_) => CommandTag::LookupSource,

            Command::Request(_) => CommandTag::Request,
            Command::Overflow(_) => CommandTag::Overflow,
            Command::Underflow(_) => CommandTag::Underflow,
            Command::PlaybackStreamKilled(_) => CommandTag::PlaybackStreamKilled,
            Command::RecordStreamKilled(_) => CommandTag::RecordStreamKilled,
            Command::Started(_) => CommandTag::Started,
            Command::PlaybackBufferAttrChanged(_) => CommandTag::PlaybackBufferAttrChanged,
        }
    }
}

impl TagStructWrite for Command {
    fn write(
        &self,
        w: &mut TagStructWriter<'_>,
        _protocol_version: u16,
    ) -> Result<(), ProtocolError> {
        match self {
            Command::Error(e) => w.write_u32(*e as u32),
            Command::Timeout => Ok(()),
            Command::Reply => Ok(()),
            Command::Exit => Ok(()),

            Command::Auth(ref p) => w.write(p),
            Command::SetClientName(ref p) => w.write(p),
            Command::CreatePlaybackStream(ref p) => w.write(p),
            Command::DeletePlaybackStream(chan) => w.write_u32(*chan),
            Command::CreateRecordStream(ref p) => w.write(p),
            Command::DeleteRecordStream(chan) => w.write_u32(*chan),
            Command::DrainPlaybackStream(chan) => w.write_u32(*chan),
            Command::GetPlaybackLatency(ref p) => w.write(p),
            Command::GetRecordLatency(ref p) => w.write(p),
            Command::CreateUploadStream(ref p) => w.write(p),
            Command::DeleteUploadStream(chan) => w.write_u32(*chan),
            Command::FinishUploadStream(chan) => w.write_u32(*chan),

            Command::Stat => Ok(()),
            Command::GetServerInfo => Ok(()),
            Command::GetSinkInfo(ref p) => w.write(p),
            Command::GetSinkInfoList => Ok(()),
            Command::GetSourceInfo(ref p) => w.write(p),
            Command::GetSourceInfoList => Ok(()),
            Command::GetModuleInfo(id) => w.write_u32(*id),
            Command::GetModuleInfoList => Ok(()),
            Command::GetClientInfo(id) => w.write_u32(*id),
            Command::GetClientInfoList => Ok(()),
            Command::GetSinkInputInfo(id) => w.write_u32(*id),
            Command::GetSinkInputInfoList => Ok(()),
            Command::GetSourceOutputInfo(id) => w.write_u32(*id),
            Command::GetSourceOutputInfoList => Ok(()),
            Command::GetSampleInfo(id) => w.write_u32(*id),
            Command::GetSampleInfoList => Ok(()),
            Command::Subscribe(mask) => w.write(mask),
            Command::SubscribeEvent(ref p) => w.write(p),
            Command::LookupSink(ref p) => w.write_string(Some(p)),
            Command::LookupSource(ref p) => w.write_string(Some(p)),

            Command::Request(ref p) => w.write(p),
            Command::Overflow(chan) => w.write_u32(*chan),
            Command::Underflow(ref p) => w.write(p),
            Command::PlaybackStreamKilled(chan) => w.write_u32(*chan),
            Command::RecordStreamKilled(chan) => w.write_u32(*chan),
            Command::Started(chan) => w.write_u32(*chan),
            Command::PlaybackBufferAttrChanged(ref p) => w.write(p),
        }
    }
}