41 #ifndef _networkplatformsocket_cpp
42 #define _networkplatformsocket_cpp
44 #include "Network/platformsocket.h.cpp"
46 #include "stringtool.h"
62 default:
return AF_UNSPEC;
102 default:
return SOCK_STREAM;
118 sockaddr_storage Ret;
121 sockaddr_in* Casted = (sockaddr_in*)&Ret;
122 Casted->sin_family = AF_INET;
123 Casted->sin_port = MezzAddr.
GetPort(
true);
126 sockaddr_in6* Casted = (sockaddr_in6*)&Ret;
127 Casted->sin6_family = AF_INET6;
128 Casted->sin6_port = MezzAddr.
GetPort(
true);
129 Casted->sin6_flowinfo = MezzAddr.
GetV6Flow();
130 Casted->sin6_scope_id = MezzAddr.
GetV6Scope();
140 sockaddr_storage Ret;
143 sockaddr_in* Casted = (sockaddr_in*)&Ret;
144 Casted->sin_family = AF_INET;
148 sockaddr_in6* Casted = (sockaddr_in6*)&Ret;
149 Casted->sin6_family = AF_INET6;
152 Casted->sin6_scope_id = MezzAddr.
V6ScopeID;
163 if( BerkAddr.ss_family == AF_INET ) {
164 const sockaddr_in& Casted =
reinterpret_cast<const sockaddr_in&
>(BerkAddr);
167 Ret.
SetPort(Casted.sin_port,
true);
168 }
else if( BerkAddr.ss_family == AF_INET6 ) {
169 const sockaddr_in6& Casted =
reinterpret_cast<const sockaddr_in6&
>(BerkAddr);
174 Ret.
SetPort(Casted.sin6_port,
true);
186 if( BerkAddr.ss_family == AF_INET ) {
187 const sockaddr_in& Casted =
reinterpret_cast<const sockaddr_in&
>(BerkAddr);
191 }
else if( BerkAddr.ss_family == AF_INET6 ) {
192 const sockaddr_in6& Casted =
reinterpret_cast<const sockaddr_in6&
>(BerkAddr);
227 InternalSocket(Handle),
235 #if defined(MEZZ_WINDOWS)
237 #elif defined(MEZZ_MACOSX) || defined(MEZZ_LINUX)
250 memset(&Hints,0,
sizeof(addrinfo));
253 Hints.ai_flags = AI_PASSIVE;
260 return ( ::bind(this->
InternalSocket,Results->ai_addr,Results->ai_addrlen) == 0 );
268 return this->
Bind( (sockaddr*)&AddressStorage,
sizeof(sockaddr_in) );
270 return this->
Bind( (sockaddr*)&AddressStorage,
sizeof(sockaddr_in6) );
277 #if defined(MEZZ_WINDOWS)
278 return ( ::bind(this->
InternalSocket,Address,Length) != SOCKET_ERROR );
279 #elif defined(MEZZ_MACOSX) || defined(MEZZ_LINUX)
286 #if defined(MEZZ_WINDOWS)
288 #elif defined(MEZZ_MACOSX) || defined(MEZZ_LINUX)
301 return this->
Connect( (sockaddr*)&AddressStorage,
sizeof(sockaddr_in) );
303 return this->
Connect( (sockaddr*)&AddressStorage,
sizeof(sockaddr_in6) );
310 #if defined(MEZZ_WINDOWS)
311 return ( ::connect(this->
InternalSocket,Address,Length) != SOCKET_ERROR );
312 #elif defined(MEZZ_MACOSX) || defined(MEZZ_LINUX)
319 #if defined(MEZZ_WINDOWS)
320 return ( ::listen(this->
InternalSocket,Backlog) != SOCKET_ERROR );
321 #elif defined(MEZZ_MACOSX) || defined(MEZZ_LINUX)
328 sockaddr_storage AddressStorage;
329 AddrLen AddressSize =
sizeof(sockaddr_storage);
331 if( NewSock != INVALID_SOCKET ) {
341 if( NewSock != INVALID_SOCKET ) {
349 #if defined(MEZZ_WINDOWS)
350 int Ret = ::send(this->
InternalSocket,static_cast<const char*>(Buffer),BufSize,Flags);
351 return ( Ret != SOCKET_ERROR ? Ret : -1 );
352 #elif defined(MEZZ_MACOSX) || defined(MEZZ_LINUX)
359 #if defined(MEZZ_WINDOWS)
360 int Ret = ::recv(this->
InternalSocket,static_cast<char*>(Buffer),BufSize,Flags);
361 return ( Ret != SOCKET_ERROR ? Ret : -1 );
362 #elif defined(MEZZ_MACOSX) || defined(MEZZ_LINUX)
375 return this->
SendTo( Buffer, BufSize, Flags, (sockaddr*)&AddressStorage,
sizeof(sockaddr_in) );
377 return this->
SendTo( Buffer, BufSize, Flags, (sockaddr*)&AddressStorage,
sizeof(sockaddr_in6) );
384 #if defined(MEZZ_WINDOWS)
385 int Ret = ::sendto(this->
InternalSocket,static_cast<const char*>(Buffer),BufSize,Flags,Address,Length);
386 return ( Ret != SOCKET_ERROR ? Ret : -1 );
387 #elif defined(MEZZ_MACOSX) || defined(MEZZ_LINUX)
388 return ::sendto(this->
InternalSocket,Buffer,BufSize,Flags,Address,Length);
394 sockaddr_storage AddressStorage;
395 AddrLen AddressSize =
sizeof(sockaddr_storage);
396 int Ret = this->
ReceiveFrom(Buffer,BufSize,Flags,(sockaddr*)&AddressStorage,&AddressSize);
403 #if defined(MEZZ_WINDOWS)
404 int Ret = ::recvfrom(this->
InternalSocket,static_cast<char*>(Buffer),BufSize,Flags,Address,Length);
405 return ( Ret != SOCKET_ERROR ? Ret : -1 );
406 #elif defined(MEZZ_MACOSX) || defined(MEZZ_LINUX)
407 return ::recvfrom(this->
InternalSocket,Buffer,BufSize,Flags,Address,Length);
416 #if defined(MEZZ_WINDOWS)
417 unsigned long Mode = ( Block ? 0 : 1 );
418 return ( ::ioctlsocket(this->
InternalSocket, FIONBIO, &Mode) != SOCKET_ERROR );
419 #elif defined(MEZZ_MACOSX) || defined(MEZZ_LINUX)
423 Flags = Block ? ( Flags & ~O_NONBLOCK ) : ( Flags | O_NONBLOCK );
435 unsigned long Ret = 0;
436 #if defined(MEZZ_WINDOWS)
437 if( ::ioctlsocket(this->
InternalSocket,FIONREAD,&Ret) == SOCKET_ERROR )
438 #elif defined(MEZZ_MACOSX) || defined(MEZZ_LINUX)
451 {
return ( ::setsockopt(this->
InternalSocket,Level,OptID,Value,ValueLen) != SOCKET_ERROR ); }
454 {
return ( ::getsockopt(this->
InternalSocket,Level,OptID,Value,ValueLen) != SOCKET_ERROR ); }
461 #if defined(MEZZ_WINDOWS)
462 return WSAGetLastError();
463 #elif defined(MEZZ_MACOSX) || defined(MEZZ_LINUX)
470 #if defined(MEZZ_WINDOWS)
472 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
474 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
475 (LPSTR)&Str, 0, NULL);
477 #elif defined(MEZZ_MACOSX) || defined(MEZZ_LINUX)
478 String ErrorMsg( strerror(ErrorCode) );
int ConvertLayer4ProtocolType(const TransportLayerProtocol Protocol)
Converts from a Mezzanine transport protocol type to it's internal counterpart.
static const UInt16 IPv6BinaryLength
The amount of space needed in a buffer to store the binary representation of an IPv6 address...
Thrown when an unknown internal error occurred.
UInt32 V6FlowInformation
An identifier used for routing IPv6 packets.
bool Boole
Generally acts a single bit, true or false.
sockaddr_storage ConvertToSocketStorage(const SystemAddress &MezzAddr)
Converts a Mezzanine address to a Berkeley sockets address.
short int ConvertLayer3ProtocolType(const NetworkLayerProtocol Protocol)
Converts from a Mezzanine address protocol type to it's internal counterpart.
void SetPort(const UInt16 TargetPort, const Boole NBO=false)
Sets the Port portion of the target system address.
const IPAddress & GetAddress() const
Gets the IP portion of the target system address.
NetworkLayerProtocol
This is an enum listing for recognized protocols on Layer 3 of the OSI model.
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
int ConvertToSocketType(const TransportLayerProtocol Protocol)
Converts from a Mezzanine transport protocol type to it's internal counterpart.
int Integer
A datatype used to represent any integer close to.
SocketDescription ConvertToSocketDescription(const sockaddr_storage &BerkAddr)
Converts a Berkeley sockets address to a Mezzanine socket descriptor.
UInt32 GetV6Flow() const
Gets the flow information to be used for routing packets to the target system.
Used for error conditions.
This implements the exception hiearchy for Mezzanine.
void SetBinaryAddress(const AddressContainer &Address)
Sets the binary form of the address being stored.
UInt32 GetV4Address(const Boole NBO=false) const
Gets a version 4 IP address value.
Internet Protocol version 6.
void SetV6Scope(const UInt32 Scope)
Sets the scope-id for the address.
uint16_t UInt16
An 16-bit unsigned integer.
TransportLayerProtocol
This is an enum listing for recognized protocols on Layer 4 of the OSI model.
UInt32 GetV6Scope() const
Gets the scope-id for the address.
UInt32 V6ScopeID
An identifier used to describe how accessible the address is to other hosts.
IPAddress SocketAddress
The IP address.
Used for error conditions.
const AddressContainer & GetBinaryAddress() const
Gets the stored binary address of this IPAddress.
UInt16 SocketPort
The port on which the socket sends and receives.
Thrown when parameters are checked at runtime and found invalid.
Transmission Control Protocol.
UInt16 GetPort(const Boole NBO=false) const
Gets the Port portion of the target system address.
Network::NetworkLayerProtocol GetProtocol() const
Gets the type of IP address this is.
SystemAddress ConvertToSystemAddress(const sockaddr_storage &BerkAddr)
Converts a Berkeley sockets address to a Mezzanine address.
void SetV6Flow(const UInt32 Flow)
Sets the flow information to be used.
A convenience class storing socket data that can be returned from utility methods and used to create ...
The bulk of the engine components go in this namspace.
void SetV4Address(const UInt32 Address, const Boole NBO=false)
Sets a version 4 IP address value.
std::vector< Int8 > AddressContainer
Convenience type to store the binary representation of IP addresses.
Used to allow type inference on PlatformSocket constructors.
std::string String
A datatype used to a series of characters.
A simple class that stores a complete set of information for establishing connections.
Internet Protocol version 4.