[Delivery-boy-cvs] client/DeliveryBoyC/classes clsEasySocket.cs,1.10,1.11 clsEasyTCPServer.cs,1.4,1.
Status: Planning
Brought to you by:
mkloubert
From: Marcel K. <gen...@us...> - 2005-02-13 16:47:12
|
Update of /cvsroot/delivery-boy/client/DeliveryBoyC/classes In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv31071/DeliveryBoyC/classes Modified Files: clsEasySocket.cs clsEasyTCPServer.cs Log Message: - moved application run code to seperate method Run() - created 2 clsEasySocket Constructors and moved init code for this class to seperate private method - clsEasyTCPServer now uses an instance of TcpListener - also added events for connected and so on - added Global class Index: clsEasyTCPServer.cs =================================================================== RCS file: /cvsroot/delivery-boy/client/DeliveryBoyC/classes/clsEasyTCPServer.cs,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** clsEasyTCPServer.cs 5 Feb 2005 00:08:30 -0000 1.4 --- clsEasyTCPServer.cs 13 Feb 2005 16:46:53 -0000 1.5 *************** *** 24,27 **** --- 24,29 ---- using System; using System.Collections; + using System.Net.Sockets; + using System.Timers; /*! \class clsEasyTCPServer clsEasyTCPServer.cs "clsEasyTCPServer.cs" *************** *** 43,46 **** --- 45,55 ---- * \note * $Log$ + * Revision 1.5 2005/02/13 16:46:53 generalpd + * - moved application run code to seperate method Run() + * - created 2 clsEasySocket Constructors and moved init code for this class to seperate private method + * - clsEasyTCPServer now uses an instance of TcpListener + * - also added events for connected and so on + * - added Global class + * * Revision 1.4 2005/02/05 00:08:30 generalpd * - removed socket sample code *************** *** 60,70 **** * */ ! public class clsEasyTCPServer { ! /// Default listen queue size ! private const Int32 DEFAULT_LISTENQUEUE_SIZE = 16; ! /// Listen socket ! private clsEasySocket m_oListenSocket; /// Local port private Int32 m_i32Port = -1; --- 69,78 ---- * */ ! public class clsEasyTCPServer { ! /// Timer ! private Timer m_oTimer = null; /// Listen socket ! private TcpListener m_oListenSocket = null; /// Local port private Int32 m_i32Port = -1; *************** *** 73,79 **** /// Listen queue size private Int32 m_i32ListenQueue = -1; /// Connections ! private ArrayList m_oConnections = null; /*! \fn public clsEasyTCPServer --- 81,94 ---- /// Listen queue size private Int32 m_i32ListenQueue = -1; + /// Maximum connections + private UInt32 m_ui32MaxConnections = 0; /// Connections ! private ArrayList m_oConnections = null; ! ! /// Accept-Event-Handler-Struct ! public delegate void AcceptHandler(System.Object sender, System.Net.Sockets.Socket socket); ! /// Accept-Event-Handler ! public event AcceptHandler OnAccept; /*! \fn public clsEasyTCPServer *************** *** 86,96 **** public clsEasyTCPServer() { ! m_oListenSocket = null; ! Port = 0; ! ListenQueue = DEFAULT_LISTENQUEUE_SIZE; m_oConnections = new ArrayList(); } /*! \property public Int32 ListenQueue * \brief Returns the size of the listen queue or sets it. --- 101,165 ---- public clsEasyTCPServer() { ! //ListenQueue = m_oListenSocket.ListenQueue; ! MaxConnections = 0; m_oConnections = new ArrayList(); } + /*! \property public UInt32 Connections + * \brief Returns all availible clsEasySocket connections. + * + * \param (none) + * + * \return List of all connections + */ + public clsEasySocket[] Connections + { + get + { + Int32 i = 0; + ArrayList oBuffer = new ArrayList(); + + clsEasySocket oSocketBuffer = null; + + for (i = 0; i < m_oConnections.Count; i++) + { + oSocketBuffer = (clsEasySocket)m_oConnections[i]; + + if ( oSocketBuffer.Connected ) + oBuffer.Add(oSocketBuffer); + } + + m_oConnections = oBuffer; + + // create a new temporary array and copy all stuff from the ArrayList to the temp array + // for return + clsEasySocket[] o_es_buffer = new clsEasySocket[m_oConnections.Count]; + oBuffer.CopyTo(o_es_buffer); + + return o_es_buffer; + } + } + + /*! \property public UInt32 MaxConnections + * \brief Returns the size of maximum server connections or sets it. + * + * \param value New size (0 => unlimited) + * + * \return Maximum connections (0 => unlimited) + */ + public UInt32 MaxConnections + { + get + { + return m_ui32MaxConnections; + } + + set + { + m_ui32MaxConnections = value; + } + } + /*! \property public Int32 ListenQueue * \brief Returns the size of the listen queue or sets it. *************** *** 129,134 **** set { ! m_oListenSocket.LocalPort = value; ! m_i32Port = m_oListenSocket.LocalPort; } } --- 198,203 ---- set { ! m_i32Port = value; ! InitListenSocket(); } } *************** *** 145,149 **** get { ! return m_oListenSocket.Connected; } } --- 214,219 ---- get { ! // return m_oListenSocket.Connected; ! return true; } } *************** *** 163,182 **** try { ! if (!Listening) ! { ! // not listening ! return SetLastException(2, null); ! } ! else ! { ! if ( 0 == m_oListenSocket.Close()) ! { ! m_oListenSocket = null; ! return SetLastException(0, null); ! } ! else ! // socket error ! return SetLastException(3, m_oListenSocket.LastException); ! } } catch (Exception e) --- 233,241 ---- try { ! m_oTimer.Stop(); ! m_oTimer = null; ! m_oListenSocket.Stop(); ! ! return SetLastException(0, null); } catch (Exception e) *************** *** 200,231 **** try { ! if (Listening) ! { ! m_oListenSocket = null; ! ! // already listening ! return SetLastException(2, null); ! } ! else ! { ! m_oListenSocket = new clsEasySocket(); ! m_oListenSocket.LocalPort = Port; ! m_oListenSocket.ListenQueue = DEFAULT_LISTENQUEUE_SIZE; ! ! if ( 0 == m_oListenSocket.Listen()) ! return SetLastException(0, null); ! else ! { ! m_oListenSocket = null; ! ! // socket error ! return SetLastException(3, m_oListenSocket.LastException); ! } ! } } catch (Exception e) { - m_oListenSocket = null; - return SetLastException(1, e); } --- 259,275 ---- try { ! // Init listener and start it ! InitListenSocket(); ! m_oListenSocket.Start(); ! ! // init timer and start it ! m_oTimer = new Timer(250); ! m_oTimer.Elapsed += new ElapsedEventHandler(this.TimerElapsed); ! m_oTimer.Start(); ! ! return SetLastException(0, null); } catch (Exception e) { return SetLastException(1, e); } *************** *** 261,263 **** --- 305,347 ---- return ui32_ErrorCode; } + + /*! \fn private void InitListenSocket () + * \brief Inits the listening socket with all stuff + * + * \param (none) + * + * \return (none) + */ + private void InitListenSocket () + { + m_oListenSocket = new TcpListener(Port); + + return; + } + + /*! \fn private void TimerElapsed (object sender, ElapsedEventArgs e); + * \brief Timer elapsed + * + * \param sender This timer instance + * \param ElapsedEventArgs Arguments + * + * \return (none) + */ + private void TimerElapsed (object sender, ElapsedEventArgs e) + { + if (m_oListenSocket.Pending()) + { + Socket s = m_oListenSocket.AcceptSocket(); + + if (null != OnAccept) + OnAccept(this, s); + + if (s.Connected) + m_oConnections.Add( new clsEasySocket(s) ); + + Console.WriteLine(m_oConnections.Count.ToString()); + } + + return; + } } Index: clsEasySocket.cs =================================================================== RCS file: /cvsroot/delivery-boy/client/DeliveryBoyC/classes/clsEasySocket.cs,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** clsEasySocket.cs 5 Feb 2005 00:08:30 -0000 1.10 --- clsEasySocket.cs 13 Feb 2005 16:46:53 -0000 1.11 *************** *** 43,46 **** --- 43,53 ---- * \note * $Log$ + * Revision 1.11 2005/02/13 16:46:53 generalpd + * - moved application run code to seperate method Run() + * - created 2 clsEasySocket Constructors and moved init code for this class to seperate private method + * - clsEasyTCPServer now uses an instance of TcpListener + * - also added events for connected and so on + * - added Global class + * * Revision 1.10 2005/02/05 00:08:30 generalpd * - removed socket sample code *************** *** 125,133 **** public clsEasySocket() { // initialize all necessary vars m_oASCII = new ASCIIEncoding(); - m_oSocket = new Socket(c_AddressFamily, c_SocketType, c_ProtocolType); - RemoteAddress = "0.0.0.0"; RemotePort = 0; --- 132,166 ---- public clsEasySocket() { + m_oSocket = new Socket(c_AddressFamily, c_SocketType, c_ProtocolType); + + InitRest(); + } + + /*! \fn clsEasySocket + * \brief Inits all necessary stuff + * + * \param socket + * + * \return (none) + */ + public clsEasySocket(Socket socket) + { + m_oSocket = socket; + + InitRest(); + } + + /*! \fn private void InitRest () + * \brief Inits the rest + * + * \param (none) + * + * \return (none) + */ + private void InitRest () + { // initialize all necessary vars m_oASCII = new ASCIIEncoding(); RemoteAddress = "0.0.0.0"; RemotePort = 0; |