TCPSocket.hGo to the documentation of this file.00001 /* 00002 * Copyright (c) 2000-2003 Barak Weichselbaum <barak@komodia.com> 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 00014 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 00015 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00016 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00017 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 00018 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00019 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00020 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00021 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00022 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00023 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00024 * SUCH DAMAGE. 00025 * 00026 * Contact info: 00027 * ------------- 00028 * 00029 * Site: http://www.komodia.com 00030 * Main contact: barak@komodia.com 00031 * For custom projects, 00032 * consulting, or other 00033 * paid services: sales@komodia.com 00034 */ 00035 00036 #if !defined(AFX_TCPSOCKET_H__77DA7F21_291E_4C2A_B12B_535ABA1E829C__INCLUDED_) 00037 #define AFX_TCPSOCKET_H__77DA7F21_291E_4C2A_B12B_535ABA1E829C__INCLUDED_ 00038 00039 #if _MSC_VER > 1000 00040 #pragma once 00041 #endif // _MSC_VER > 1000 00042 00043 #include "Socket.h" 00044 00045 KOMODIA_NAMESPACE_START 00046 00047 class CTCPSocket : public CSocket 00048 { 00049 public: 00050 //Set nagle status (default is on) 00051 BOOL SetNagle(BOOL bNagle); 00052 00053 //Close the socket 00054 virtual BOOL Close(); 00055 00056 //Get our connection status 00057 BOOL IsConnected()const; 00058 00059 //Send data over the sockets 00060 //Return value: 00061 //Positive - The number of bytes received. 00062 //Negative - Error 00063 virtual int Send(const char* pBuffer, 00064 unsigned long ulBufferLength); 00065 00066 //Accept a connection, supply an already made socket 00067 virtual BOOL Accept(CTCPSocket* pNewSocket); 00068 00069 //Accept a connection, create the socket class 00070 CTCPSocket* Accept(); 00071 00072 //Listen to incoming connections 00073 virtual BOOL Listen(unsigned long ulBackLog=5); 00074 00075 //Connect to a remote system 00076 //Choose the source address explicitly 00077 //usSourcePort - OS will allocate source port 00078 virtual BOOL Connect(unsigned short usSourcePort, 00079 IP aDestinationAddress, 00080 unsigned short usDestinationPort); 00081 virtual BOOL Connect(unsigned short usSourcePort, 00082 const std::string& rDestinationAddress, 00083 unsigned short usDestinationPort); 00084 00085 //Connect to a remote system 00086 //Using OS allocated source port 00087 virtual BOOL Connect(IP aDestinationAddress, 00088 unsigned short usDestinationPort); 00089 virtual BOOL Connect(const std::string& rDestinationAddress, 00090 unsigned short usDestinationPort); 00091 00092 //Create the socket 00093 virtual BOOL Create(); 00094 00095 //Ctor and Dtor 00096 CTCPSocket(BOOL bRawSocket=false); 00097 virtual ~CTCPSocket(); 00098 protected: 00099 //Called before we accept the socket (user overide) 00100 virtual void BeforeAccept(); 00101 00102 //Indicate if we are a blocked socket or an async one 00103 virtual BOOL IsAsyncClass()const; 00104 00105 //Set our connection status 00106 void SetConnectionStatus(BOOL bConnected); 00107 00108 //When the socket is accepted, what to do (user overide) 00109 virtual void Accepted(); 00110 00111 //Set acceptance of a socket 00112 void SetAcceptance(CTCPSocket* pNewSocket, 00113 SOCKET aSocket, 00114 sockaddr_in aAddress)const; 00115 private: 00116 //Attach to a socket 00117 CTCPSocket(SOCKET aSocket); 00118 00119 //No copy Ctor 00120 CTCPSocket(const CTCPSocket& rSocket); 00121 00122 //No assignment operator 00123 CTCPSocket& operator=(const CTCPSocket& rSocket); 00124 00125 //Initialize the class 00126 void InitializeTCP(); 00127 00128 //Are we connected 00129 BOOL m_bConnected; 00130 }; 00131 00132 KOMODIA_NAMESPACE_END 00133 00134 #endif // !defined(AFX_TCPSOCKET_H__77DA7F21_291E_4C2A_B12B_535ABA1E829C__INCLUDED_) |