コード例 #1
0
ファイル: SGIPClient.cs プロジェクト: yuyu2you/X_NET20
        /// <summary>登录。发送Bind指令,接收Bind_Resp响应</summary>
        /// <returns></returns>
        public Boolean Login()
        {
            WriteLog(String.Format("正在连接服务器…… {0}:{1}", IP, Port));
            var client = new TcpSession();

            Client = client;
            try
            {
                //client.Connect(IP, Port);
                //client.Connect(IP, Port);
                client.Remote.Host = IP;
                client.Remote.Port = Port;
            }
            catch (Exception ex)
            {
                String str = IP + ":" + Port.ToString();
                throw new NetException("连接网关服务器" + str + "出错,请确定网络是否畅通!" + ex.Message, ex);
            }

            var cmd = new SGIPBind();

            cmd.LoginName     = SystemID;
            cmd.LoginPassowrd = Password;
            cmd.LoginType     = LoginTypes.SpToSmg;

            WriteLog("正在登录……");

            var session = client as ISocketSession;

            session.Send(cmd.GetStream());
            var data = client.Receive();
            var resp = SGIPEntity.Read(new MemoryStream(data)) as SGIPResponse;

            if (resp == null)
            {
                throw new Exception("登录失败!服务器没有响应!");
            }
            if (resp.Result != SGIPErrorCodes.Success)
            {
                throw new Exception("登录失败!" + resp.Result.GetDescription());
            }

            //登录完成,开始读取指令
            client.Received += Client_Received;
            client.ReceiveAsync();

            Logined = true;

            WriteLog("登录成功!");

            return(true);
        }
コード例 #2
0
ファイル: SGIPClient.cs プロジェクト: yuyu2you/X_NET20
        /// <summary>读命令</summary>
        /// <returns></returns>
        private SGIPEntity Read()
        {
            if (Client == null || !Client.Socket.Connected)
            {
                throw new InvalidOperationException("没有连接到服务器!");
            }

            try
            {
                var cmd = SGIPEntity.Read(new MemoryStream(Client.Receive()));
                if (cmd == null)
                {
                    throw new Exception("获取命令失败!");
                }

                WriteLog("收包:" + cmd.ToString());
                return(cmd);
            }
            catch (IOException ex)
            {
                throw new InvalidOperationException("读命令失败!", ex);
            }
        }