Spinning Topp Logo BlackTopp Studios
inc
tcpsocket.cpp
1 // © Copyright 2010 - 2014 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 
41 #ifndef _networktcpsocket_cpp
42 #define _networktcpsocket_cpp
43 
44 #include "Network/platformsocket.h.cpp"
45 
46 #include "Network/tcpsocket.h"
47 #include "Network/tcpv4socket.h"
48 #include "Network/tcpv6socket.h"
49 
50 namespace Mezzanine
51 {
52  namespace Network
53  {
55  { }
56 
58  { }
59 
60  ///////////////////////////////////////////////////////////////////////////////
61  // Utility
62 
64  { return Network::TLP_TCP; }
65 
66  ///////////////////////////////////////////////////////////////////////////////
67  // Core Operations
68 
70  { return this->InternalSocket->Listen(Backlog); }
71 
73  {
74  PlatformSocket* SockPtr = this->InternalSocket->Accept(Address);
75  if( SockPtr != NULL ) {
77  return new TCPv4Socket( SockPtr );
78  }else if( this->GetNetworkLayerProtocol() == Network::NLP_IPv6 ) {
79  return new TCPv6Socket( SockPtr );
80  }
81  }
82  return NULL;
83  }
84 
85  ///////////////////////////////////////////////////////////////////////////////
86  // Configuration
87 
88  void TCPSocket::SetKeepAlive(const Boole Alive)
89  {
90  int Value = ( Alive ? 1 : 0 );
91  this->InternalSocket->SetSocketOption(Network::Sol_Socket,Network::SockOpts_KeepAlive,(char*)&Value,sizeof(Value));
92  }
93 
95  {
96  int Value = 0;
97  AddrLen Size;
98  this->InternalSocket->GetSocketOption(Network::Sol_Socket,Network::SockOpts_KeepAlive,(char*)&Value,&Size);
99  return ( Value != 0 );
100  }
101 
102  void TCPSocket::SetNoDelay(const Boole NoDelay)
103  {
104  int Value = ( NoDelay ? 1 : 0 );
105  this->InternalSocket->SetSocketOption(Network::Sol_TCP,Network::TCPOpts_NoDelay,(char*)&Value,sizeof(Value));
106  }
107 
109  {
110  int Value = 0;
111  AddrLen Size;
112  this->InternalSocket->GetSocketOption(Network::Sol_TCP,Network::TCPOpts_NoDelay,(char*)&Value,&Size);
113  return ( Value != 0 );
114  }
115  }//Network
116 }//Mezzanine
117 
118 #endif
TCPSocket()
Class constructor.
Definition: tcpsocket.cpp:54
PlatformSocket * Accept(SystemAddress &Address)
Accepts an incoming connection from a remote host.
PlatformSocket * InternalSocket
A pointer to the internal system socket.
Definition: socket.h:163
void SetNoDelay(const Boole NoDelay)
If enabled, the Nagle algorithm is disabled and TCP segments will be sent immediately.
Definition: tcpsocket.cpp:102
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
A TCP socket that uses IPv6 internet addresses for connections.
Definition: tcpv6socket.h:54
Boole GetSocketOption(const Integer Level, const Integer OptID, char *Value, AddrLen *ValueLen) const
Get a low level socket parameter.
Boole GetKeepAlive() const
Gets whether or not a connection on this socket should use periodic packets during inactivity to ensu...
Definition: tcpsocket.cpp:94
int Integer
A datatype used to represent any integer close to.
Definition: datatypes.h:154
Boole Listen(const int Backlog=5)
Prepares this socket for incoming connections.
Internet Protocol version 6.
TransportLayerProtocol
This is an enum listing for recognized protocols on Layer 4 of the OSI model.
This is helper class for managing platform specific data of socket implementations.
A TCP socket that uses IPv4 internet addresses for connections.
Definition: tcpv4socket.h:54
TCPSocket * Accept(SystemAddress &Address)
Accepts an incoming connection from a remote host.
Definition: tcpsocket.cpp:72
Boole Listen(const Integer Backlog=5)
Prepares this socket for incoming connections.
Definition: tcpsocket.cpp:69
Transmission Control Protocol.
virtual NetworkLayerProtocol GetNetworkLayerProtocol() const =0
Gets the layer 3 protocol this socket is operating on.
TransportLayerProtocol GetTransportLayerProtocol() const
Gets the layer 4 protocol this socket is operating on.
Definition: tcpsocket.cpp:63
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
virtual ~TCPSocket()
Class destructor.
Definition: tcpsocket.cpp:57
A socket class using the TCP stream protocol.
Definition: tcpsocket.h:53
void SetKeepAlive(const Boole Alive)
Sets whether or not a connection on this socket should use periodic packets during inactivity to ensu...
Definition: tcpsocket.cpp:88
Boole GetNoDelay() const
Gets whether or not the Nagle algorithm is disabled.
Definition: tcpsocket.cpp:108
A simple class that stores a complete set of information for establishing connections.
Definition: systemaddress.h:53
Internet Protocol version 4.
Boole SetSocketOption(const Integer Level, const Integer OptID, const char *Value, const AddrLen ValueLen)
Set a low level socket parameter.