• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

src/UdpServer.cpp

00001 /*
00002  *   This file is part of the Standard Portable Library (SPL).
00003  *
00004  *   SPL is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   SPL is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with SPL.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 #include <spl/net/UdpServer.h>
00018 
00019 UdpServer::UdpServer
00020 (
00021         int port, 
00022         RefCountPtr<IDelegateThreeParameter<UdpSocket&, Array<byte>&, int> > callback,
00023         int bufferSize
00024 )
00025 :       m_sock(port), 
00026         m_callback(callback), 
00027         m_thread(this, &UdpServer::Run), 
00028         m_running(false),
00029         m_bufSize(bufferSize)
00030 {
00031         m_sock.SetBlocking();
00032         m_thread.Start();
00033 }
00034 
00035 UdpServer::~UdpServer()
00036 {
00037 }
00038 
00039 void UdpServer::Run()
00040 {
00041         ASSERT(m_running == false);
00042         
00043         Array<byte> m_buf(m_bufSize);   
00044         m_running = true;
00045         
00046         while (m_running)
00047         {
00048                 int len = m_sock.Recv(m_buf);
00049                 if (0 > len)
00050                 {
00051                         m_running = false;
00052                         return;
00053                 }
00054                 m_callback->Call(m_sock, m_buf, len);
00055         }
00056 }
00057 
00058 void UdpServer::Join()
00059 {
00060         m_thread.Join();
00061 }
00062 
00063 void UdpServer::Close()
00064 {
00065         m_running = false;
00066         m_sock.Close();
00067 }
00068 
00069 #if defined(DEBUG) || defined(_DEBUG)
00070 void UdpServer::CheckMem() const
00071 {
00072         m_sock.CheckMem();
00073 }
00074 
00075 void UdpServer::ValidateMem() const
00076 {
00077         m_sock.ValidateMem();
00078 }
00079 #endif