Shadowrun: Awakened 29 September 2011 - Build 871
UDPEndpoint.hpp
Go to the documentation of this file.
00001 /*
00002     Copyright (c) 2009-2010 Christopher A. Taylor.  All rights reserved.
00003 
00004     Redistribution and use in source and binary forms, with or without
00005     modification, are permitted provided that the following conditions are met:
00006 
00007     * Redistributions of source code must retain the above copyright notice,
00008       this list of conditions and the following disclaimer.
00009     * Redistributions in binary form must reproduce the above copyright notice,
00010       this list of conditions and the following disclaimer in the documentation
00011       and/or other materials provided with the distribution.
00012     * Neither the name of LibCat nor the names of its contributors may be used
00013       to endorse or promote products derived from this software without
00014       specific prior written permission.
00015 
00016     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00019     ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00020     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00021     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00022     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00023     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00024     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00025     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00026     POSSIBILITY OF SUCH DAMAGE.
00027 */
00028 
00029 #ifndef CAT_UDP_ENDPOINT_HPP
00030 #define CAT_UDP_ENDPOINT_HPP
00031 
00032 /*
00033     Windows version of thread pool sockets with IO Completion Ports
00034 
00035     Included from <cat/net/ThreadPoolSockets.hpp>
00036     Do not include directly
00037 */
00038 
00039 namespace cat {
00040 
00041 
00042 /*
00043     class UDPEndpoint
00044 
00045     Object that represents a UDP endpoint bound to a single port
00046 */
00047 class UDPEndpoint : public ThreadRefObject
00048 {
00049     Socket _socket;
00050     Port _port;
00051     volatile u32 _closing;
00052     bool _ipv6;
00053 
00054     struct RecvFromTag
00055     {
00056         // Not necessarily an IPv6 address, but we allocate enough space for one
00057         int addrLen;
00058         sockaddr_in6 addr;
00059     };
00060 
00061 public:
00062     UDPEndpoint(int priorityLevel);
00063     virtual ~UDPEndpoint();
00064 
00065     CAT_INLINE bool Valid() { return _socket != SOCKET_ERROR; }
00066     Port GetPort();
00067 
00068     // Is6() result is only valid AFTER Bind()
00069     CAT_INLINE bool Is6() { return _ipv6; }
00070     CAT_INLINE bool IsClosed() { return _closing != 0; }
00071 
00072     // For servers: Bind() with ignoreUnreachable = true ((default))
00073     // For clients: Bind() with ignoreUnreachable = false and call this
00074     //              after the first packet from the server is received.
00075     bool IgnoreUnreachable();
00076 
00077     // Disabled by default; useful for MTU discovery
00078     bool DontFragment(bool df = true);
00079 
00080     void Close(); // Invalidates this object
00081     bool Bind(bool onlySupportIPv4, Port port = 0, bool ignoreUnreachable = true, int kernelReceiveBufferBytes = 0);
00082     bool QueueWSARecvFrom();
00083 
00084     // If Is6() == true, the address must be promoted to IPv6
00085     // before calling PostWrite() with addr.PromoteTo6()
00086     // skip_bytes: Number of bytes to skip at the start of the post buffer
00087     bool Post(const NetAddr &addr, u8 *data, u32 data_bytes, u32 skip_bytes = 0);
00088 
00089 private:
00090     bool Read(AsyncBuffer *buffer = 0);
00091 
00092     bool OnReadComplete(ThreadPoolLocalStorage *tls, int error, AsyncBuffer *buffer, u32 bytes);
00093     bool OnWriteComplete(ThreadPoolLocalStorage *tls, int error, AsyncBuffer *buffer, u32 bytes);
00094 
00095 protected:
00096     virtual void OnRead(ThreadPoolLocalStorage *tls, const NetAddr &addr, u8 *data, u32 bytes) = 0; // false = close
00097     virtual bool OnWrite(ThreadPoolLocalStorage *tls, AsyncBuffer *buffer, u32 bytes) { return true; } // false = do not delete AsyncBase object
00098     virtual void OnClose() = 0;
00099     virtual void OnUnreachable(const NetAddr &addr) {} // Only IP is valid
00100 };
00101 
00102 
00103 } // namespace cat
00104 
00105 #endif // CAT_UDP_ENDPOINT_HPP

Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.

GNU Lesser General Public License 3 Sourceforge.net