[Delivery-boy-cvs] client/DeliveryBoyC/classes clsEasySocket.cs,1.11,1.12 clsEasyTCPServer.cs,1.6,1.
Status: Planning
Brought to you by:
mkloubert
From: Marcel K. <gen...@us...> - 2005-04-27 15:34:51
|
Update of /cvsroot/delivery-boy/client/DeliveryBoyC/classes In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8587/DeliveryBoyC/classes Modified Files: clsEasySocket.cs clsEasyTCPServer.cs Log Message: - some source code optimizations Index: clsEasyTCPServer.cs =================================================================== RCS file: /cvsroot/delivery-boy/client/DeliveryBoyC/classes/clsEasyTCPServer.cs,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** clsEasyTCPServer.cs 5 Mar 2005 10:29:43 -0000 1.6 --- clsEasyTCPServer.cs 27 Apr 2005 15:34:35 -0000 1.7 *************** *** 31,35 **** * \brief Easy-to-use TCP Server class. * ! * This class is a simple TCP server class that uses clsEasySocket * * \author generalpd --- 31,35 ---- * \brief Easy-to-use TCP Server class. * ! * This class is a simple TCP server class that uses EasySocket * * \author generalpd *************** *** 45,48 **** --- 45,51 ---- * \note * $Log$ + * Revision 1.7 2005/04/27 15:34:35 generalpd + * - some source code optimizations + * * Revision 1.6 2005/03/05 10:29:43 generalpd * - clsEasyTCPServer class now works *************** *** 72,92 **** * */ ! public class clsEasyTCPServer { /// Timer ! private Timer m_oTimer = null; /// Listen socket ! private TcpListener m_oListenSocket = null; /// Local port ! private Int32 m_i32Port = -1; ! /// Last occurred exception ! private Exception m_oLastException = null; /// Listen queue size ! private Int32 m_i32ListenQueue = -1; /// Maximum connections ! private UInt32 m_ui32MaxConnections = 0; /// Connections ! private clsEasySocket[] m_oConnections; /// Accept-Event-Handler-Struct --- 75,93 ---- * */ ! public class EasyTCPServer { /// Timer ! private System.Timers.Timer m_oTimer = null; /// Listen socket ! private System.Net.Sockets.TcpListener m_oListenSocket = null; /// Local port ! private System.Int32 m_i32Port = -1; /// Listen queue size ! private System.Int32 m_i32ListenQueue = -1; /// Maximum connections ! private System.UInt32 m_ui32MaxConnections = 0; /// Connections ! private DeliveryBoyClient.Classes.EasySocket[] m_oConnections; /// Accept-Event-Handler-Struct *************** *** 95,128 **** public event AcceptHandler OnAccept; /// Receive-Event-Handler-Struct ! public delegate void ReceiveHandler(System.Object sender, clsEasySocket socket, System.Byte[] buffer, System.Int32 bytes_read); /// Receive-Event-Handler public event ReceiveHandler OnReceive; /*! \fn public clsEasyTCPServer * \brief Inits all necessary stuff * - * \param (none) - * - * \return (none) */ ! public clsEasyTCPServer() { //ListenQueue = m_oListenSocket.ListenQueue; ! MaxConnections = 0; } /*! \property public UInt32 Connections ! * \brief Returns all availible clsEasySocket connections. ! * ! * \param (none) * * \return List of all connections */ ! public clsEasySocket[] Connections { ! get ! { ! return m_oConnections; ! } } --- 96,125 ---- public event AcceptHandler OnAccept; /// Receive-Event-Handler-Struct ! public delegate void ReceiveHandler(System.Object sender, DeliveryBoyClient.Classes.EasySocket socket, System.Byte[] buffer, System.Int32 bytes_read); /// Receive-Event-Handler public event ReceiveHandler OnReceive; + /// Error-Event-Handler-Struct + public delegate void ErrorHandler(System.Object sender, System.Exception e); + /// Error-Event-Handler + public event ErrorHandler OnError; /*! \fn public clsEasyTCPServer * \brief Inits all necessary stuff * */ ! public EasyTCPServer() { //ListenQueue = m_oListenSocket.ListenQueue; ! this.MaxConnections = 0; } /*! \property public UInt32 Connections ! * \brief Returns all availible EasySocket connections. * * \return List of all connections */ ! public DeliveryBoyClient.Classes.EasySocket[] Connections { ! get { return this.m_oConnections; } } *************** *** 134,154 **** * \return Maximum connections (0 => unlimited) */ ! public UInt32 MaxConnections { ! get ! { ! return m_ui32MaxConnections; ! } set { ! int i = 0; ! ! m_ui32MaxConnections = value; ! m_oConnections = new clsEasySocket[m_ui32MaxConnections]; ! for (i = 0; i < m_oConnections.GetLength(0); i++) ! m_oConnections[i] = new clsEasySocket(); } } --- 131,147 ---- * \return Maximum connections (0 => unlimited) */ ! public System.UInt32 MaxConnections { ! get { return this.m_ui32MaxConnections; } set { ! System.Int32 i = 0; ! this.m_ui32MaxConnections = value; ! this.m_oConnections = new DeliveryBoyClient.Classes.EasySocket[this.m_ui32MaxConnections]; ! for (i = 0; i < this.m_oConnections.GetLength(0); i++) ! this.m_oConnections[i] = new DeliveryBoyClient.Classes.EasySocket(); } } *************** *** 161,175 **** * \return Size of listen queue */ ! public Int32 ListenQueue { ! get ! { ! return m_i32ListenQueue; ! } ! set ! { ! m_i32ListenQueue = value; ! } } --- 154,162 ---- * \return Size of listen queue */ ! public System.Int32 ListenQueue { ! get { return this.m_i32ListenQueue; } ! set { this.m_i32ListenQueue = value; } } *************** *** 181,195 **** * \return Port */ ! public Int32 Port { ! get ! { ! return m_i32Port; ! } set { ! m_i32Port = value; ! InitListenSocket(); } } --- 168,179 ---- * \return Port */ ! public System.Int32 Port { ! get { return this.m_i32Port; } set { ! this.m_i32Port = value; ! this.InitListenSocket(); } } *************** *** 198,212 **** * \brief Return if this server is listening * - * \param (none) - * * \return Is listening (true) or not (false) */ ! public bool Listening { ! get ! { ! // return m_oListenSocket.Connected; ! return true; ! } } --- 182,190 ---- * \brief Return if this server is listening * * \return Is listening (true) or not (false) */ ! public System.Boolean Listening { ! get { return true; } } *************** *** 214,237 **** * \brief Stops the server. * - * \param (none) - * - * \return Error code:<BR> - * 0 => no error<BR> - * 1 => general exception<BR> - * 2 => not listening */ ! public UInt32 Stop () { try { ! m_oTimer.Stop(); ! m_oTimer = null; ! m_oListenSocket.Stop(); ! return SetLastException(0, null); } ! catch (Exception e) { ! return SetLastException(1, e); } } --- 192,209 ---- * \brief Stops the server. * */ ! public void Stop () { try { ! this.m_oTimer.Stop(); ! this.m_oTimer = null; ! this.m_oListenSocket.Stop(); ! return; } ! catch (System.Exception e) { ! this.pOnError(e); } } *************** *** 240,311 **** * \brief Starts the server. * - * \param (none) - * - * \return Error code:<BR> - * 0 => no error<BR> - * 1 => general exception<BR> - * 2 => already listening */ ! public UInt32 Start () { try { // Init listener and start it ! InitListenSocket(); ! m_oListenSocket.Start(); // init timer and start it ! m_oTimer = new Timer(125); ! m_oTimer.Elapsed += new ElapsedEventHandler(this.TimerElapsed); ! m_oTimer.Start(); ! return SetLastException(0, null); } ! catch (Exception e) { ! return SetLastException(1, e); } } - /*! \property public Exception LastException - * \brief Returns the last occurred exception. - * - * \param (none) - * - * \return Exception object (null => no error) - */ - public Exception LastException - { - get - { - return m_oLastException; - } - } - - /*! \fn private UInt32 SetLastException (UInt32 ui32_ErrorCode, Exception oNewException) - * \brief Sets the last occurred exception. - * - * \param ui32_ErrorCode Error code. - * \param oNewException The new exception object. - * - * \return Error code submitted by ui32_ErrorCode - */ - private UInt32 SetLastException (UInt32 ui32_ErrorCode, Exception oNewException) - { - m_oLastException = oNewException; - - 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; --- 212,244 ---- * \brief Starts the server. * */ ! public void Start () { try { // Init listener and start it ! this.InitListenSocket(); ! this.m_oListenSocket.Start(); // init timer and start it ! this.m_oTimer = new System.Timers.Timer(125); ! this.m_oTimer.Elapsed += new System.Timers.ElapsedEventHandler(this.TimerElapsed); ! this.m_oTimer.Start(); ! return; } ! catch (System.Exception e) { ! this.pOnError(e); } } /*! \fn private void InitListenSocket () * \brief Inits the listening socket with all stuff * */ private void InitListenSocket () { ! this.m_oListenSocket = new System.Net.Sockets.TcpListener(this.Port); return; *************** *** 320,351 **** * \return (none) */ ! private void TimerElapsed (object sender, ElapsedEventArgs e) { ! if (m_oListenSocket.Pending()) { ! int i = 0; ! bool boolIsFull = true; ! Socket s = m_oListenSocket.AcceptSocket(); ! if (null != OnAccept) ! OnAccept(this, s); ! if (s.Connected) ! { ! for (i = 0; i < m_oConnections.GetLength(0); i++) ! { ! if (!m_oConnections[i].Connected) ! { ! m_oConnections[i] = new clsEasySocket(s); ! m_oConnections[i].OnReceive += new clsEasySocket.ReceiveHandler(ReceiveData); ! boolIsFull = false; ! break; ! } ! } ! if (boolIsFull) ! s.Close(); ! } } --- 253,291 ---- * \return (none) */ ! private void TimerElapsed (System.Object sender, System.Timers.ElapsedEventArgs e) { ! try { ! if (this.m_oListenSocket.Pending()) ! { ! System.Int32 i = 0; ! System.Boolean boolIsFull = true; ! System.Net.Sockets.Socket s = this.m_oListenSocket.AcceptSocket(); ! if (null != this.OnAccept) ! this.OnAccept(this, s); ! if (s.Connected) ! { ! for (i = 0; i < this.m_oConnections.GetLength(0); i++) ! { ! if (!this.m_oConnections[i].Connected) ! { ! this.m_oConnections[i] = new DeliveryBoyClient.Classes.EasySocket(s); ! this.m_oConnections[i].OnReceive += new DeliveryBoyClient.Classes.EasySocket.ReceiveHandler(this.ReceiveData); ! boolIsFull = false; ! break; ! } ! } ! if (boolIsFull) ! s.Close(); ! } ! } ! } ! catch (System.Exception e2) ! { ! this.pOnError(e2); } *************** *** 364,371 **** private void ReceiveData (System.Object sender, System.Byte[] buffer, System.Int32 bytes_read) { ! if (null != OnReceive) ! OnReceive(this, (clsEasySocket)sender, buffer, bytes_read); return; } } --- 304,328 ---- private void ReceiveData (System.Object sender, System.Byte[] buffer, System.Int32 bytes_read) { ! if (null != this.OnReceive) ! this.OnReceive(this, (DeliveryBoyClient.Classes.EasySocket)sender, buffer, bytes_read); return; } + + /*! \fn private void pOnError ( Exception e ) + * \brief Is invoked if an error occurred + * + * \param ar Async result + * + * \return (none) + */ + private void pOnError ( System.Exception e ) + { + if (null != this.OnError) + this.OnError(this, e); + else + throw (e); + + return; + } } Index: clsEasySocket.cs =================================================================== RCS file: /cvsroot/delivery-boy/client/DeliveryBoyC/classes/clsEasySocket.cs,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** clsEasySocket.cs 13 Feb 2005 16:46:53 -0000 1.11 --- clsEasySocket.cs 27 Apr 2005 15:34:34 -0000 1.12 *************** *** 43,46 **** --- 43,49 ---- * \note * $Log$ + * Revision 1.12 2005/04/27 15:34:34 generalpd + * - some source code optimizations + * * Revision 1.11 2005/02/13 16:46:53 generalpd * - moved application run code to seperate method Run() *************** *** 65,648 **** [...988 lines suppressed...] ! * \return (none) ! */ ! private void pOnConnectWithRemote ( System.IAsyncResult ar ) ! { ! try ! { ! this.Socket.EndConnect(ar); ! if (null != this.OnConnectWithRemote) ! this.OnConnectWithRemote(this, this.m_oSocket); ! return; ! } ! catch (System.Exception e) ! { ! this.pOnError(e); ! } ! } ! } } |