【发布时间】:2014-12-26 23:04:55
【问题描述】:
我写一个套接字服务器类其他类将来自 P>导出
当我从这个类派生,试图使一个网络服务器,基础类事件在那里。 我不希望他们看到在另一个班,从网络服务器,也没有使用Web服务器类最终用户获得。我会以某种方式能够隐藏他们进一步使用,但仍然可以进行订阅。因为在我的头在这一点的唯一方法是重新发明轮子的类,从我的基地导出! P>
下面如下的基类的一些代码:
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;
以上是我怎么有我的委托和事件! 真正的问题是在我的委托回调,在那里我把这些事件 P>
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事件在我的其他类导出这个类,那么我能做些实际的核心是什么?我的头碰了壁。也许从一开始一个坏结构? P>
【问题讨论】:
-
如果你想在导出 I>类拥有的唯一通道,使用
protected访问修饰符。否则,我不知道你想要完成的任务。 SPAN>