我写一个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
事件派生这个类,那么我能做些实际的核心?我的头撞墙。也许从开始就有一个糟糕的结构?
如果您希望* derived *类具有唯一访问权限,请使用'protected'访问修饰符。否则,我不确定你想要完成什么。 – BradleyDotNET 2014-10-30 19:12:08