File: btstream.h

package info (click to toggle)
ctorrent 1.3.4.dnh3.3.2-5
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,240 kB
  • sloc: cpp: 13,161; sh: 675; ansic: 383; makefile: 6
file content (66 lines) | stat: -rw-r--r-- 1,501 bytes parent folder | download | duplicates (3)
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
#ifndef BTSTREAM_H
#define BTSTREAM_H

#include "./def.h"
#include "./bufio.h"

#ifdef WINDOWS
#include <Winsock2.h>
#else
#include <unistd.h>
#endif

#include "rate.h"

size_t get_nl(char *from);
void set_nl(char *to, size_t from);

class btStream
{
private:
  SOCKET sock, sock_was;
  size_t m_oldbytes;

public:
  BufIo in_buffer;
  BufIo out_buffer;

  btStream() { sock = sock_was = INVALID_SOCKET; m_oldbytes = 0; }
  ~btStream() { if( INVALID_SOCKET != sock) CLOSE_SOCKET(sock); }

  SOCKET GetSocket() { return (INVALID_SOCKET==sock) ? sock_was : sock; }
  void SetSocket(SOCKET sk){ sock = sk; }

  void Close(){
    if( INVALID_SOCKET != sock ){
      CLOSE_SOCKET(sock);
      sock_was = sock;
      sock = INVALID_SOCKET;
    }
    in_buffer.Close();
    out_buffer.Close();
  }

  ssize_t PickMessage(); //�Ƴ����ջ����е�һ����Ϣ
  ssize_t Feed();
  ssize_t Feed(Rate *rate);
  ssize_t Feed(size_t limit, Rate *rate);

  int HaveMessage();  // ����ֵ 1: ����������Ϣ 0: ������Ϣ -1: ʧ��
  char PeekMessage();
  int PeekMessage(char m);
  int PeekNextMessage(char m);

  ssize_t Send_Keepalive();
  ssize_t Send_State(unsigned char state);
  ssize_t Send_Have(size_t idx);
  ssize_t Send_Piece(size_t idx,size_t off,char *piece_buf,size_t len);
  ssize_t Send_Bitfield(char *bit_buf,size_t len);
  ssize_t Send_Request(size_t idx,size_t off,size_t len);
  ssize_t Send_Cancel(size_t idx,size_t off,size_t len);
  ssize_t Send_Buffer(char *buf,size_t len);

  ssize_t Flush();
};

#endif