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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
use std::{
    convert::TryFrom, 
    ops::Range,
};
use serde_json::{Map, Value};
use cyfs_base::*;
use crate::{
    types::*, 
    interface::udp::MTU, 
    protocol::{PackageCmdCode, Package},
    tunnel::{udp::Tunnel as UdpTunnel, DynamicTunnel}, 
    datagram::DatagramOptions
};
use super::super::super::{
    types::*
};

#[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd)]
pub enum CommandCode {
    Interest = 0,
    RespInterest = 1,
}


impl TryFrom<u8> for CommandCode {
    type Error = BuckyError;
    fn try_from(v: u8) -> std::result::Result<Self, Self::Error> {
        match v {
            0u8 => Ok(Self::Interest),
            1u8 => Ok(Self::RespInterest),
            _ => Err(BuckyError::new(
                BuckyErrorCode::InvalidParam,
                "invalid channel command value",
            )),
        }
    }
}

pub trait CommandPackage {
    fn command_code() -> CommandCode;
}

struct FlagsEncodeContext {
    flags: u16,
    length: usize,
}

impl FlagsEncodeContext {
    pub fn new<'a>(
        command_code: u8, 
        buf: &'a mut [u8]
    ) -> Result<(Self, &'a mut [u8]), BuckyError> {
        let buf = command_code.raw_encode(buf, &None)?;
        let buf = u16::from(0 as u16).raw_encode(buf, &None)?;
        Ok((
            Self {
                flags: 0,
                length: 3,
            },
            buf,
        ))
    }

    // 不检查是否merge
    pub fn encode<'a, T: RawEncode>(
        &mut self,
        buf: &'a mut [u8],
        value: &T
    ) -> Result<&'a mut [u8], BuckyError> {
        let pre_len = buf.len();
        let next_buf = value.raw_encode(buf, &None)?;
        self.length += pre_len - next_buf.len();
        Ok(next_buf)
    }

    pub fn option_encode<'a, T: RawEncode>(
        &mut self,
        buf: &'a mut [u8],
        value: &Option<T>,
        inc_flags: u16,
    ) -> Result<&'a mut [u8], BuckyError> {
        if let Some(v) = value {
            let pre_len = buf.len();
            self.flags |= inc_flags;
            let next_buf = v.raw_encode(buf, &None)?;
            self.length += pre_len - next_buf.len();
            Ok(next_buf)
        } else {
            Ok(buf)
        }
    }

    pub fn set_flags(&mut self, inc_flags: u16) {
        self.flags |= inc_flags;
    }

    pub fn get_flags(&self) -> u16 {
        self.flags
    }

    pub fn finish<'a>(&self, buf: &'a mut [u8]) -> Result<&'a mut [u8], BuckyError> {
        let begin_buf = buf;
        let buf = &mut begin_buf[u8::raw_bytes().unwrap()..];
        u16::from(self.flags).raw_encode(buf, &None).map(|_| ())?;
        Ok(&mut begin_buf[self.length..])
    }
}


struct FlagsDecodeContext {
    flags: u16, 
}

impl FlagsDecodeContext {
    pub fn new<'a>(
        buf: &'a [u8]
    ) -> Result<(Self, &'a [u8]), BuckyError> {
        let (flags, buf) = u16::raw_decode(buf)?;
        Ok((
            Self {
                flags, 
            },
            buf,
        ))
    }


    // 如果flags 的对应bit是0,会出错
    // TODO: 支持返回Option None
    pub fn decode<'a, T: RawDecode<'a>>(
        &mut self,
        buf: &'a [u8]
    ) -> Result<(T, &'a [u8]), BuckyError> {
        T::raw_decode(buf)
    }

    pub fn option_decode<'a, T: RawDecode<'a>>(
        &mut self,
        buf: &'a [u8],
        check_flags: u16,
    ) -> Result<(Option<T>, &'a [u8]), BuckyError> {
        if self.flags & check_flags == 0 {
            Ok((None, buf))
        } else {
            T::raw_decode(buf).map(|(v, buf)| (Some(v), buf))
        }
    }

    pub fn check_flags(&self, bits: u16) -> bool {
        self.flags & bits != 0
    }

    pub fn flags(&self) -> u16 {
        self.flags
    }
}


struct FlagsCounter {
    counter: u8,
}

impl FlagsCounter {
    pub fn new() -> Self {
        Self { counter: 0 }
    }

