Inheritance: RpcConnectionSocket
コード例 #1
0
ファイル: conn_asyncsock.cs プロジェクト: bowen-xu/TCE
        protected bool sendBufferredMsg()
        {
            if (_unsent_msglist.Count == 0)
            {
                return(true);
            }
            RpcMessage m = _unsent_msglist[0];

            _unsent_msglist.RemoveAt(0);

            if (_sent_num == 0)
            { //第一次连接进入之后的第一个数据包需要携带令牌和设备标识码,用于接入服务器的验证
                if (_token != null && !_token.Equals(""))
                {
                    m.extra.setPropertyValue("__token__", _token);
                    m.extra.setPropertyValue("__device_id__", RpcCommunicator.getSystemDeviceID());
                }
            }

            byte[] bytes = createMsgBody(m).ToArray();

            _sock.BeginSend(bytes, 0, bytes.Length, SocketFlags.None, delegate(IAsyncResult ar) {
                try {
                    RpcConnectionAsyncSocket s = (RpcConnectionAsyncSocket)ar.AsyncState;
                    s.sendBufferredMsg();
                }
                catch (Exception e) {
                    RpcCommunicator.instance().logger.error("BeginSend failed:" + e.ToString());
                }
            }, this);

            _sent_num++;
            return(true);
        }
コード例 #2
0
ファイル: conn_asyncsock.cs プロジェクト: bowen-xu/TCE
        protected override bool connect()
        {
            IPAddress  addr = IPAddress.Parse(_ep.host);
            IPEndPoint ep   = new IPEndPoint(addr, _ep.port);

            _status = ConnectStatus.CONNECTING;
            _sock   = newSocket();
            _sock.BeginConnect(ep, delegate(IAsyncResult ar) {
                RpcConnectionAsyncSocket s = (RpcConnectionAsyncSocket)ar.AsyncState;
                try {
                    s.handler.EndConnect(ar);
                    if (s.handler.Connected == true)
                    {
                        s.onConnected();
                        //s._thread = new Thread(run);
                        //s._thread.Start();  // launch one thread for data recieving .
                    }
                }
                catch (Exception e) {
                    // connect failed
                    //s.onDisconnected();
                    RpcCommunicator.instance().logger.error("connect to host failed!");
                }
                //connect failed, trigger event to user as Promise
                if (s.handler.Connected == false)
                {
                    foreach (RpcMessage m in _unsent_msglist)
                    {
                        RpcAsyncContext ctx = m.async.ctx;
                        ctx.exception       = new RpcException(RpcException.RPCERROR_CONNECT_FAILED);
                        m.async.promise.onError(ctx);
                    }
                    _unsent_msglist.Clear();
                }
                s._status = ConnectStatus.STOPPED;
            }, this);

            return(true);
        }