Beispiel #1
0
 public void dispatchMsg(RpcMessage m)
 {
     lock (_messages) {
         _messages.Add(m);
         _read_ev.Set();
     }
 }
Beispiel #2
0
 // static int count = 0;
 public void dispatchMsg(RpcMessage m)
 {
     if ((m.calltype & RpcMessage.CALL) != 0){
         if (_adapter != null){
             _adapter.dispatchMsg(m);
         }
     }
     if ((m.calltype & RpcMessage.RETURN) != 0){
         this.doReturnMsg(m);
     }
 }
Beispiel #3
0
 //消息进入communicator调度执行
 internal void dispatchMsg(RpcMessage m)
 {
     _dispatcher.dispatchMsg(m);
 }
Beispiel #4
0
 //处理在此adapter内产生的消息
 /*
 public void onConnectionMessage(RpcMessage m) {
     if (_dispatcher != null) {
         _dispatcher.dispatchMsg(m);
     }
 }
  * */
 void doError(int errcode, RpcMessage m)
 {
     RpcMessageReturn msgreturn = new RpcMessageReturn(m.sequence,errcode);
     msgreturn.send(m.conn);
     //		RpcConnection conn = m.conn;
     /*
     var sm:RpcMessageReturn = new RpcMessageReturn();
     sm.sequence = m.sequence;
     sm.errcode = errcode;
     conn.sendMessage(sm);
     */
 }
Beispiel #5
0
 public void dispatchMsg(RpcMessage m)
 {
     RpcServantDelegate dg = null;
     if ((m.calltype & RpcMessage.CALL) != 0) {
         lock (_servants) {
             if (!_servants.ContainsKey(m.ifidx)) {
                 doError( RpcException.RPCERROR_INTERFACE_NOTFOUND,m);
                 return;
             }
             dg = _servants[m.ifidx];
         }
         try {
             RpcMessage msgreturn = dg.invoke(m);
             if (msgreturn != null) {
                 m.conn.sendMessage(msgreturn);
             }
         }
         catch (Exception e) {
             RpcCommunicator.instance().logger.error(" execute servant failed:" + e.ToString());
             doError(RpcException.RPCERROR_REMOTEMETHOD_EXCEPTION,m);
         }
     }
 }
Beispiel #6
0
 protected override bool sendDetail(RpcMessage m)
 {
     _unsent_msglist.Add(m);
     if (!isConnected) {
         if (_status == ConnectStatus.STOPPED) {
             connect();
         }
         return true;
     }
     sendBufferredMsg();
     return true;
 }
Beispiel #7
0
        protected override bool sendDetail(RpcMessage m)
        {
            if ( !isConnected) {
                if (!connect()) {
                    return false;
                }
            }
            if (_sent_num == 0) { //第一次连接进入之后的第一个数据包需要携带令牌和设备标识码,用于接入服务器的验证
                if (_token != null && !_token.Equals("")) {
                    m.extra.setPropertyValue("__token__",_token);
                    m.extra.setPropertyValue("__device_id__", RpcCommunicator.getSystemDeviceID());
                }
            }

            //byte[] body = null;
            //body = ((MemoryStream) m.marshall()).ToArray();
            byte[] bytes = createMsgBody(m).ToArray();
            _sock.Send(bytes);
            _sent_num++;
            return true;
        }
Beispiel #8
0
 public virtual RpcMessage invoke(RpcMessage m)
 {
     return(null);
 }
Beispiel #9
0
 public virtual bool invoke(RpcMessage m)
 {
     return(true);
 }