    pub fn next(&mut self) -> u16 {
        let inc = self.counter;
        self.counter += 1;
        1 << inc
    }
}


#[derive(Debug, Clone)]
pub struct Interest {
    pub session_id: TempSeq, 
    pub chunk: ChunkId,
    pub prefer_type: ChunkEncodeDesc, 
    pub referer: Option<String>,
    pub from: Option<DeviceId>, 
    // pub link_url: Option<String>,
    // flow_id:Option<u32>,
    // priority: Option<u8>,
    // from_device_obj : Option<DeviceObject>,
    // token : Option<String>,//必要的验证token
    // sign : Option<Vec<u8>>,
}


impl RawEncodeWithContext<DatagramOptions> for Interest {
    fn raw_measure_with_context(
        &self, 
        _options: &mut DatagramOptions, 
        _purpose: &Option<RawEncodePurpose>
    ) -> Result<usize, BuckyError> {
        unimplemented!()
    }
    fn raw_encode_with_context<'a>(
        &self,
        enc_buf: &'a mut [u8],
        options: &mut DatagramOptions,
        _purpose: &Option<RawEncodePurpose>
    ) -> Result<&'a mut [u8], BuckyError> {
        options.sequence = Some(self.session_id);
        let mut flags = FlagsCounter::new();
        let (mut context, buf) = FlagsEncodeContext::new(CommandCode::Interest as u8, enc_buf)?;
        let buf = context.encode(buf, &self.chunk)?;
        let buf = context.encode(buf, &self.prefer_type)?;
        let buf = context.option_encode(buf, &self.referer, flags.next())?;
        let _ = context.option_encode(buf, &self.from, flags.next())?;
        context.finish(enc_buf)
    }
}

impl<'de> RawDecodeWithContext<'de, &DatagramOptions> for Interest {
    fn raw_decode_with_context(
        buf: &'de [u8],
        options: &DatagramOptions,
    ) -> Result<(Self, &'de [u8]), BuckyError> {
        let session_id = options.sequence.ok_or_else(|| 
            BuckyError::new(BuckyErrorCode::InvalidData, "Interest package should has sequence"))?;
        let mut flags = FlagsCounter::new();
        let (mut context, buf) = FlagsDecodeContext::new(buf)?;
        let (chunk, buf) = context.decode(buf)?;
        let (prefer_type, buf) = context.decode(buf)?;
        let (referer, buf) = context.option_decode(buf, flags.next())?;
        let (from, buf) = context.option_decode(buf, flags.next())?;
        Ok((
            Self {
                session_id, 
                chunk, 
                prefer_type, 
                referer,
                from,
            },
            buf,
        ))
    }
}

impl JsonCodec<Interest> for Interest {
    fn encode_json(&self) -> Map<String, Value> {
        let mut obj = Map::new();
        JsonCodecHelper::encode_number_field(&mut obj, "session_id", self.session_id.value());
        JsonCodecHelper::encode_string_field(&mut obj, "chunk", &self.chunk);
        JsonCodecHelper::encode_field(&mut obj, "prefer_type", &self.prefer_type);
        JsonCodecHelper::encode_option_string_field(&mut obj, "referer", self.referer.as_ref());
        JsonCodecHelper::encode_option_string_field(&mut obj, "from", self.from.as_ref());
        obj
    }

    fn decode_json(obj: &Map<String, Value>) -> BuckyResult<Self> {
        let session_id: u32 = JsonCodecHelper::decode_int_field(obj, "session_id")?;
        Ok(Self {
            session_id: TempSeq::from(session_id), 
            chunk: JsonCodecHelper::decode_string_field(obj, "chunk")?, 
            prefer_type: JsonCodecHelper::decode_field(obj, "prefer_type")?, 
            referer: JsonCodecHelper::decode_option_string_field(obj, "referer")?, 
            from: JsonCodecHelper::decode_option_string_field(obj, "from")?, 
        })
    }
}


#[test]
fn encode_protocol_ineterest() {
    let src = Interest {
        session_id: TempSeq::from(123), 
        chunk: ChunkId::default(),
        prefer_type: ChunkEncodeDesc::Stream(None, None, None), 
        from: None,
        referer: Some("referer".to_owned()),
    };

    let mut buf = [0u8; 1500]; 
    let mut options = DatagramOptions::default();
    let _ = src.raw_encode_with_context(&mut buf, &mut options, &None).unwrap();

    let (cmd, dec) = u8::raw_decode(&buf).map(|(code, dec)| (CommandCode::try_from(code).unwrap(), dec)).unwrap();
    assert_eq!(cmd, CommandCode::Interest);
    let (dst, _) = Interest::raw_decode_with_context(dec, &mut options).unwrap();
    assert_eq!(src.chunk, dst.chunk);
    assert_eq!(src.referer, dst.referer);
}



