private static void DisconnetUser(string nickName) { SessionCurrent sessionDisconnect = GetSessionByNickName(nickName); MessageTransport objMessageUser = new MessageTransport(); try { objMessageUser._typeObject = Enums.TypeObject.FromServer; objMessageUser.Message = "Ud será desconectado"; sessionDisconnect.ObjSocket.Send(MessageTransportToByte(objMessageUser)); Thread.Sleep(3000); sessionDisconnect.ObjSocket.Shutdown(SocketShutdown.Both); sessionDisconnect.ObjSocket.Close(); } catch { sessionDisconnect.ObjSocket.Close(); } finally { clientSockets.Remove(sessionDisconnect); } }
private static void AcceptCallback(IAsyncResult AR) { Socket socket; try { socket = serverSocket.EndAccept(AR); } catch (ObjectDisposedException) // I cannot seem to avoid this (on exit when properly closing sockets) { return; } SessionCurrent objSession = new SessionCurrent() { ObjSocket = socket }; clientSockets.Add(objSession); byte[] b = new byte[socket.ReceiveBufferSize]; string nombre = Encoding.ASCII.GetString(b); Array.Copy(buffer, b, BUFFER_SIZE); socket.BeginReceive(buffer, 0, BUFFER_SIZE, SocketFlags.None, ReceiveCallback, socket); Console.WriteLine($"{nombre} connected, waiting for request..."); serverSocket.BeginAccept(AcceptCallback, null); SendRooms(socket); }
private static SessionCurrent GetSessionByNickName(string nickName) { SessionCurrent currentSession = null; foreach (var item in clientSockets) { if (item.NickName == nickName) { currentSession = item; break; } } return(currentSession); }
private static SessionCurrent CurrentSession(Socket current, MessageTransport messageTransport) { SessionCurrent currentSession = null; foreach (var item in clientSockets) { if (current.Equals(item.ObjSocket)) { item.NickName = messageTransport.NickName; item.Room = messageTransport.Room; currentSession = item; break; } } return(currentSession); }
private static void SendInfoClient(SessionCurrent current, byte[] data) { try { if (current.ObjSocket.Connected) { current.ObjSocket.Send(data); } else { clientSockets.Remove(current); } } catch (Exception ex) { clientSockets.Remove(current); } }