Estou desenvolvendo um servidor TCP Asynchronous em C#, mas não como fazer para o meu sistema detectar que o usuário desconectou ou foi desconectado por alguma razão e executar um procedimento para a remoção do mesmo da lista de usuários. O meu objeto Usuário está assim no momento:
public int id;
public string name;
private Socket _socket;
private byte[] _buffet;
public PlayerInfo playerInfo (){
name = "";
return new PlayerInfo (id, name);
}
public ClientInfo(Action<int, String> callback, Socket client){
try{
this.callback = callback;
this._socket = client;
this._buffet = new byte[this._socket.ReceiveBufferSize];
this._socket.BeginReceive (this._buffet, 0, this._buffet.Length, SocketFlags.None, new AsyncCallback (receiveCallBack), null);
}catch(Exception){
}
}
public void Send(Message msg){
if (this._socket.Connected) {
try{
byte[] buffer = SMS.Enconde.GetEncode().GetBytes(JsonConvert.SerializeObject(msg));
this._socket.BeginSend(buffer,0, buffer.Length, SocketFlags.None, new AsyncCallback(SendCallBack), null);
}catch(Exception){
}
}
}
private void SendCallBack(IAsyncResult IA){
try{
this._socket.EndSend(IA);
}catch(Exception){
}
}
private void receiveCallBack(IAsyncResult IA){
try{
int received = this._socket.EndReceive (IA);
Array.Resize (ref this._buffet, received);
String text = SMS.Enconde.GetEncode().GetString(this._buffet);
Array.Resize (ref this._buffet, this._socket.ReceiveBufferSize);
this._socket.BeginReceive (this._buffet, 0, this._buffet.Length, SocketFlags.None, new AsyncCallback (receiveCallBack), null);
}catch(Exception){
}
}