#[derive(Clone, Debug)]
pub struct RespInterest {
    pub session_id: TempSeq, 
    pub chunk: ChunkId,  
    pub err: BuckyErrorCode,
    pub redirect: Option<DeviceId>,
    pub redirect_referer: Option<String>,
    pub to: Option<DeviceId>,
}


impl RawEncodeWithContext<DatagramOptions> for RespInterest {
    fn raw_measure_with_context(
        &self, 
        _options: &mut DatagramOptions, 
        _purpose: &Option<RawEncodePurpose>
    ) -> Result<usize, BuckyError> {
        unimplemented!()
    }
    fn raw_encode_with_context<'a>(
        &self,
        enc_buf: &'a mut [u8],
        options: &mut DatagramOptions,
        _purpose: &Option<RawEncodePurpose>
    ) -> Result<&'a mut [u8], BuckyError> {
        let mut flags = FlagsCounter::new();

        options.sequence = Some(self.session_id);
        // let mut flags = FlagsCounter::new();
        let (mut context, buf) = FlagsEncodeContext::new(CommandCode::RespInterest as u8, enc_buf)?;
        let buf = context.encode(buf, &self.chunk)?;
        let buf = context.encode(buf, &(self.err.into_u16()))?;
        let buf = context.option_encode(buf, &(self.redirect), flags.next())?;
        let buf = context.option_encode(buf, &(self.redirect_referer), flags.next())?;
        let _ = context.option_encode(buf, &(self.to), flags.next())?;
        context.finish(enc_buf)
    }
}

impl<'de> RawDecodeWithContext<'de, &DatagramOptions> for RespInterest {
    fn raw_decode_with_context(
        buf: &'de [u8],
        options: &DatagramOptions,
    ) -> Result<(Self, &'de [u8]), BuckyError> {
        let session_id = options.sequence.ok_or_else(|| 
            BuckyError::new(BuckyErrorCode::InvalidData, "RespInterest package should has sequence"))?;
        let mut flags = FlagsCounter::new();

        let (mut context, buf) = FlagsDecodeContext::new(buf)?;
        let (chunk, buf) = context.decode(buf)?;
        let (err, buf) = context.decode::<u16>(buf)?;
        let err = BuckyErrorCode::from(err);
        let (id, buf) = context.option_decode(buf, flags.next())?;
        let (referer, buf) = context.option_decode(buf, flags.next())?;
        let (to, buf) = context.option_decode(buf, flags.next())?;

        Ok((
            Self {
                session_id, 
                chunk, 
                err,
                redirect: id,
                redirect_referer: referer,
                to
            },
            buf,
        ))
    }
}

impl JsonCodec<RespInterest> for RespInterest {
    fn encode_json(&self) -> Map<String, Value> {
        let mut obj = Map::new();
        JsonCodecHelper::encode_number_field(&mut obj, "session_id", self.session_id.value());
        JsonCodecHelper::encode_string_field(&mut obj, "chunk", &self.chunk);
        let err: u32 = self.err.into();
        JsonCodecHelper::encode_number_field(&mut obj, "err", err);
        JsonCodecHelper::encode_option_string_field(&mut obj, "redirect", self.redirect.as_ref());
        JsonCodecHelper::encode_option_string_field(&mut obj, "redirect_referer", self.redirect_referer.as_ref());
        JsonCodecHelper::encode_option_string_field(&mut obj, "to", self.to.as_ref());
        obj
    }

    fn decode_json(obj: &Map<String, Value>) -> BuckyResult<Self> {
        let session_id: u32 = JsonCodecHelper::decode_int_field(obj, "session_id")?;
        let err: u32 = JsonCodecHelper::decode_int_field(obj, "err")?;
        Ok(Self {
            session_id: TempSeq::from(session_id), 
            chunk: JsonCodecHelper::decode_string_field(obj, "chunk")?, 
            err: BuckyErrorCode::from(err), 
            redirect: JsonCodecHelper::decode_option_string_field(obj, "redirect")?, 
            redirect_referer: JsonCodecHelper::decode_option_string_field(obj, "redirect_referer")?, 
            to: JsonCodecHelper::decode_option_string_field(obj, "to")?, 
        })
    }
}


