RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Cleaned Listener.cs a bit

Rawolf

Wanderer
Cleaned Listener.cs a bit

Ok I know what I did made no sense except to make the code cleaner and to avoid throwing an array of size 0 to the MessagePumper. This part of the core is very important & used every time there's incoming data, that's the main reason I wanted to clean it up.

In Listener.cs add the following function:

Code:
      public bool bSocketWaiting()
        {
            if (m_Accepted.Count > 0)
                return true;
            else
                return false;
        }

That's a function to check if we have any connection waiting in the m_Accepted queue.

Now modify the Socket[] Slice() function by removing the following lines:
Code:
if ( m_Accepted.Count == 0 )
	return m_EmptySockets;
Those lines are no longer necessary.


Now open MessagePump.cs

and change the CheckListener() function to this:

Code:
private void CheckListener()
		{
			for ( int j = 0; j < m_Listeners.Length; ++j )
			{
				if (m_Listeners[j].bSocketWaiting())
				{
					Socket[] accepted = m_Listeners[j].Slice();

					for ( int i = 0; i < accepted.Length; ++i )
					{
						NetState ns = new NetState( accepted[i], this );
						ns.Start();

						if ( ns.Running )
							Console.WriteLine( "Client: {0}: Connected. [{1} Online]", ns, 						}		NetState.Instances.Count );
				}
			}
		}

That's it. Thanks.
 

Zippy

Razor Creator
Shouldn't that be a continue and not a return, right? Otherwise all other listeners would never be checked if the first one did not receive any new connections.

I'm not sure I understand the point of this change though. The old code implicitly accomplished the same thing. Returning an empty array means the for loop will never get run, and I think that is maybe a little bit more clear than this code.
 

Rawolf

Wanderer
You're right, I forgot that it's looping through every listener. But in that case I'd just modify CheckListener() to:
Code:
		private void CheckListener()
		{
			for ( int j = 0; j < m_Listeners.Length; ++j )
			{
                           		 if (m_Listeners[j].bSocketWaiting())
                               		 {
					Socket[] accepted = m_Listeners[j].Slice();

					for ( int i = 0; i < accepted.Length; ++i )
					{
						NetState ns = new NetState( accepted[i], this );
						ns.Start();

						if ( ns.Running )
							Console.WriteLine( "Client: {0}: Connected. [{1} Online]", ns, NetState.Instances.Count );
					}
				}
			}
		}

For me it feels cleaner as I'm not passing around any stuff, but I guess it's just a personal perspective. Booleans are great to check, as they're only 1 byte and fast.
 
Top