Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

DatagramSocket.d

Go to the documentation of this file.
00001 /*******************************************************************************
00002 
00003         @file DatagramSocket.d
00004         
00005         Copyright (c) 2004 Kris Bell
00006         
00007         This software is provided 'as-is', without any express or implied
00008         warranty. In no event will the authors be held liable for damages
00009         of any kind arising from the use of this software.
00010         
00011         Permission is hereby granted to anyone to use this software for any 
00012         purpose, including commercial applications, and to alter it and/or 
00013         redistribute it freely, subject to the following restrictions:
00014         
00015         1. The origin of this software must not be misrepresented; you must 
00016            not claim that you wrote the original software. If you use this 
00017            software in a product, an acknowledgment within documentation of 
00018            said product would be appreciated but is not required.
00019 
00020         2. Altered source versions must be plainly marked as such, and must 
00021            not be misrepresented as being the original software.
00022 
00023         3. This notice may not be removed or altered from any distribution
00024            of the source.
00025 
00026         4. Derivative works are permitted, but they must carry this notice
00027            in full and credit the original source.
00028 
00029 
00030                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00031 
00032 
00033         @version        Initial version, March 2004      
00034         @author         Kris
00035 
00036 
00037 *******************************************************************************/
00038 
00039 module mango.io.DatagramSocket;
00040 
00041 private import  mango.io.Socket,
00042                 mango.io.Exception;
00043 
00044 private import  mango.io.model.IBuffer,
00045                 mango.io.model.IConduit;
00046 
00047 /*******************************************************************************
00048         
00049         Wrapper around datagram style of sockets, to simplify the API a
00050         bit, and to tie them into the IBuffer construct.
00051 
00052         Note that when used with a SocketListener you must first bind the
00053         DatagramSocket to a local adapter. This can be done by binding it
00054         to an InternetAddress constructed with a port only (ADDR_ANY).
00055 
00056 *******************************************************************************/
00057 
00058 class DatagramSocket : Socket, ISocketReader
00059 {
00060         /***********************************************************************
00061         
00062                 Create an internet datagram socket
00063 
00064         ***********************************************************************/
00065 
00066         this ()
00067         {
00068                 super (Socket.AddressFamily.INET, Socket.Type.DGRAM, Socket.Protocol.IP);                
00069         }
00070 
00071         /***********************************************************************
00072         
00073                 Read the available bytes from datagram into the given IBuffer.
00074                 The 'from' address will be populated appropriately
00075 
00076         ***********************************************************************/
00077 
00078         int read (IBuffer target, Address addr)
00079         in {
00080            assert (target);
00081            }
00082         body
00083         {
00084                 int reader (void[] src)
00085                 {
00086                         int count = receiveFrom (src, addr);
00087                         if (count <= 0)
00088                             count = IConduit.Eof;
00089                         return count;
00090                 }
00091 
00092                 return target.write (&reader);
00093         }
00094 
00095         /***********************************************************************
00096         
00097                 Read the available bytes from datagram into the given IBuffer.
00098                 The 'from' address will be populated appropriately
00099 
00100         ***********************************************************************/
00101 
00102         int read (IBuffer target)
00103         {
00104                 Address addr;
00105 
00106                 return read (target, addr);
00107         }
00108 
00109         /***********************************************************************
00110        
00111                 Write content from the specified buffer to the given address.
00112          
00113         ***********************************************************************/
00114 
00115         int write (IBuffer source, Address to)
00116         in {
00117            assert (to);
00118            assert (source);
00119            }
00120         body
00121         {
00122                 int writer (void[] src)
00123                 {
00124                         int count = sendTo (src, Socket.Flags.NONE, to);
00125                         if (count <= 0)
00126                             count = IConduit.Eof;
00127                         return count;
00128                 }
00129 
00130                 int count = source.read (&writer);
00131 
00132                 // if we didn't write everything, move remaining content
00133                 // to front of buffer for a subsequent write
00134                 source.compress();
00135                 return count;
00136         }
00137 }
00138 

Generated on Sat Apr 9 20:11:25 2005 for Mango by doxygen 1.3.6