#[derive(Clone, Debug)]
pub enum PieceDesc {
    Raptor(u32 /*raptor seq*/, u16 /*raptor k*/),
    Range(u32 /*range index*/, u16 /*range size*/),
}

impl PieceDesc {
    pub fn raw_raptor_bytes() -> usize {
        u8::raw_bytes().unwrap() + u32::raw_bytes().unwrap() + u16::raw_bytes().unwrap()
    }

    pub fn raw_stream_bytes() -> usize {
        u8::raw_bytes().unwrap() + u32::raw_bytes().unwrap() + u16::raw_bytes().unwrap()
    }

    pub fn raptor_index(&self, require_k: u16) -> Option<u32> {
        match self {
            Self::Raptor(index, k) => if *k == require_k {
                Some(*index)
            } else {
                None
            },
            Self::Range(_, _) => None 
        }
    }

    pub fn range_index(&self, require_range_size: u16) -> Option<u32> {
        match self {
            Self::Range(index, range_size) => if *range_size == require_range_size {
                Some(*index)
            } else {
                None
            },
            Self::Raptor(_, _) => None 
        }
    }
}

impl RawFixedBytes for PieceDesc {
    fn raw_bytes() -> Option<usize> {
        Some(Self::raw_raptor_bytes())
    }
}

impl RawEncode for PieceDesc {
    fn raw_measure(&self, _purpose: &Option<RawEncodePurpose>) -> BuckyResult<usize> {
        Ok(Self::raw_bytes().unwrap())
    }

    fn raw_encode<'a>(
        &self,
        buf: &'a mut [u8],
        purpose: &Option<RawEncodePurpose>,
    ) -> BuckyResult<&'a mut [u8]> {
        match self {
            Self::Raptor(index, k) => {
                let buf = 0u8.raw_encode(buf, purpose)?;
                let buf = index.raw_encode(buf, purpose)?;
                k.raw_encode(buf, purpose)
            }, 
            Self::Range(index, len) => {
                let buf = 1u8.raw_encode(buf, purpose)?;
                let buf = index.raw_encode(buf, purpose)?;
                len.raw_encode(buf, purpose)
            }
        }
    }
}

impl<'de> RawDecode<'de> for PieceDesc {
    fn raw_decode(buf: &'de [u8]) -> BuckyResult<(Self, &'de [u8])> {
        let (code, buf) = u8::raw_decode(buf)?;
        match code {
            0u8 => {
                let (index, buf) = u32::raw_decode(buf)?;
                let (k, buf) = u16::raw_decode(buf)?;
                Ok((Self::Raptor(index, k), buf))
            }, 
            1u8 => {
                let (index, buf) = u32::raw_decode(buf)?;
                let (len, buf) = u16::raw_decode(buf)?;
                Ok((Self::Range(index, len), buf))
            }, 
            _ => Err(BuckyError::new(BuckyErrorCode::InvalidData, "invalid piece desc type code"))
        }
    }
}

pub struct PieceData {
    pub est_seq: Option<TempSeq>,
    pub session_id: TempSeq, 
    pub chunk: ChunkId, 
    pub desc: PieceDesc, 
    pub data: Vec<u8>,
}

impl Package for PieceData {
    fn version(&self) -> u8 {
        0
    }
    
    fn cmd_code() -> PackageCmdCode {
        PackageCmdCode::PieceData
    }
}

impl PieceData {
    pub fn max_header_len() -> usize {
        TempSeq::raw_bytes().unwrap()
            + TempSeq::raw_bytes().unwrap()
            + ChunkId::raw_bytes().unwrap()
            + PieceDesc::raw_bytes().unwrap()
    }

    pub fn max_payload() -> usize {
        UdpTunnel::raw_data_max_payload_len() - u8::raw_bytes().unwrap() - Self::max_header_len()
    }

