00001 // 00002 // Handshakes.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 // CHandshakes listens for remote computers that want to connect to us 00023 // http://wiki.shareaza.com/static/Developers.Code.CHandshakes 00024 00025 // Make the compiler only include the lines here once, this is the same thing as pragma once 00026 #if !defined(AFX_HANDSHAKES_H__2314A7BE_5C51_4F8E_A4C6_6059A7621AE0__INCLUDED_) 00027 #define AFX_HANDSHAKES_H__2314A7BE_5C51_4F8E_A4C6_6059A7621AE0__INCLUDED_ 00028 00029 // Only include the lines beneath this one once 00030 #pragma once 00031 00032 // Tell the compiler that a class named CHandshake exists, and is defined in detail elsewhere 00033 class CHandshake; // Lets methods defined here take and return pointers to CHandshake objects 00034 00035 class CHandshakes 00036 { 00037 00038 public: 00039 00040 // Make the CHandshakes object, and later, delete it 00041 CHandshakes(); 00042 virtual ~CHandshakes(); 00043 00044 public: 00045 00046 // How many connections our listening socket has received, and how long they've been connected 00047 DWORD m_nStableCount; // The number of connections our listening socket has received 00048 DWORD m_tStableTime; // The time at least one has been connected (do) 00049 00050 protected: 00051 00052 // The listening socket 00053 SOCKET m_hSocket; // Our one listening socket 00054 HANDLE m_hThread; // This thread waits for remote computers to call the listening socket 00055 CEvent m_pWakeup; // Fire this event when a remote computer calls our listening socket 00056 00057 // The list of CHandshake objects 00058 CPtrList m_pList; // The list of pointers to CHandshake objects 00059 CCriticalSection m_pSection; // Use to make sure only one thread accesses the list at a time 00060 00061 public: 00062 00063 // Start and stop listening on the socket 00064 BOOL Listen(); // Listen on the socket 00065 void Disconnect(); // Stop listening 00066 00067 // Connect to an IP, and determine if we are connected to one 00068 BOOL PushTo(IN_ADDR* pAddress, WORD nPort, DWORD nIndex = 0); // Connect to the given IP 00069 BOOL IsConnectedTo(IN_ADDR* pAddress); // Looks for the IP in the handshake objects list 00070 00071 protected: 00072 00073 // Replace and remove CHandshake objects in the m_pList of them 00074 void Substitute(CHandshake* pOld, CHandshake* pNew); // Replace an old CHandshake object in the list with a new one 00075 void Remove(CHandshake* pHandshake); // Remove a CHandshake object from the list 00076 00077 protected: 00078 00079 // Loop to listen for connections and accept them 00080 static UINT ThreadStart(LPVOID pParam); // The thread that waits while the socket listens starts here 00081 void OnRun(); // Accept incoming connections from remote computers 00082 void RunHandshakes(); // Send and receive data with each remote computer in the list 00083 BOOL AcceptConnection(); // Accept a connection, making a new CHandshake object in the list for it 00084 void CreateHandshake(SOCKET hSocket, SOCKADDR_IN* pHost); // Make the new CHandshake object for the new connection 00085 void RunStableUpdate(); // Update the discovery services (do) 00086 00087 // Tell WSAAccept if we want to accept a connection from a computer that just called us 00088 static int CALLBACK AcceptCheck(IN LPWSABUF lpCallerId, IN LPWSABUF lpCallerData, IN OUT LPQOS lpSQOS, IN OUT LPQOS lpGQOS, IN LPWSABUF lpCalleeId, OUT LPWSABUF lpCalleeData, OUT GROUP FAR * g, IN DWORD dwCallbackData); 00089 00090 public: 00091 00092 // Returns an iterator at the start of the list of handshake objects 00093 inline POSITION GetIterator() const 00094 { 00095 // Returns a MFC POSITION iterator, or null if the list is empty 00096 return m_pList.GetHeadPosition(); 00097 } 00098 00099 // Given a position in the handshake list, returns the next handshake object 00100 inline CHandshake* GetNext(POSITION& pos) const 00101 { 00102 // Returns the CHandshake object at the current position, and then moves the position iterator to the next one 00103 return (CHandshake*)m_pList.GetNext( pos ); // Does two things 00104 } 00105 00106 // True if the socket is valid, false if its closed 00107 inline BOOL IsListening() const 00108 { 00109 // If the socket is not invalid, it is connected to the remote computer 00110 return m_hSocket != INVALID_SOCKET; 00111 } 00112 00113 // Get complete access to CHandhsake member variables and methods (do) 00114 friend class CHandshake; 00115 }; 00116 00117 // When the program runs, it makes a single global CHandshakes object 00118 extern CHandshakes Handshakes; // Access Handshakes externally here in Handshakes.h, even though it is defined in Handshakes.cpp 00119 00120 // End the group of lines to only include once, pragma once doesn't require an endif at the bottom 00121 #endif // !defined(AFX_HANDSHAKES_H__2314A7BE_5C51_4F8E_A4C6_6059A7621AE0__INCLUDED_)