Beispiel #10
0
        protected ReturnValue parsePacket(MemoryStream stream)
        {
            /***
             *  magic,packet_size,compress,encrypt,version,content
             *  packet_size: all fields size except magic.
             *
             */
            List <RpcMessage> msglist = new List <RpcMessage>();
            BinaryReader      reader  = new BinaryReader(stream);
            long size = stream.Length;

            while (size > 0)
            {
                if (size < META_PACKET_HDR_SIZE)
                {
                    return(new ReturnValue(ReturnValue.NEED_MORE, msglist, stream));
                }
                uint magic    = (uint)IPAddress.NetworkToHostOrder(reader.ReadInt32());
                uint pktsize  = (uint)IPAddress.NetworkToHostOrder(reader.ReadInt32());
                byte compress = reader.ReadByte();
                byte encrypt  = reader.ReadByte();
                uint version  = (uint)IPAddress.NetworkToHostOrder(reader.ReadInt32());
                if (magic != PACKET_META_MAGIC)
                {
                    return(new ReturnValue(ReturnValue.DATA_DIRTY, msglist, stream));
                }
                if (pktsize > MAX_PACKET_SIZE)
                {
                    return(new ReturnValue(ReturnValue.DATA_DIRTY, msglist, stream));
                }
                if (size <= META_PACKET_HDR_SIZE - 4)
                {
                    return(new ReturnValue(ReturnValue.DATA_DIRTY, msglist, stream));
                }
                if (size < pktsize + 4)
                {
                    return(new ReturnValue(ReturnValue.NEED_MORE, msglist, stream));
                }
                size -= META_PACKET_HDR_SIZE;
                uint   content_size = pktsize - (META_PACKET_HDR_SIZE - 4);
                Stream s            = new MemoryStream(reader.ReadBytes((int)content_size));
                size -= content_size;

                //decompress stream
                if (compress == RpcConstValue.COMPRESS_ZLIB)
                {
                    s = new DeflateStream(s, CompressionMode.Decompress);
                }
                else if (compress == RpcConstValue.COMPRESS_BZIP2)
                {
                    // nothing.
                }

                RpcMessage m = RpcMessage.unmarshall(s);
                if (m == null)
                {
                    return(new ReturnValue(ReturnValue.DATA_DIRTY, msglist, stream));
                }
                m.conn = this;
                msglist.Add(m);
            }
            return(new ReturnValue(ReturnValue.SUCC, msglist, stream));
        }
Beispiel #11
0
 public virtual bool sendMessage(RpcMessage m)
 {
     if( (m.calltype&RpcMessage.CALL)!=0 && (m.calltype&RpcMessage.ONEWAY) == 0 ){
         m.conn = this;
         //请求 Communicator 进行调度
         RpcCommunicator.instance().enqueueMessage(m.sequence, m);
     }
     bool r = false;
     lock (this) {
         r = sendDetail(m);
     }
     if (!r){ //发送失败,清除队列消息
         if ((m.calltype & RpcMessage.CALL) != 0 && (m.calltype & RpcMessage.ONEWAY) == 0){
             RpcCommunicator.instance().dequeueMessage(m.sequence);
         }
     }
     return r;
 }
Beispiel #12
0
 protected virtual bool sendDetail(RpcMessage m)
 {
     return false;
 }
Beispiel #13
0
 protected virtual void onMessage(RpcMessage m)
 {
     if ( _adapter == null &&_acceptor != null && _acceptor.adapter!=null) { // the connection from acceptor
         _adapter = _acceptor.adapter;
     }
     if (_adapter != null &&  _adapter.dispatcher!=null) { //由adapter的线程执行
         _adapter.dispatcher.dispatchMsg(m);
     }
     else { //由全局通信器进行调度执行
         RpcCommunicator.instance().dispatchMsg(m);
     }
 }