    pub fn encode_header<'a>(
        buf: &'a mut [u8],
        session_id: &TempSeq,  
        chunk: &ChunkId, 
        desc: &PieceDesc) -> BuckyResult<&'a mut [u8]> { 
        let buf = (PieceData::cmd_code() as u8).raw_encode(buf, &None)?;
        let buf = TempSeq::default().raw_encode(buf, &None)?;
        let buf = session_id.raw_encode(buf, &None)?;
        let buf = chunk.raw_encode(buf, &None)?;
        desc.raw_encode(buf, &None)
    }

    pub fn reset_estimate(buf: &mut [u8], est_seq: TempSeq) {
        let _ = est_seq.raw_encode(&mut buf[u8::raw_bytes().unwrap()..], &None).unwrap();
    }

    pub fn decode_from_raw_data(buf: &[u8]) -> BuckyResult<Self> {
        let (est_seq, buf) = TempSeq::raw_decode(buf).map(|(s, buf)| {
            (
                if s == TempSeq::default() {
                    None
                } else {
                    Some(s)
                },
                buf,
            )
        })?;
        let (session_id, buf) = TempSeq::raw_decode(buf)?;
        let (chunk, buf) = ChunkId::raw_decode(buf)?;
        let (desc, data) = PieceDesc::raw_decode(buf)?;
        Ok(Self {
            est_seq,
            session_id, 
            chunk,
            desc,  
            //FIXME: 这里有机会减少一次拷贝
            data: Vec::from(data),
        })
    }
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum PieceControlCommand {
    Continue,
    Finish, 
    Pause, 
    Cancel,
}


impl RawEncode for PieceControlCommand {
    fn raw_measure(&self, _purpose: &Option<RawEncodePurpose>) -> BuckyResult<usize> {
        Ok(u8::raw_bytes().unwrap())
    }

    fn raw_encode<'a>(
        &self,
        buf: &'a mut [u8],
        purpose: &Option<RawEncodePurpose>,
    ) -> BuckyResult<&'a mut [u8]> {
        match self {
            Self::Continue => 0u8.raw_encode(buf, purpose), 
            Self::Finish => 1u8.raw_encode(buf, purpose), 
            Self::Pause => 2u8.raw_encode(buf, purpose), 
            Self::Cancel => 3u8.raw_encode(buf, purpose), 
        }
    }
}


impl<'de> RawDecode<'de> for PieceControlCommand {
    fn raw_decode(buf: &'de [u8]) -> BuckyResult<(Self, &'de [u8])> {
        let (code, buf) = u8::raw_decode(buf)?;
        let command = match code {
            0u8 => Ok(Self::Continue), 
            1u8 => Ok(Self::Finish), 
            2u8 => Ok(Self::Pause),
            3u8 => Ok(Self::Cancel), 
            _ => Err(BuckyError::new(BuckyErrorCode::InvalidData, "invalid piece control command code"))
        }?;
        Ok((command, buf)) 
    }
}


#[derive(Debug)]
pub struct PieceControl {
    pub sequence: TempSeq, 
    pub session_id: TempSeq, 
    pub chunk: ChunkId, 
    pub command: PieceControlCommand, 
    pub max_index: Option<u32>, 
    pub lost_index: Option<Vec<Range<u32>>>
}

impl PieceControl {
    fn max_index_payload() -> usize {
        125
    }

    pub fn split_send(mut self, tunnel: &DynamicTunnel) -> BuckyResult<()> {
        let send_once = |ctrl: PieceControl| {
            let mut buffer = vec![0u8; MTU];
            let len = ctrl.raw_encode(&mut buffer[tunnel.as_ref().raw_data_header_len()..], &None)
                .map(|buf| MTU - buf.len())?;
            tunnel.as_ref().send_raw_data(&mut buffer[..len])?;
            Ok(())
        };

        match self.command {
            PieceControlCommand::Continue => {
                if self.lost_index.is_some() {
                    let lost_index = self.lost_index.as_mut().unwrap();
                    let mut buffer = vec![0u8; MTU];

                    let enc_from = tunnel.as_ref().raw_data_header_len();
                    
                    let mut flags = FlagsCounter::new();
                    let (mut context, buf_ptr) = FlagsEncodeContext::new(PackageCmdCode::PieceControl as u8, &mut buffer[enc_from..])?;
                    let buf_ptr = context.encode(buf_ptr, &self.sequence)?;
                    let buf_ptr = context.encode(buf_ptr, &self.session_id)?;
                    let buf_ptr = context.encode(buf_ptr, &self.chunk)?;
                    let buf_ptr = context.encode(buf_ptr, &self.command)?;
                    let index_from = MTU - buf_ptr.len(); 
                    let buf_ptr = context.option_encode(buf_ptr, &Some(0), flags.next())?;
                    let _ = context.option_encode(buf_ptr, &Some(vec![0u8; 0]), flags.next())?;
                    let _ = context.finish(&mut buffer[enc_from..])?;
                    
                    for indices in lost_index.chunks(Self::max_index_payload()) {
                        let buf_ptr = self.max_index.unwrap().raw_encode(&mut buffer[index_from..], &None)?;
                        let buf_ptr = indices.raw_encode(buf_ptr, &None)?;

                        let len = MTU - buf_ptr.len();
                        tunnel.as_ref().send_raw_data(&mut buffer[..len])?;
                    } 
                    Ok(())
                } else {
                    send_once(self)
                }
            }, 
            _ => {
                send_once(self)
            }
        }
    }
}

