SocketTCP.cpp

00001 
00002 //
00003 // SFML - Simple and Fast Multimedia Library
00004 // Copyright (C) 2007 Laurent Gomila (laurent.gom@gmail.com)
00005 //
00006 // This software is provided 'as-is', without any express or implied warranty.
00007 // In no event will the authors be held liable for any damages arising from the use of this software.
00008 //
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it freely,
00011 // subject to the following restrictions:
00012 //
00013 // 1. The origin of this software must not be misrepresented;
00014 //    you must not claim that you wrote the original software.
00015 //    If you use this software in a product, an acknowledgment
00016 //    in the product documentation would be appreciated but is not required.
00017 //
00018 // 2. Altered source versions must be plainly marked as such,
00019 //    and must not be misrepresented as being the original software.
00020 //
00021 // 3. This notice may not be removed or altered from any source distribution.
00022 //
00024 
00026 // Headers
00028 #include <SFML/Network/SocketTCP.h>
00029 #include <SFML/Network/SocketTCP.hpp>
00030 #include <SFML/Network/IPAddress.hpp>
00031 #include <SFML/Network/Packet.hpp>
00032 #include <SFML/Internal.h>
00033 
00034 
00035 // WARNING : this structure must always be the SAME as in Network/Packet.h
00036 struct sfPacket
00037 {
00038     sf::Packet This;
00039 };
00040 
00041 struct sfSocketTCP
00042 {
00043     sf::SocketTCP This;
00044 };
00045 
00046 
00050 sfSocketTCP* sfSocketTCP_Create()
00051 {
00052     return new sfSocketTCP;
00053 }
00054 
00055 
00059 void sfSocketTCP_Destroy(sfSocketTCP* Socket)
00060 {
00061     if (Socket)
00062     {
00063         Socket->This.Close();
00064         delete Socket;
00065     }
00066 }
00067 
00068 
00072 sfBool sfSocketTCP_Connect(sfSocketTCP* Socket, unsigned short Port, sfIPAddress HostAddress)
00073 {
00074     sf::IPAddress Address(HostAddress.Address);
00075 
00076     CSFML_CALL_RETURN(Socket, Connect(Port, Address), sfFalse);
00077 }
00078 
00079 
00083 sfBool sfSocketTCP_Listen(sfSocketTCP* Socket, unsigned short Port)
00084 {
00085     CSFML_CALL_RETURN(Socket, Listen(Port), sfFalse);
00086 }
00087 
00088 
00094 sfSocketStatus sfSocketTCP_Accept(sfSocketTCP* Socket, sfSocketTCP** Connected, sfIPAddress* Address)
00095 {
00096     CSFML_CHECK_RETURN(Socket,    sfSocketError);
00097     CSFML_CHECK_RETURN(Connected, sfSocketError);
00098 
00099     // Call SFML internal function
00100     sf::IPAddress ClientAddress;
00101     sf::SocketTCP Client;
00102     sf::Socket::Status Status = Socket->This.Accept(Client, &ClientAddress);
00103     if (Status != sf::Socket::Done)
00104         return static_cast<sfSocketStatus>(Status);
00105 
00106     // Convert the client socket returned
00107     *Connected = sfSocketTCP_Create();
00108     (*Connected)->This = Client;
00109 
00110     // Convert the address if needed
00111     if (Address)
00112         strcpy(Address->Address, ClientAddress.ToString().c_str());
00113 
00114     return sfSocketDone;
00115 }
00116 
00117 
00121 sfSocketStatus sfSocketTCP_Send(sfSocketTCP* Socket, const char* Data, size_t Size)
00122 {
00123     CSFML_CHECK_RETURN(Socket, sfSocketError);
00124 
00125     return static_cast<sfSocketStatus>(Socket->This.Send(Data, Size));
00126 }
00127 
00128 
00132 sfSocketStatus sfSocketTCP_Receive(sfSocketTCP* Socket, char* Data, size_t MaxSize, size_t* SizeReceived)
00133 {
00134     CSFML_CHECK_RETURN(Socket, sfSocketError);
00135 
00136     if (SizeReceived)
00137     {
00138         return static_cast<sfSocketStatus>(Socket->This.Receive(Data, MaxSize, *SizeReceived));
00139     }
00140     else
00141     {
00142         std::size_t Size = 0;
00143         return static_cast<sfSocketStatus>(Socket->This.Receive(Data, MaxSize, Size));
00144     }
00145 }
00146 
00147 
00151 sfSocketStatus sfSocketTCP_SendPacket(sfSocketTCP* Socket, sfPacket* Packet)
00152 {
00153     CSFML_CHECK_RETURN(Socket, sfSocketError);
00154     CSFML_CHECK_RETURN(Packet, sfSocketError);
00155 
00156     return static_cast<sfSocketStatus>(Socket->This.Send(Packet->This));
00157 }
00158 
00159 
00163 sfSocketStatus sfSocketTCP_ReceivePacket(sfSocketTCP* Socket, sfPacket* Packet)
00164 {
00165     CSFML_CHECK_RETURN(Socket, sfSocketError);
00166     CSFML_CHECK_RETURN(Packet, sfSocketError);
00167 
00168     return static_cast<sfSocketStatus>(Socket->This.Receive(Packet->This));
00169 }
00170 
00171 
00176 sfBool sfSocketTCP_IsValid(sfSocketTCP* Socket)
00177 {
00178     CSFML_CALL_RETURN(Socket, IsValid(), sfFalse);
00179 }