SocketUDP.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/SocketUDP.h>
00029 #include <SFML/Network/SocketUDP.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 sfSocketUDP
00042 {
00043     sf::SocketUDP This;
00044 };
00045 
00046 
00050 sfSocketUDP* sfSocketUDP_Create()
00051 {
00052     return new sfSocketUDP;
00053 }
00054 
00055 
00059 void sfSocketUDP_Destroy(sfSocketUDP* Socket)
00060 {
00061     delete Socket;
00062 }
00063 
00064 
00068 sfBool sfSocketUDP_Bind(sfSocketUDP* Socket, unsigned short Port)
00069 {
00070     CSFML_CALL_RETURN(Socket, Bind(Port), sfFalse);
00071 }
00072 
00073 
00077 sfBool sfSocketUDP_Unbind(sfSocketUDP* Socket)
00078 {
00079     CSFML_CALL_RETURN(Socket, Unbind(), sfFalse);
00080 }
00081 
00082 
00086 sfSocketStatus sfSocketUDP_Send(sfSocketUDP* Socket, const char* Data, size_t Size, sfIPAddress Address, unsigned short Port)
00087 {
00088     CSFML_CHECK_RETURN(Socket, sfSocketError)
00089 
00090     // Convert the address
00091     sf::IPAddress Receiver(Address.Address);
00092 
00093     return static_cast<sfSocketStatus>(Socket->This.Send(Data, Size, Receiver, Port));
00094 }
00095 
00096 
00102 sfSocketStatus sfSocketUDP_Receive(sfSocketUDP* Socket, char* Data, size_t MaxSize, size_t* SizeReceived, sfIPAddress* Address)
00103 {
00104     CSFML_CHECK_RETURN(Socket, sfSocketError);
00105 
00106     // Call SFML internal function
00107     sf::IPAddress Sender;
00108     if (SizeReceived)
00109     {
00110         sf::Socket::Status Status = Socket->This.Receive(Data, MaxSize, *SizeReceived, Sender);
00111         if (Status != sf::Socket::Done)
00112             return static_cast<sfSocketStatus>(Status);
00113     }
00114     else
00115     {
00116         std::size_t Received = 0;
00117         sf::Socket::Status Status = Socket->This.Receive(Data, MaxSize, Received, Sender);
00118         if (Status != sf::Socket::Done)
00119             return static_cast<sfSocketStatus>(Status);
00120     }
00121 
00122     // Convert the address if needed
00123     if (Address)
00124         strcpy(Address->Address, Sender.ToString().c_str());
00125 
00126     return sfSocketDone;
00127 }
00128 
00129 
00133 sfSocketStatus sfSocketUDP_SendPacket(sfSocketUDP* Socket, sfPacket* Packet, sfIPAddress Address, unsigned short Port)
00134 {
00135     CSFML_CHECK_RETURN(Socket, sfSocketError);
00136     CSFML_CHECK_RETURN(Packet, sfSocketError);
00137 
00138     // Convert the address
00139     sf::IPAddress Receiver(Address.Address);
00140 
00141     return static_cast<sfSocketStatus>(Socket->This.Send(Packet->This, Receiver, Port));
00142 }
00143 
00144 
00150 sfSocketStatus sfSocketUDP_ReceivePacket(sfSocketUDP* Socket, sfPacket* Packet, sfIPAddress* Address)
00151 {
00152     CSFML_CHECK_RETURN(Socket, sfSocketError);
00153     CSFML_CHECK_RETURN(Packet, sfSocketError);
00154 
00155     sf::IPAddress Sender;
00156     sf::Socket::Status Status = Socket->This.Receive(Packet->This, Sender);
00157     if (Status != sf::Socket::Done)
00158         return static_cast<sfSocketStatus>(Status);
00159 
00160     // Convert the address if needed
00161     if (Address)
00162         strcpy(Address->Address, Sender.ToString().c_str());
00163 
00164     return sfSocketDone;
00165 }
00166 
00167 
00172 sfBool sfSocketUDP_IsValid(sfSocketUDP* Socket)
00173 {
00174     CSFML_CALL_RETURN(Socket, IsValid(), sfFalse);
00175 }