impl Package for PieceControl {
    fn version(&self) -> u8 {
        0
    }

    fn cmd_code() -> PackageCmdCode {
        PackageCmdCode::PieceControl
    }
}

impl RawEncode for PieceControl {
    fn raw_measure(&self, _purpose: &Option<RawEncodePurpose>) -> BuckyResult<usize> {
        unimplemented!()
    }

    fn raw_encode<'a>(
        &self,
        enc_buf: &'a mut [u8],
        _purpose: &Option<RawEncodePurpose>,
    ) -> BuckyResult<&'a mut [u8]> {
        let mut flags = FlagsCounter::new();
        let (mut context, buf) = FlagsEncodeContext::new(PackageCmdCode::PieceControl as u8, enc_buf)?;
        let buf = context.encode(buf, &self.sequence)?;
        let buf = context.encode(buf, &self.session_id)?;
        let buf = context.encode(buf, &self.chunk)?;
        let buf = context.encode(buf, &self.command)?;
        let buf = context.option_encode(buf, &self.max_index, flags.next())?;
        let _buf = context.option_encode(buf, &self.lost_index, flags.next())?;
        context.finish(enc_buf)
    }
}

impl<'de> RawDecode<'de> for PieceControl {
    fn raw_decode(buf: &'de [u8]) -> BuckyResult<(Self, &'de [u8])> {
        let mut flags = FlagsCounter::new();
        let (mut context, buf) = FlagsDecodeContext::new(buf)?;
        let (sequence, buf) = context.decode(buf)?;
        let (session_id, buf) = context.decode(buf)?;
        let (chunk, buf) = context.decode(buf)?;
        let (command, buf) = context.decode(buf)?;
        let (max_index, buf) = context.option_decode(buf, flags.next())?;
        let (lost_index, buf) = context.option_decode(buf, flags.next())?;
        Ok((Self {
            sequence, 
            session_id, 
            chunk, 
            command, 
            max_index, 
            lost_index
        }, buf))
    }
}


pub struct ChannelEstimate {
    pub sequence: TempSeq, 
    pub recved: u64,
}

impl Default for ChannelEstimate {
    fn default() -> Self {
        Self {
            sequence: TempSeq::default(), 
            recved: 0 
        }
    }
}

impl Package for ChannelEstimate {
    fn version(&self) -> u8 {
        0
    }
    
    fn cmd_code() -> PackageCmdCode {
        PackageCmdCode::ChannelEstimate
    }
}


impl RawFixedBytes for ChannelEstimate {
    fn raw_bytes() -> Option<usize> {
        Some(
            u8::raw_bytes().unwrap()
            + TempSeq::raw_bytes().unwrap()
            + u64::raw_bytes().unwrap())
    }
}

impl RawEncode for ChannelEstimate {
    fn raw_measure(&self, _purpose: &Option<RawEncodePurpose>) -> BuckyResult<usize> {
        Ok(Self::raw_bytes().unwrap())
    }

    fn raw_encode<'a>(
        &self,
        buf: &'a mut [u8],
        purpose: &Option<RawEncodePurpose>,
    ) -> BuckyResult<&'a mut [u8]> {
        let buf = (Self::cmd_code() as u8).raw_encode(buf, purpose)?;
        let buf = self.sequence.raw_encode(buf, purpose)?;
        self.recved.raw_encode(buf, purpose)
    }
}

impl<'de> RawDecode<'de> for ChannelEstimate {
    fn raw_decode(buf: &'de [u8]) -> BuckyResult<(Self, &'de [u8])> {
        let (sequence, buf) = TempSeq::raw_decode(buf)?;
        let (recved, buf) = u64::raw_decode(buf)?;
        Ok((Self {
            sequence, 
            recved 
        }, buf))
    }
}