Exemplo n.º 1
0
        public virtual bool Connect(string remote_ip, int remote_port, int timeout = 10000)
        {
            try
            {
                this.ServerIp   = remote_ip;
                this.ServerPort = remote_port;

                EzWireProtocol protocol = new EzWireProtocol();
                protocol.DataReceived += protocol_DataReceived;//接收到数据,但不一定是message

                _client = ScsClientFactory.CreateClient(new ScsTcpEndPoint(remote_ip, remote_port));
                _client.WireProtocol     = protocol;
                _client.Connected       += client_Connected;
                _client.Disconnected    += client_Disconnected;
                _client.MessageReceived += client_MessageReceived;
                _client.ConnectTimeout   = timeout;
                _client.Connect();

                this._remain_life_number = this.DefaultLifeNumber;

                Helper.start_timer(ref _1s_timer, "1s timer", 1000, _1s_timer_Elapsed);
            }
            catch (Exception ex)
            {
                EzLogger.GlobalLogger.warning(string.Format("{0}{2}{1}{2}",
                                                            ex.Message, ex.StackTrace, Environment.NewLine));

                Dispose();

                return(false);
            }
            return(true);
        }
        public override bool Connect(string remote_ip, int remote_port, int timeout = 10000)
        {
            try
            {
                this.ServerIp = remote_ip;
                this.ServerPort = remote_port;

                EzWireProtocol protocol = new EzWireProtocol();
                protocol.DataReceived += protocol_DataReceived;//接收到数据,但不一定是message

                _client = ScsClientFactory.CreateClient(new ScsTcpEndPoint(remote_ip, remote_port));
                _client.WireProtocol = protocol;
                _client.ConnectTimeout = timeout;
                _client.Connected += client_Connected;
                _client.Disconnected += client_Disconnected;

                Messenger = new RequestReplyMessenger<IScsClient>(_client);
                Messenger.MessageReceived += messenger_MessageReceived;
                Messenger.Start();

                _client.Connect();

                this._remain_life_number = this.DefaultLifeNumber;

                Helper.start_timer(ref _1s_timer, "1s timer", 1000, _1s_timer_Elapsed);
            }
            catch (Exception ex)
            {
                EzLogger.GlobalLogger.warning(string.Format("{0}{2}{1}{2}", ex.Message, ex.StackTrace, Environment.NewLine));

                Dispose();

                return false;
            }
            return true;
        }