예제 #1
0
		protected internal bool connect()
		{
			bool result = false;
			bool startReader = false;

			lock (lockSocket)
			{
				if (mrSocket == null)
				{
#if LOGGER
					logger.Info("Connecting to {0}:{1}", hostname, port);
#endif
					try
					{
						mrSocket = new SocketConnection(hostname, port, socketEncoding);
						result = mrSocket.IsConnected;
					}
#if LOGGER
					catch (Exception ex)
					{
						logger.Info("Connect - Exception  : {0}", ex.Message);
#else
					catch
					{
#endif
						result = false;
					}
					if (result)
					{
						if (this.mrReader == null)
						{
							mrReader = new ManagerReader(this);
							mrReaderThread = new Util.ThreadClass(new ThreadStart(this.mrReader.Run), "ManagerReader-" + DateTime.Now.Second);
							mrReader.Socket = mrSocket;
							startReader = true;
						}
						else
							mrReader.Socket = mrSocket;
						mrReader.Reinitialize();
					}
					else
						mrSocket = null;
				}
			}

			if (startReader)
				mrReaderThread.Start();

			return IsConnected();
		}
예제 #2
0
		/// <summary> Closes the socket connection.</summary>
		private void disconnect(bool withDie)
		{
			lock (lockSocket)
			{
				if (withDie)
				{
					reconnectEnable = false;
					reconnected = false;
					enableEvents = true;
				}

				if (mrReader != null)
				{
					if (withDie)
					{
						mrReader.Die = true;
						mrReader = null;
					}
					else
						mrReader.Socket = null;
				}

				if (this.mrSocket != null)
				{
					mrSocket.Close();
					mrSocket = null;
				}

				responseEventHandlers.Clear();
				responseHandlers.Clear();
				pingHandlers.Clear();
			}
		}
예제 #3
0
        /// <summary>
        /// Async Read callback
        /// </summary>
        /// <param name="ar">IAsyncResult</param>
        private void mrReaderCallbback(IAsyncResult ar)
        {
            // mreader = Mr.Reader
            ManagerReader mrReader = (ManagerReader)ar.AsyncState;

            if (mrReader.die)
            {
                return;
            }

            SocketConnection mrSocket = mrReader.mrSocket;

            if (mrSocket == null || mrSocket.TcpClient == null)
            {
                // No socket - it's DISCONNECT !!!
                disconnect = true;
                return;
            }

            NetworkStream nstream = mrSocket.NetworkStream;

            if (nstream == null)
            {
                // No network stream - it's DISCONNECT !!!
                disconnect = true;
                return;
            }

            try
            {
                int count = nstream.EndRead(ar);
                if (count == 0)
                {
                    // No received data - it's may be DISCONNECT !!!
                    if (!is_logoff)
                    {
                        disconnect = true;
                    }
                    return;
                }
                string line = mrSocket.Encoding.GetString(mrReader.lineBytes, 0, count);
                mrReader.lineBuffer += line;
                int idx;
                // \n - because not all dev in Digium use \r\n
                // .Trim() kill \r
                lock (((ICollection)lineQueue).SyncRoot)
                    while (!string.IsNullOrEmpty(mrReader.lineBuffer) && (idx = mrReader.lineBuffer.IndexOf("\n")) >= 0)
                    {
                        line = idx > 0 ? mrReader.lineBuffer.Substring(0, idx).Trim() : string.Empty;
                        mrReader.lineBuffer = (idx + 1 < mrReader.lineBuffer.Length ? mrReader.lineBuffer.Substring(idx + 1) : string.Empty);
                        lineQueue.Enqueue(line);
                    }
                // Give a next portion !!!
                nstream.BeginRead(mrReader.lineBytes, 0, mrReader.lineBytes.Length, mrReaderCallbback, mrReader);
            }
#if LOGGER
            catch (Exception ex)
            {
                mrReader.logger.Error("Read data error", ex.Message);
#else
            catch
            {
#endif
                // Any catch - disconncatch !
                disconnect = true;
                if (mrReader.mrSocket != null)
                {
                    mrReader.mrSocket.Close();
                }
                mrReader.mrSocket = null;
            }
        }