2014-10-30 102 views
-1

我寫一個socket服務器類,其他類將從繼承問題的基礎VS孩子

得到當我從這個類派生,試圖使一個網絡服務器,基礎類事件在那裏。 我不希望它們在另一個類中可見,從Web服務器派生,也不是使用Web服務器類的最終用戶。我會以某種方式隱藏它們以供進一步使用,但仍然能夠訂閱它們。因爲在這一點上,我腦海中唯一的辦法就是重新發明我的基地的班級!

這裏如下基類的一些代碼:

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; 

以上是我怎麼有我的委託和事件! 真正的問題是在我的委託回調,在那裏我把這些事件

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); 
     } 
    } 

如果我不能同意我在其他類中的DataReceived事件派生這個類,那麼我能做些實際的核心?我的頭撞牆。也許從開始就有一個糟糕的結構?

+0

如果您希望* derived *類具有唯一訪問權限,請使用'protected'訪問修飾符。否則,我不確定你想要完成什麼。 – BradleyDotNET 2014-10-30 19:12:08

回答

1

我不相信有一種方法可以完全隱藏繼承的成員。類似問題「Hiding inherited members」的一個建議是重寫它們,將它們標記爲obselete,並將DesignerSerializationVisibility設置爲隱藏。

如果您只想使用組件中的那些成員,但將它們隱藏到外部程序集中,請將它們標記爲internal而不是public

如果你真的想隱藏它們,你可以將你的web服務器類寫成一個包裝而不是子類。在內部使用套接字服務器類並訂閱事件。