/// <summary> /// Implements <see cref="System.IDisposable.Dispose"/> method of /// <see cref="System.IDisposable"/> interface. /// </summary> /// <remarks> /// <para>Stops threads, closes socket, etc.</para> /// <seealso cref="System.IDisposable"/> ///</remarks> public void Dispose(bool disposing) { if (isDisposed) { return; } lock (this) { isDisposed = true; GC.SuppressFinalize(this); if (disposing) { if (this.ConnectionChecker != null) { this.ConnectionChecker.Dispose(); } // Command to Logoff from SP. AltiLinkPlus.ALPCommand ac = new AltiLinkPlus.ALPCommand(0, Convert.ToInt32(ALPCmdID.LOGOFF)); this.SendALPCommand(ac); this.spStatus = SPStatus.DISCONNECTED; System.Threading.Thread.Sleep(300); // Setting flags for threads to finish the work. this.inThreadLivingStatus = false; this.outThreadLivingStatus = false; this.outALPThreadLivingStatus = false; this.commandsALPQueue.Enqueue(null); this.commandsQueue.Enqueue(null); this.eventsQueue.Enqueue(null); this.tcpc.Close(); } } }
/// <summary> /// Initializes main SP components. /// </summary> public AltiGenSPCore() { // Creating Status object; this.spStatus = SPStatus.DISCONNECTED; // Creating new TCPClient to communicate using AltiLink Plus. this.tcpc = new TcpClient(); // Creating 3 queues for commands, events & responses. this.commandsQueue = new WaitingQueue <Diacom.Cmd.CmdBase>(); this.commandsALPQueue = new WaitingQueue <AltiLinkPlus.ALPCommand>(); this.eventsQueue = new WaitingQueue <Diacom.Ev.EvBase>(); this.waitingCommands = new Hashtable(); // Thread to get messages from SP. this.inThreadHandle = new Thread(new ThreadStart(inThread)); this.inThreadHandle.IsBackground = inThreadLivingStatus = true; this.inThreadHandle.Name = "AltiGenSP: IN"; // Thread to send messages to SP. this.outThreadHandle = new Thread(new ThreadStart(outThread)); this.outThreadHandle.IsBackground = outThreadLivingStatus = true; this.outThreadHandle.Name = "AltiGenSP: OUT"; // Thread to send ALP messages. this.outALPThreadHandle = new Thread(new ThreadStart(outALPThread)); this.outALPThreadHandle.IsBackground = outALPThreadLivingStatus = true; this.outALPThreadHandle.Name = "AltiGenSP: ALP OUT"; }
/// <summary> /// Raises event with information about IAltiGenSP status. /// </summary> protected void RaiseEvent(SPStatus _status, string _info) { SPStatusEventArgs e = new SPStatusEventArgs(_status, _info); spStatus = _status; SPStatusEventHandler eh = this.SPStatusEvent; if (eh != null) { eh(this, e); } }
/// <summary> /// Raises event with specified arguments if <see cref="ISP"/> status is not OK. /// </summary> /// <param name="status">Service Provider status.</param> /// <param name="info">Additional information.</param> public SPStatusEventArgs(SPStatus status, string info) { this.Status = status; this.Info = info; }
/// <summary> /// Connects to server with given parameters. /// </summary> /// <param name="serverIP">IP address of server.</param> /// <param name="serverPort">Port to connect.</param> /// <param name="logonType">Type of logon.</param> /// <param name="account">Account.</param> /// <param name="password">Password.</param> /// <param name="timeout">Timeout interval.</param> /// <remarks> /// <para>Creates new TCP socket, connects to server with given IP address and port, /// tries to logon as "logonType" to "account" with given "password".</para> /// <para>Blocks the current thread until initialization is finished. /// Waits for connection "timeout" miliseconds.</para> /// </remarks> public void Connect(string serverIP, int serverPort, SPLogonType logonType, string account, string password, int timeout) { try { // Connect to the AltiWare server. this.tcpc.Connect(serverIP, serverPort); this.ConnectionIsAlive = true; // Assign network stream. this.st = tcpc.GetStream(); // To Binaryreader. this.bw = new BinaryWriter(st); // To BinaryWriter. this.br = new BinaryReader(st); this.lines = new Hashtable(); // Starting threads. this.outALPThreadHandle.Start(); this.outThreadHandle.Start(); this.inThreadHandle.Start(); if (timeout == 0) { timeout = System.Threading.Timeout.Infinite; } else if (timeout < 1000) { timeout *= 1000; } // Command to Register Application. AltiLinkPlus.ALPCommand ac = new AltiLinkPlus.ALPCommand(0, Convert.ToInt32(ALPCmdID.REGISTER_APPID)); // AltiGen SDK application. ac[0] = new AltiLinkPlus.ALPParameter("GATORS11"); this.SendALPCommandAndWait(ac, timeout); // Command to Logon ac = new AltiLinkPlus.ALPCommand(0, Convert.ToInt32(ALPCmdID.LOGON)); ac[0] = new AltiLinkPlus.ALPParameter((int)logonType); ac[1] = new AltiLinkPlus.ALPParameter(account); ac[2] = new AltiLinkPlus.ALPParameter(password); ac[3] = new AltiLinkPlus.ALPParameter(0); ac[4] = new AltiLinkPlus.ALPParameter(0); ac[5] = new AltiLinkPlus.ALPParameter(0); ac[6] = new AltiLinkPlus.ALPParameter(0); this.SendALPCommandAndWait(ac, timeout); // Get version this.SendALPCommandAndWait(new AltiLinkPlus.ALPCommand(0, Convert.ToInt32(ALPCmdID.GET_VERSION)), timeout); // Issue command to Get Lines Information. this.SendALPCommandAndWait(new Diacom.AltiGen.AltiLinkPlus.ALPCommand(0, Convert.ToInt32(ALPCmdID.GET_LINEINFO)), timeout); // Have a bad response on REGISTER_APPID, LOGON or GET_LINES command. if ((lines == null) || (ControlLocationID == 0)) { TraceOut.Put("Logon unsuccessful"); RaiseEvent(SPStatus.ERROR_LOGON, "Logon Error"); return; } this.ConnectionChecker = new System.Threading.Timer(new System.Threading.TimerCallback(this.CheckConnection), null, 30000, 30000); spStatus = SPStatus.OK; RaiseEvent(SPStatus.OK, "Logon Success"); } catch (Exception _x) { TraceOut.Put("Connect Exception"); TraceOut.Put("_x.Message"); RaiseEvent(SPStatus.ERROR_LOGON, _x.Message); } }
/// <summary> /// Raises event with specified arguments if <see cref="ISP"/> status is not OK. /// </summary> /// <param name="status">Service Provider status.</param> /// <param name="info">Additional information.</param> public SPStatusEventArgs(SPStatus status, string info) { this.Status = status; this.Info = info; this.Time = DateTime.Now; }