Logo
~Apps~
~Projects~
~Contact~


pSocket.h

Go to the documentation of this file.
00001 
00006 /*
00007 Copyright (C) 2005  Anders Hedstrom
00008 
00009 This program is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU General Public License
00011 as published by the Free Software Foundation; either version 2
00012 of the License, or (at your option) any later version.
00013 
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with this program; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 */
00023 #ifndef _PSOCKET_H
00024 #define _PSOCKET_H
00025 
00026 #include <TcpSocket.h>
00027 #include <ISocketHandler.h>
00028 
00029 #define DLCHECKSIZE 30 // seconds
00030 
00031 
00033 
00034 class pSocket : public TcpSocket
00035 {
00036         enum state_t {
00037                 ACCEPT_LENGTH = 0,
00038                 ACCEPT_PROTOCOL,
00039                 ACCEPT_NULLBYTES,
00040                 ACCEPT_HASH,
00041                 ACCEPT_PEER_ID,
00042                 STATE_COMMAND,
00043                 STATE_COMMAND2,
00044                 STATE_GET_INTEGERS,
00045                 STATE_GET_BITMAP,
00046                 STATE_GET_PIECE,
00047                 STATE_GET_COMMAND,
00048         };
00049 public:
00050         pSocket(ISocketHandler&);
00051         pSocket(ISocketHandler&,const std::string& hash,unsigned char *remote_peer_id);
00052         ~pSocket();
00053 
00054         void OnAccept(); // hash still unknown
00055         void OnConnect();
00056         void OnRead();
00057         void OnConnectFailed();
00058         void OnDelete();
00059 
00060         void SendHello();
00061         void SendBitmap();
00062         void SendInterest(bool);
00063         void SendRequest(size_t piece,size_t offset,size_t length);
00064         void SendCancel(size_t piece,size_t offset,size_t length);
00065         void SendChoke(bool);
00066         void SendHave(size_t piece);
00067         void SendPiece(size_t piece,size_t offset,size_t length);
00068 
00069         const std::string& GetHash() { return m_hash; }
00070         Peer *GetPeer();
00071 
00072         bool Interested() { return m_interest; }
00073         bool Choked() { return m_choke; }
00074         time_t ChokeTime() { return time(NULL) - m_t_choke; }
00075 
00076         bool CTS() { return m_cts; }
00077         int GetSockId() { return m_id; }
00078 
00079         size_t GetBytesR(); // GetBytesReceived
00080         size_t GetBytesW(); // GetBytesSent
00081         bool GetDownloadRate(size_t&);
00082 
00083         void ShowStatus(size_t);
00084 
00085 private:
00086         pSocket(const pSocket& s) : TcpSocket(s) {} // copy constructor
00087         pSocket& operator=(const pSocket& ) { return *this; } // assignment operator
00088 
00089         void cmdChoke();
00090         void cmdUnchoke();
00091         void cmdInterested();
00092         void cmdNotinterested();
00093         void cmdHave(size_t);
00094         void cmdBitfield();
00095         void cmdRequest(size_t, size_t, size_t);
00096         void cmdCancel(size_t, size_t, size_t);
00097         void cmdPiece(size_t, size_t, unsigned char *piece);
00098 
00099         std::string m_hash;
00100         state_t m_state;
00101         bool m_server;
00102         char m_cmd;
00103         unsigned char m_remote_peer_id[20];
00104         unsigned long m_int[3];
00105         size_t m_integers;
00106         size_t m_ptr;
00107         unsigned char *m_slice;
00108 static  int m_next_id;
00109         int m_id;
00110         bool m_interest;
00111         bool m_choke;
00112         time_t m_t_choke;
00113         bool m_cts;
00114         Session *m_sess;
00115         Peer *m_peer;
00116         size_t m_last_r;
00117         size_t m_last_w;
00118         size_t m_last_r_buf[DLCHECKSIZE];
00119         int m_last_r_ptr;
00120         size_t m_length_cmd;
00121 };
00122 
00123 
00124 
00125 
00126 #endif // _PSOCKET_H
Page, code, and content Copyright (C) 2006 by Anders Hedström
Generated on Mon Aug 29 20:21:47 2005 for C++ Sockets by  doxygen 1.4.4