/// <summary>
            /// Initializes a new instance of the <see cref="RequestHandler" /> class.
            /// </summary>
            /// <param name="handler">The underlying client connection handler.</param>
            /// <param name="record">The value for the <see cref="RequestHandler.BaseRecord" /> property.</param>
            /// <param name="level">The value for the <see cref="RequestHandler.Level" /> property.</param>
            /// <exception cref="ArgumentNullException">
            /// At least one parameter is <see langword="null" />.
            /// </exception>
            public RequestHandler(TcpClientConnectionHandler handler, BeginRequestRecord record, int level)
            {
                if (handler == null)
                {
                    throw new ArgumentNullException("handler");
                }

                if (record == null)
                {
                    throw new ArgumentNullException("record");
                }

                this.BaseRecord = record;
                this.Level = level;
                this.Handler = handler;

                this._CONTEXT = new RequestContext(this);
                this._CONTEXT.Address = this.Server._SETTINGS.LocalAddress ?? IPAddress.Loopback;
                this._CONTEXT.Port = this.Server._SETTINGS.Port;

                int? readBufferSize = null;
                int? writeBufferSize = null;
                this._CONTEXT.BodyStream = this._CONTEXT.CreateInputStream(ref readBufferSize, ref writeBufferSize);

                this._CONTEXT.ReadBufferSize = readBufferSize;
                if (this._CONTEXT.ReadBufferSize < 1)
                {
                    this._CONTEXT.ReadBufferSize = 10240;
                }

                this._CONTEXT.WriteBufferSize = writeBufferSize;
                if (this._CONTEXT.WriteBufferSize < 1)
                {
                    this._CONTEXT.WriteBufferSize = 10240;
                }

                this._CONTEXT.Ended += this._CONTEXT_Ended;
            }
Пример #2
0
        public TelegramClient(int apiId, string apiHash,
                              ISessionStore store = null, string sessionUserId = "session", TcpClientConnectionHandler handler = null)
        {
            if (apiId == default(int))
            {
                throw new MissingApiConfigurationException("API_ID");
            }
            if (string.IsNullOrEmpty(apiHash))
            {
                throw new MissingApiConfigurationException("API_HASH");
            }

            if (store == null)
            {
                store = new FileSessionStore();
            }

            this.apiHash = apiHash;
            this.apiId   = apiId;
            this.handler = handler;

            session   = Session.TryLoadOrCreateNew(store, sessionUserId);
            transport = new TcpTransport(session.DataCenter.Address, session.DataCenter.Port, this.handler);
        }
Пример #3
0
 public TLClient(int apiId, string apiHash, ISessionStore store = null, string sessionUserId = "session", TcpClientConnectionHandler handler = null) : base(apiId, apiHash, store, sessionUserId, handler)
 {
 }
Пример #4
0
        /// <summary>
        /// Creates a new TelegramClient
        /// </summary>
        /// <param name="apiId">The API ID provided by Telegram. Get one at https://my.telegram.org </param>
        /// <param name="apiHash">The API Hash provided by Telegram. Get one at https://my.telegram.org </param>
        /// <param name="store">An ISessionStore object that will handle the session</param>
        /// <param name="sessionUserId">The name of the session that tracks login info about this TelegramClient connection</param>
        /// <param name="handler">A delegate to invoke when a connection is needed and that will return a TcpClient that will be used to connect</param>
        /// <param name="dcIpVersion">Indicates the preferred IpAddress version to use to connect to a Telegram server</param>
        public TelegramClient(int apiId, string apiHash,
                              ISessionStore store             = null, string sessionUserId = "session", TcpClientConnectionHandler handler = null,
                              DataCenterIPVersion dcIpVersion = DataCenterIPVersion.Default)
        {
            if (apiId == default(int))
            {
                throw new MissingApiConfigurationException("API_ID");
            }
            if (string.IsNullOrEmpty(apiHash))
            {
                throw new MissingApiConfigurationException("API_HASH");
            }

            if (store == null)
            {
                store = new FileSessionStore();
            }
            this.store = store;

            this.apiHash     = apiHash;
            this.apiId       = apiId;
            this.handler     = handler;
            this.dcIpVersion = dcIpVersion;

            this.sessionUserId = sessionUserId;
        }
Пример #5
0
        /// <summary>
        /// Starts the communication with a remote client.
        /// </summary>
        /// <param name="client">The TCP client.</param>
        /// <param name="throwException">
        /// Throw exception (<see langword="true" />) or not (<see langword="false" />).
        /// </param>
        /// <returns>
        /// Operation was successful (<see langword="true" />) or not (<see langword="false" />).
        /// <see langword="null" /> indicates that <paramref name="client" /> is <see langword="null" />.
        /// </returns>
        /// <exception cref="ServerException">
        /// The raised exception.
        /// </exception>
        protected bool? StartCommunicationWithRemoteClient(RemoteClient client, bool throwException = true)
        {
            if (client == null)
            {
                return null;
            }

            try
            {
                var newHandler = new TcpClientConnectionHandler(this, client);

                Task.Factory
                    .StartNew(action: (state) =>
                                      {
                                          var handler = (TcpClientConnectionHandler)state;
                                          var server = handler.Server;

                                          try
                                          {
                                              server.RaiseEventHandler(server.Connected,
                                                                       new ClientEventArgs(handler.RemoteClient));

                                              using (handler)
                                              {
                                                  do
                                                  {
                                                      handler.HandleNext();
                                                  }
                                                  while (!handler.CloseConnection);
                                              }
                                          }
                                          catch (Exception ex)
                                          {
                                              server.RaiseError(ex, false);
                                          }
                                      },
                                      state: newHandler);

                return true;
            }
            catch (Exception ex)
            {
                this.RaiseError(ex, throwException);
                return false;
            }
        }
Пример #6
-1
        public TelegramClient(int apiId, string apiHash,
                              ISessionStore store = null, string sessionUserId = "session", TcpClientConnectionHandler handler = null)
        {
            if (apiId == default(int))
            {
                throw new MissingApiConfigurationException("API_ID");
            }
            if (string.IsNullOrEmpty(apiHash))
            {
                throw new MissingApiConfigurationException("API_HASH");
            }

            if (store == null)
            {
                store = new FileSessionStore();
            }

            TLContext.Init();
            _apiHash = apiHash;
            _apiId   = apiId;
            _handler = handler;

            _session   = Session.TryLoadOrCreateNew(store, sessionUserId);
            _transport = new TcpTransport(_session.ServerAddress, _session.Port, _handler);
        }
Пример #7
-1
        public TelegramClient(int apiId, string apiHash, ISessionStore store = null, string sessionUserId = "session", string AppVersion = "1.0", string DeviceModel = "PC", string LangCode = "en", string SystemVersion = "Windows", TcpClientConnectionHandler handler = null)
        {
            try
            {
                if (apiId == default(int))
                {
                    throw new MissingApiConfigurationException("API_ID");
                }
                if (string.IsNullOrEmpty(apiHash))
                {
                    throw new MissingApiConfigurationException("API_HASH");
                }

                if (store == null)
                {
                    store = new FileSessionStore();
                }

                TLContext.Init();
                _apiHash       = apiHash;
                _apiId         = apiId;
                _appVersion    = AppVersion;
                _DeviceModel   = DeviceModel;
                _LangCode      = LangCode;
                _SystemVersion = SystemVersion;
                _handler       = handler;
                _session       = Session.TryLoadOrCreateNew(store, sessionUserId);
                _transport     = new TcpTransport(_session.ServerAddress, _session.Port, _handler);
            }
            catch
            {
                throw new Exception("Not connected to the internet");
            }
        }