示例#1
0
 protected virtual void DisposeSelf()
 {
     if (this.ClientSocket != null)
     {
         CtkNetUtil.DisposeSocketTry(this.ClientSocket);
     }
 }
        public int ConnectTry(string ip = null, int?port = null)
        {
            lock (this)
            {
                if (this.TcpClient.IsNonStopRunning)
                {
                    return(0);
                }

                if (this.TcpClient.RemoteUri == null)
                {
                    this.TcpClient.RemoteUri = CtkNetUtil.ToUri(ip, 1000);
                }

                if (!string.IsNullOrEmpty(ip))
                {
                    this.RemoteIp = ip;
                }
                if (port.HasValue)
                {
                    this.RemotePort = port.Value;
                }

                if (!this.TcpClient.IsOpenRequesting && !this.TcpClient.IsRemoteConnected)
                {
                    this.TcpClient.ConnectTry();
                }
                return(0);
            }
        }
示例#3
0
        public int CfInit()
        {
            this.HsmsConnector = new CxHsmsConnector();
            //hsmsConnector.ctkConnSocket.isActively = true;

            var localUri  = string.IsNullOrEmpty(this.cfg.LocalUri) ? null : new Uri(this.cfg.LocalUri);
            var remoteUri = string.IsNullOrEmpty(this.cfg.RemoteUri) ? null : new Uri(this.cfg.RemoteUri);

            var localIp = CtkNetUtil.GetIpAdr1stLikelyOr127(localUri == null ? null : localUri.Host, remoteUri == null ? null : remoteUri.Host);

            if (localIp == null)
            {
                throw new Exception("無法取得在地IP");
            }
            this.HsmsConnector.LocalUri       = CtkNetUtil.ToUri(localIp.ToString(), localUri == null ? 0 : localUri.Port);
            this.HsmsConnector.EhReceiveData += delegate(Object sen, CxHsmsConnectorRcvDataEventArg ea)
            {
                var myMsg = ea.msg;


                //System.Diagnostics.Debug.WriteLine("S{0}F{1}", myMsg.header.StreamId, myMsg.header.FunctionId);
                //System.Diagnostics.Debug.WriteLine("SType= {0}", myMsg.header.SType);

                switch (myMsg.header.SType)
                {
                case 1:
                    HsmsConnector.Send(CxHsmsMessage.CtrlMsg_SelectRsp(0));
                    return;

                case 5:
                    HsmsConnector.Send(CxHsmsMessage.CtrlMsg_LinktestRsp());
                    return;
                }

                this.OnReceiveData(myMsg);
            };



            return(0);
        }
示例#4
0
        public int CfInit()
        {
            hsmsConnector = new CxHsmsConnector();
            //hsmsConnector.ctkConnSocket.isActively = true;

            var localUri  = new Uri(this.cfg.LocalUri);
            var remoteUri = new Uri(this.cfg.RemoteUri);

            var localIp = CtkNetUtil.GetIpAdr1stLikelyOr127(localUri.Host, remoteUri.Host);

            if (localIp == null)
            {
                throw new Exception("無法取得在地IP");
            }
            hsmsConnector.LocalUri       = CtkNetUtil.ToUri(localIp.ToString(), localUri.Port);
            hsmsConnector.EhReceiveData += delegate(Object sen, CxHsmsConnectorRcvDataEventArg ea)
            {
                var myMsg = ea.msg;

                switch (myMsg.header.SType)
                {
                case 1:
                    hsmsConnector.Send(CxHsmsMessage.CtrlMsg_SelectRsp(0));
                    return;

                case 5:
                    hsmsConnector.Send(CxHsmsMessage.CtrlMsg_LinktestRsp());
                    return;
                }

                this.OnReceiveData(myMsg);
            };



            return(0);
        }
示例#5
0
        public virtual int CfInit()
        {
            if (this.Config == null)
            {
                throw new SNetException("沒有設定參數");
            }

            var remoteUri   = new Uri(this.Config.RemoteUri);
            var localUri    = string.IsNullOrEmpty(this.Config.LocalUri) ? null : new Uri(this.Config.LocalUri);
            var localIpAddr = CtkNetUtil.GetSuitableIp(localUri == null ? null : localUri.Host, remoteUri.Host);

            localUri = CtkNetUtil.ToUri(localIpAddr.ToString(), localUri == null ? 0 : localUri.Port);


            switch (this.Config.ProtoConnect)
            {
            case SNetEnumProtoConnect.Tcp:
                this.ProtoConn = new SNetProtoConnTcp(localUri, remoteUri, this.Config.IsActivelyConnect);
                break;

            case SNetEnumProtoConnect.Custom:
                //由使用者自己實作
                break;

            default: throw new ArgumentException("ProtoConn");;
            }

            this.ProtoConn.IntervalTimeOfConnectCheck = this.Config.IntervalTimeOfConnectCheck;
            this.ProtoConn.EhFirstConnect            += (ss, ee) =>
            {
                this.ProtoSession.FirstConnect(this.ProtoConn);

                if (this.Config.IsActivelyTx)
                {
                    var ackDataMsg = this.SignalTran.CreateAckMsg(this.Config.SignalCfgList);
                    if (ackDataMsg != null)
                    {
                        this.ProtoConn.WriteMsg(ackDataMsg);
                    }
                }
                else
                {
                    var reqDataMsg = this.SignalTran.CreateDataReqMsg(this.Config.SignalCfgList);
                    if (reqDataMsg != null)
                    {
                        this.ProtoConn.WriteMsg(reqDataMsg);
                    }
                }
            };
            this.ProtoConn.EhDataReceive += (ss, ee) =>
            {
                var ea = ee as CtkProtocolEventArgs;
                this.ProtoFormat.ReceiveMsg(ea.TrxMessage);
                this.areMsg.Set();

                if (this.ProtoFormat.HasMessage())
                {
                    SignalHandle();
                }
            };



            switch (this.Config.ProtoFormat)
            {
            case SNetEnumProtoFormat.SNetCmd:
                this.ProtoFormat = new SNetProtoFormatSNetCmd();
                break;

            case SNetEnumProtoFormat.Secs:
                this.ProtoFormat = new SNetProtoFormatSecs();
                break;

            case SNetEnumProtoFormat.Custom:
                //由使用者自己實作
                break;

            default: throw new ArgumentException("必須指定ProtoFormat");
            }



            switch (this.Config.ProtoSession)
            {
            case SNetEnumProtoSession.SNetCmd:
                this.ProtoSession = new SNetProtoSessionSNetCmd();
                break;

            case SNetEnumProtoSession.Secs:
                this.ProtoSession = new SNetProtoSessionSecs();
                break;

            case SNetEnumProtoSession.Custom:
                //由使用者自己實作
                break;

            default: throw new ArgumentException("必須指定ProtoFormat");
            }


            switch (this.Config.SignalTran)
            {
            case SNetEnumSignalTrans.SNetCmd:
                this.SignalTran = new SNetSignalTransSNetCmd();
                break;

            case SNetEnumSignalTrans.Secs001:
                this.SignalTran = new SNetSignalTransSecs001();
                break;

            case SNetEnumSignalTrans.Custom:
                //由使用者自己實作
                break;

            default: throw new ArgumentException("必須指定ProtoFormat");
            }



            return(0);
        }