/// <summary> /// Entry Point /// </summary> /// <param name="userId"></param> /// <param name="serverIp"></param> /// <param name="port"></param> public UserSocket(int userId, string description, string serverIp, int port, int connectionId) { try { Model = new ConnectionModel(); GlobalObjects.UserSockets.StopMonitoringAll(userId); Model.Monitoring = false; Model.Server = serverIp; Model.Port = port; Model.UserId = userId; Model.Description = description; Model.ConnectionId = connectionId; Socket = new AsyncSocket(); Socket.CouldNotConnect += Socket_CouldNotConnect; Socket.SocketConnected += Socket_SocketConnected; Socket.SocketDataArrival += Socket_SocketDataArrival; Socket.SocketDisconnected += Socket_SocketDisconnected; Socket.SocketError += Socket_SocketError; ConnectionsHelper.Update(Model); } catch (Exception ex) { if (ProcessError != null) { ProcessError(ex, "UserSocket"); } } }
/// <summary> /// Socket Error /// </summary> /// <param name="_ex"></param> private void Socket_SocketError(Exception _ex) { try { var b = false; if (_ex.Message.Contains("An established connection was aborted by the software in your host machine")) { b = true; } else if (_ex.Message.Contains("An existing connection was forcibly closed by the remote host")) { b = true; } if (b) { SocketDisconnected(); } else { Model.Connected = false; Socket = new AsyncSocket(); Socket.CouldNotConnect += Socket_CouldNotConnect; Socket.SocketConnected += Socket_SocketConnected; Socket.SocketDataArrival += Socket_SocketDataArrival; Socket.SocketDisconnected += Socket_SocketDisconnected; Socket.SocketError += Socket_SocketError; ConnectionsHelper.Update(Model); } } catch (Exception ex) { if (ProcessError != null) { ProcessError(ex, "Socket_SocketError"); } } }
public TreeViewModel( Func <NodeViewModel> nodeFactory, ConnectionsHelper connectionsHelper) { _nodeFactory = nodeFactory; _connectionsHelper = connectionsHelper; }
/// <summary> /// Stop Monitoring /// </summary> /// <param name="userId"></param> public void StopMonitoringAll(int userId) { try { foreach (var item in _userSockets.Where(u => u.Model.UserId == userId)) { item.Model.Monitoring = false; ConnectionsHelper.Update(item.Model); } } catch (Exception ex) { if (ProcessError != null) { ProcessError(ex, "StopMonitoringAll"); } } }
/// <summary> /// Disconnect /// </summary> public void Disconnect() { try { Socket.Close(); Socket = new AsyncSocket(); Socket.CouldNotConnect += Socket_CouldNotConnect; Socket.SocketConnected += Socket_SocketConnected; Socket.SocketDataArrival += Socket_SocketDataArrival; Socket.SocketDisconnected += Socket_SocketDisconnected; Socket.SocketError += Socket_SocketError; ConnectionsHelper.Update(Model); } catch (Exception ex) { if (ProcessError != null) { ProcessError(ex, "Disconnect"); } } }
/// <summary> /// Monitor Connection /// </summary> /// <param name="connectionId"></param> public bool Monitor(int connectionId, int sourceUserId) { try { if (_userSockets.Where(s => s.Model.ConnectionId == connectionId && !s.Model.Monitoring && s.Model.UserId == sourceUserId).Count() != 0) { var socket = _userSockets.Where(s => s.Model.ConnectionId == connectionId && !s.Model.Monitoring && s.Model.UserId == sourceUserId).FirstOrDefault(); socket.Model.Monitoring = true; ConnectionsHelper.Update(socket.Model); return(true); } return(false); } catch (Exception ex) { if (ProcessError != null) { ProcessError(ex, "Monitor"); } return(false); } }
/// <summary> /// Could not connect /// </summary> /// <param name="SocketID"></param> private void Socket_CouldNotConnect(string SocketID) { try { if (Model.Monitoring) { GlobalObjects.Users.SendSocket(Model.UserId, Model.Server + ":Socket_CouldNotConnect"); } Socket = new AsyncSocket(); Socket.CouldNotConnect += Socket_CouldNotConnect; Socket.SocketConnected += Socket_SocketConnected; Socket.SocketDataArrival += Socket_SocketDataArrival; Socket.SocketDisconnected += Socket_SocketDisconnected; Socket.SocketError += Socket_SocketError; Model.Connected = false; ConnectionsHelper.Update(Model); } catch (Exception ex) { if (ProcessError != null) { ProcessError(ex, "Socket_CouldNotConnect"); } } }
/// <summary> /// Socket Connected /// </summary> /// <param name="SocketID"></param> private void Socket_SocketConnected(string SocketID) { try { Model.Connected = true; ConnectionsHelper.Update(Model); //if (Model.ServiceType == Enums.ServiceType.IrcClient) { var user = UsersHelper.Get(Model.UserId); Socket.Send("NICK " + user.Nickname + Environment.NewLine); System.Threading.Thread.Sleep(200); Socket.Send("USER " + user.IrcUser + " " + user.IrcHostName + " " + user.IrcServerName + " :" + user.IrcRealName + Environment.NewLine); //} if (Model.Monitoring) { GlobalObjects.Users.SendSocket(Model.UserId, Model.Server + ":Socket_SocketConnected"); } } catch (Exception ex) { if (ProcessError != null) { ProcessError(ex, "Socket_SocketConnected"); } } }
/// <summary> /// Create User Socket /// </summary> /// <param name="userId"></param> /// <returns></returns> public int Create(string description, int userId, string serverIp, int port, int serviceTypeId) { try { var connectionId = ConnectionsHelper.Create(new Models.ConnectionModel() { Connected = false, Description = description, Monitoring = false, Port = port, Server = serverIp, UserId = userId, ServiceTypeId = serviceTypeId }); var s = new UserSocket(userId, description, serverIp, port, connectionId); _userSockets.Add(s); return(s.Model.ConnectionId); } catch (Exception ex) { if (ProcessError != null) { ProcessError(ex, "Create"); } return(0); } }