Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

Connection.h

Go to the documentation of this file.
00001 //
00002 // Connection.h
00003 //
00004 // Copyright (c) Shareaza Development Team, 2002-2005.
00005 // This file is part of SHAREAZA (www.shareaza.com)
00006 //
00007 // Shareaza is free software; you can redistribute it
00008 // and/or modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2 of
00010 // the License, or (at your option) any later version.
00011 //
00012 // Shareaza is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with Shareaza; if not, write to the Free Software
00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 //
00021 
00022 // CConnection holds a socket used to communicate with a remote computer, and is the root of a big inheritance tree
00023 // http://wiki.shareaza.com/static/Developers.Code.CConnection
00024 
00025 // Make the compiler only include the lines here once, this is the same thing as pragma once
00026 #if !defined(AFX_CONNECTION_H__6312EF26_B2C8_431F_93EF_243EA5E1A3DF__INCLUDED_)
00027 #define AFX_CONNECTION_H__6312EF26_B2C8_431F_93EF_243EA5E1A3DF__INCLUDED_
00028 
00029 // Only include the lines beneath this one once
00030 #pragma once
00031 
00032 // Tell the compiler these classes exist, and it will find out more about them soon
00033 class CBuffer;
00034 class CConnection;
00035 
00036 // Keep track of how fast we are reading or writing bytes to a socket
00037 typedef struct
00038 {
00039         // Options to limit bandwidth
00040         DWORD* pLimit;    // Points to a DWORD that holds the limit for this bandwidth meter
00041         BOOL   bUnscaled; // Set to true to worry about bandwidth even if the program settings don't
00042 
00043         // Transfer statistics
00044         DWORD nTotal;   // The total number of bytes read or written
00045         DWORD tLast;    // The time the last read or write happened
00046         DWORD nMeasure; // The average speed in bytes per second over the last 2 seconds
00047 
00048         // The arrays of byte counts and times
00049         DWORD pHistory[64]; // 64 records of a number of bytes transferred
00050         DWORD pTimes[64];   // The times each of these transfers happened
00051         DWORD nPosition;    // The next spot in the array to use
00052         DWORD tLastAdd;     // When we last recorded a transfer of some bytesThe last time something was recorded into pHistory and pTimes
00053         DWORD tLastSlot;    // When we started using this time slot
00054 
00055 } TCPBandwidthMeter;
00056 
00057 // A socket connection to a remote compueter on the Internet running peer-to-peer software
00058 class CConnection
00059 {
00060 
00061 public:
00062 
00063         // Make a new CConnection object, and delete one
00064         CConnection();
00065         virtual ~CConnection();
00066 
00067 public:
00068 
00069         // The remote computer's IP address, who connected to the other, are we connected, and when it happened
00070         SOCKADDR_IN m_pHost;      // The remote computer's IP address in Windows Sockets format
00071         CString     m_sAddress;   // The same IP address in a string like "1.2.3.4"
00072         BOOL        m_bInitiated; // True if we initiated the connection, false if the remote computer connected to us
00073         BOOL        m_bConnected; // True when the socket is connected
00074         DWORD       m_tConnected; // The tick count when the connection was made
00075 
00076 public:
00077 
00078         // The actual socket, buffers for reading and writing bytes, and some headers from the other computer
00079         SOCKET   m_hSocket;     // The actual Windows socket for the Internet connection to the remote computer
00080         CBuffer* m_pInput;      // Data from the remote computer, will be compressed if the remote computer is sending compressed data
00081         CBuffer* m_pOutput;     // Data to send to the remote computer, will be compressed if we are sending the remote computer compressed data
00082         CString  m_sUserAgent;  // The name of the program the remote computer is running
00083         CString  m_sLastHeader; // The handshake header that ReadHeaders most recently read
00084 
00085 public:
00086 
00087         // Structures to control bandwidth in each direction
00088         TCPBandwidthMeter m_mInput;     // Input and output TCP bandwidth meters
00089         TCPBandwidthMeter m_mOutput;
00090         int               m_nQueuedRun; // The queued run state of 0, 1, or 2 (do)
00091 
00092 public:
00093 
00094         // Make a connection, accept a connection, copy a connection, and close a connection
00095         virtual BOOL ConnectTo(SOCKADDR_IN* pHost);                  // Connect to an IP address and port number
00096         virtual BOOL ConnectTo(IN_ADDR* pAddress, WORD nPort);
00097         virtual void AcceptFrom(SOCKET hSocket, SOCKADDR_IN* pHost); // Accept a connection from a remote computer
00098         virtual void AttachTo(CConnection* pConnection);             // Copy a connection (do)
00099         virtual void Close();                                        // Disconnect from the remote computer
00100 
00101 public:
00102 
00103         // Exchange data with the other computer, measure bandwidth, and work with headers
00104         BOOL DoRun();          // Communicate with the other computer, reading and writing everything we can right now
00105         void QueueRun();       // (do) may no longer be in use
00106         void Measure();        // Measure the bandwidth, setting nMeasure in the bandwidth meters for each direction
00107         BOOL ReadHeaders();    // Read text headers sitting in the input buffer
00108         BOOL SendMyAddress();  // If we are listening on a port, tell the other computer our IP address and port number
00109         BOOL IsAgentBlocked(); // Check the other computer's software title against our list of programs not to talk to
00110 
00111 protected:
00112 
00113         // Read and write data through the socket, and look at headers
00114         virtual BOOL OnRun();                // (do) just returns true
00115         virtual BOOL OnConnected();          // (do) just returns true
00116         virtual BOOL OnRead();               // Read data waiting in the socket into the input buffer
00117         virtual BOOL OnWrite();              // Move the contents of the output buffer into the socket
00118         virtual void OnDropped(BOOL bError); // (do) empty
00119         virtual BOOL OnHeaderLine(CString& strHeader, CString& strValue); // Processes a single line from the headers
00120         virtual BOOL OnHeadersComplete();    // (do) just returns true
00121 
00122 public:
00123 
00124         // Encode and decode URL text, and see if a string starts with a tag
00125         static CString URLEncode(LPCTSTR pszInput);                   // Encode "hello world" into "hello%20world"
00126         static CString URLDecode(LPCTSTR pszInput);                   // Decode "hello%20world" back to "hello world"
00127         static CString URLDecodeANSI(LPCTSTR pszInput);               // Decodes properly encoded URLs
00128         static CString URLDecodeUnicode(LPCTSTR pszInput);            // Decodes URLs with extended characters
00129         static BOOL    StartsWith(LPCTSTR pszInput, LPCTSTR pszText); // StartsWith("hello world", "hello") is true
00130 };
00131 
00132 // End the group of lines to only include once, pragma once doesn't require an endif at the bottom
00133 #endif // !defined(AFX_CONNECTION_H__6312EF26_B2C8_431F_93EF_243EA5E1A3DF__INCLUDED_)

Generated on Thu Dec 15 10:39:34 2005 for Shareaza 2.2.1.0 by  doxygen 1.4.2