Beispiel #14
0
        protected virtual void doReturnMsg(RpcMessage m2)
        {
            RpcMessage m1 = null;
               // count+=1;

            m1 = RpcCommunicator.instance().dequeueMessage(m2.sequence);

            if(m1!=null){
                if(m1.async !=null){
                    // it will raise exception in user callback function
                    try {
                        if (m2.errcode != RpcException.RPCERROR_SUCC) {
                            // remote exception
                            //m1.async.ctx.exception = new RpcException(m2.errcode);
                            if (m1.async.promise != null) {
                                RpcAsyncContext ctx = new RpcAsyncContext();
                                ctx.promise = m1.async.promise;
                                ctx.exception = new RpcException(m2.errcode);
                                m1.async.promise.onError( ctx );
                            }
                            else {
                                m1.async.onError(m2.errcode);
                            }
                        }
                        else {
                            m1.async.callReturn(m1, m2);
                        }
                    }
                    catch (Exception e) {
                        RpcCommunicator.instance().logger.error("User CallBack failed: m1.async.callReturn ." + e.ToString());
                    }
                }else{
                    lock(m1){
                        m1.result = m2; // assing to init-caller
                        //m1.notify();
                        m1.ev.Set();
                    }
                }
            }
        }
Beispiel #15
0
 public virtual RpcMessage invoke(RpcMessage m) {
     return null;
 }
Beispiel #16
0
 /**
  * 将接收到的Rpc消息推入处理队列,等待线程读取
  *
  */
 internal RpcCommunicator enqueueMessage(int sequence, RpcMessage m)
 {
     lock (_cachedMsgList) {
         m.issuetime = Utility.unixTimestamp(DateTime.Now);
         _cachedMsgList.Add(sequence,m);
     }
     return this;
 }
Beispiel #17
0
 //消息进入communicator调度执行
 internal void dispatchMsg(RpcMessage m)
 {
     _dispatcher.dispatchMsg(m);
 }
Beispiel #18
0
 public virtual void callReturn(RpcMessage m1, RpcMessage m2)
 {
 }
Beispiel #19
0
 protected virtual bool sendDetail(RpcMessage m)
 {
     return(false);
 }
Beispiel #20
0
 //从二进制流中反序列化出对象RpcMessage
 public static RpcMessage unmarshall(Stream stream)
 {
     RpcMessage m = new RpcMessage();
     BinaryReader reader = new BinaryReader(stream);
     try {
         m.type = RpcBinarySerializer.readByte(reader);
         m.sequence = RpcBinarySerializer.readInt(reader);
         m.calltype = RpcBinarySerializer.readByte(reader);
         m.ifidx = RpcBinarySerializer.readShort(reader);
         m.opidx = RpcBinarySerializer.readShort(reader);
         m.errcode = RpcBinarySerializer.readInt(reader);
         m.paramsize = RpcBinarySerializer.readByte(reader);
         m.call_id = RpcBinarySerializer.readShort(reader);
         if (m.extra.unmarshall(stream) == false) {
             return null;
         }
         m.paramstream = reader.ReadBytes( (int)(stream.Length - stream.Position) );
     }
     catch (Exception e) {
         RpcCommunicator.instance().logger.error(e.ToString());
         m = null;
     }
     return m;
 }
Beispiel #21
0
 public virtual void callReturn(RpcMessage m1, RpcMessage m2)
 {
 }
Beispiel #22
0
        protected MemoryStream createMsgBody(RpcMessage m)
        {
            //byte[] hdrbBytes = null;
            MemoryStream stream = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(stream);
            MemoryStream bodystream = (MemoryStream) m.marshall();
            byte[] bytes = bodystream.ToArray();
            unchecked
            {
                writer.Write((uint)IPAddress.HostToNetworkOrder((int)PACKET_META_MAGIC));
            }

            writer.Write((uint)IPAddress.HostToNetworkOrder(bytes.Length + META_PACKET_HDR_SIZE - 4));
            writer.Write((byte)RpcConstValue.COMPRESS_NONE);
            writer.Write((byte)RpcConstValue.ENCRYPT_NONE);
            writer.Write((uint)IPAddress.HostToNetworkOrder(VERSION));

            writer.Write(bytes);
            //hdrbBytes = stream.ToArray();
            //return hdrbBytes;
            return stream;
        }