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 
00027                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00028 
00029 
00030         @version        Initial version, March 2004      
00031         @author         Kris
00032 
00033 
00034 *******************************************************************************/
00035 
00036 module mango.io.DatagramSocket;
00037 
00038 private import  mango.io.Socket,
00039                 mango.io.Exception;
00040 
00041 private import  mango.io.model.IBuffer,
00042                 mango.io.model.IConduit;
00043 
00044 /*******************************************************************************
00045         
00046         Wrapper around datagram style of sockets, to simplify the API a
00047         bit, and to tie them into the IBuffer construct.
00048 
00049         Note that when used with a SocketListener you must first bind the
00050         DatagramSocket to a local adapter. This can be done by binding it
00051         to an InternetAddress constructed with a port only (ADDR_ANY).
00052 
00053 *******************************************************************************/
00054 
00055 class DatagramSocket : Socket, ISocketReader
00056 {
00057         /***********************************************************************
00058         
00059                 Create an internet datagram socket
00060 
00061         ***********************************************************************/
00062 
00063         this ()
00064         {
00065                 super (Socket.AddressFamily.INET, Socket.Type.DGRAM, Socket.Protocol.IP);                
00066         }
00067 
00068         /***********************************************************************
00069         
00070                 Read the available bytes from datagram into the given IBuffer.
00071                 The 'from' address will be populated appropriately
00072 
00073         ***********************************************************************/
00074 
00075         int read (IBuffer target, Address addr)
00076         in {
00077            assert (target);
00078            }
00079         body
00080         {
00081                 int reader (void[] src)
00082                 {
00083                         int count = receiveFrom (src, addr);
00084                         if (count <= 0)
00085                             count = IConduit.Eof;
00086                         return count;
00087                 }
00088 
00089                 return target.write (&reader);
00090         }
00091 
00092         /***********************************************************************
00093         
00094                 Read the available bytes from datagram into the given IBuffer.
00095                 The 'from' address will be populated appropriately
00096 
00097         ***********************************************************************/
00098 
00099         int read (IBuffer target)
00100         {
00101                 Address addr;
00102 
00103                 return read (target, addr);
00104         }
00105 
00106         /***********************************************************************
00107        
00108                 Write content from the specified buffer to the given address.
00109          
00110         ***********************************************************************/
00111 
00112         int write (IBuffer source, Address to)
00113         in {
00114            assert (to);
00115            assert (source);
00116            }
00117         body
00118         {
00119                 int writer (void[] src)
00120                 {
00121                         int count = sendTo (src, Socket.Flags.NONE, to);
00122                         if (count <= 0)
00123                             count = IConduit.Eof;
00124                         return count;
00125                 }
00126 
00127                 int count = source.read (&writer);
00128 
00129                 // if we didn't write everything, move remaining content
00130                 // to front of buffer for a subsequent write
00131                 source.compress();
00132                 return count;
00133         }
00134 }
00135 

Generated on Sun Nov 7 19:06:50 2004 for Mango by doxygen 1.3.6