-1

I am writing a socket server class which other classes will derive from

When I derive from this class, trying to make a webserver, the base class events are there. I don't want them to be visible in another classes, deriving from the webserver, nor the end user using the webserver class. I would somehow be able to hide them for further use but still be able to subscribe to them. Because the only way in my head at this point is reinventing the wheel in the class that derives from my base!

Here follows some code of the base class:

    public delegate void ConnectionRequest (CoreAsyncSocketObj client);
    public delegate void DataReceived (CoreAsyncSocketObj client, string data);
    public delegate void DataSent (CoreAsyncSocketObj client);
    public delegate void ConnectionClose (CoreAsyncSocketObj client);
    public delegate void ServerFull (CoreAsyncSocketObj client);

    /// <summary>
    /// Occurs when a client connects to server.
    /// </summary>
    public event ConnectionRequest OnConnectionRequest;
    /// <summary>
    /// Occurs when server receives data from any client.
    /// </summary>
    public event DataReceived OnDataReceived;
    /// <summary>
    /// Occurs when data has been sent to client.
    /// </summary>
    public event DataSent OnDataSent;
    /// <summary>
    /// Occurs when client has closed its connection.
    /// </summary>
    public event ConnectionClose OnConnectionClose;
    /// <summary>
    /// Occurs when server is full according to the MaximumUsers property.
    /// </summary>
    public event ServerFull OnServerFull;

The above is how i have my delegates and events! The question is really on my delegate callbacks, where i call these events

    void Receive (IAsyncResult ar)
    {
        CoreAsyncSocketObj client = (CoreAsyncSocketObj)ar.AsyncState;
        string data = string.Empty;
        try
        {
            int bytesReceived = client.sock.EndReceive (ar);
            if (bytesReceived > 0) // client is connected
            {
                if (this.protocolEOL == string.Empty) // If no delimeter then just raise event and restart receiving.
                {
                    if (this.OnDataReceived != null) 
                    {
                        data = Encoding.GetEncoding((int)this.encoder).GetString(client.buffer);
                        this.OnDataReceived (client, data);
                        client.sock.BeginReceive (client.buffer, 0, client.buffer.Length, SocketFlags.None,
                                                new AsyncCallback (Receive), client);
                    }
                } 
                else // A specified delimter (EOL).
                {
                    // Append to a second buffer using the specified encoding.
                    // Check the end of the buffer if it matches the EOL.
                    client.stringBuffer.Append(Encoding.GetEncoding((int)this.encoder).GetString(client.buffer));
                    //client.stringBuffer.Append (Encoding.Default.GetString (client.buffer));
                    data = client.stringBuffer.ToString ();
                    if (data.EndsWith (this.protocolEOL) == true) 
                    {
                        if (this.OnDataReceived != null)
                            this.OnDataReceived (client, data);

                        client.stringBuffer.Clear(); // Clear buffer
                    }
                    client.sock.BeginReceive (client.buffer, 0, client.buffer.Length, SocketFlags.None,
                                              new AsyncCallback (Receive), client);  // restart
                }
            } 
            else // Client has closed its connection
            {
                this.DisconnectClient(client);
                if (this.OnConnectionClose != null)
                    this.OnConnectionClose(client);
            }
        }
        catch(SocketException exception)
        {
            Logger.Write(exception.Message + "\"" + exception.ErrorCode.ToString() + "\"");
        }
        catch(ObjectDisposedException exception)
        {
            Logger.Write(exception.Message);
        }
        catch(Exception exception)
        {
            Logger.Write(exception.Message);
        }
    }

If I can't subscribe to the DataReceived event in my other class that derives this class, then what can I do about the actual core? My head hit a wall. Maybe a bad structure from the begining?

edmz
  • 8,220
  • 2
  • 26
  • 45
Kepp
  • 1
  • 1
  • If you want the *derived* classes to have the only access, use the `protected` access modifier. Otherwise, I'm not sure what you want to accomplish. – BradleyDotNET Oct 30 '14 at 19:12

1 Answers1

1

I don't believe there is a way to completely hide inherited members. One suggestion from a similar question "Hiding inherited members" is to override them, mark them obselete, and set DesignerSerializationVisibility to Hidden.

If you only want to use those members from within your assembly but hide them to external assemblies, mark them as internal instead of public.

If you truly want to hide them, you could write your web server class as a wrapper rather than a subclass. Internally use the socket server class and subscribe to the events.

Community
  • 1
  • 1
Robert Graves
  • 2,290
  • 3
  • 20
  • 24