Beispiel #1
0
        // 对发送给服务端的数据进行打包
        public void rpcCall(string funcname, string parameters = null, RpcCallBackDelegate callback = null)
        {
            // generate protobuf msg
            var tmp = new NinjaMessage.Message
            {
                OpCode = NinjaMessage.OPCODE.RpcCall,
                Uid    = int.Parse(netProtocol.Node.uid)
            };

            tmp.Request         = new NinjaMessage.Request();
            tmp.Request.RpcFunc = funcname;

            // use msgpack to pack the parameters
            if (parameters != null)
            {
                string paramString = parameters;
                var    serializer  = MessagePackSerializer.Get <string>();
                using (var byteStream = new MemoryStream())
                {
                    serializer.Pack(byteStream, paramString);
                    byte[] tmpBytes = new byte[byteStream.Length];
                    int    iter     = 0;
                    byteStream.Seek(0, SeekOrigin.Begin);
                    StringBuilder myStringBuilder = new StringBuilder();
                    while (iter < tmpBytes.Length)
                    {
                        byte numIter  = (byte)(byteStream.ReadByte());
                        char tempChar = '\u0000';
                        tempChar += (char)numIter;
                        myStringBuilder.Append(tempChar);
                        tmpBytes[iter++] = numIter;
                    }
                    tmp.Request.RpcParams = Convert.ToBase64String(tmpBytes);
                }
            }

            // use protobuf to encode message
            byte[] protoMsg;
            using (var byteStream = new MemoryStream())
            {
                tmp.WriteTo(byteStream);
                protoMsg = new byte[byteStream.Length];
                int iter = 0;
                byteStream.Seek(0, SeekOrigin.Begin);
                while (iter < protoMsg.Length)
                {
                    protoMsg[iter++] = (byte)(byteStream.ReadByte());
                }
            }

            // DebugLogger.Debug(tmp.Request.RpcParams);
            SendMessage(protoMsg, "RPC_CALL", callback);
        }
Beispiel #2
0
        internal void LoginRpcCallback(byte[] data)
        {
            var msg = ProtobufDecoder(data);

            if (msg.Response.RpcRsp != null)
            {
                var needCreate = MessagePackDecoder <long>(msg.Response.RpcRsp);
                DebugLogger.Debug(needCreate.ToString());
                if (needCreate == 1)
                {
                    RpcCallBackDelegate createPlayerCallback = new RpcCallBackDelegate(userCreateCallback);
                    rpcCall("user.create", "player_" + netProtocol.Node.uid, createPlayerCallback);
                }
            }
        }
Beispiel #3
0
        // 向服务器发信并注册回调函数
        public void SendMessage(byte[] message, string opCode, RpcCallBackDelegate callback)
        {
            uint session = 0;

            if (callback != null)
            {
                session = NetUtil.GetSessionID();

                lock (sessionToCallback)
                {
                    sessionToCallback[session] = new DataPakage {
                        CallBackFunc = callback, Session = session, cTick = netTick
                    };
                    netProtocol.WaitRecive++;
                }
            }
            var data = DataPack.Pack(session, opCode, message);

            sendQueue.Enqueue(data);
            sendEvent.Set();
        }
Beispiel #4
0
        public bool Login(string ip, int port, string secret)
        {
            // 连接服务器
            if (ChkException(usrLogin(ip, port, secret, out netProtocol)))
            {
                OnError("ConnectLogin");
                rpcCall("user.get_login_info", null, null);
                return(false);
            }
            else
            {
                // 登陆成功
                DebugLogger.Debug("Login success");
            }
            RpcCallBackDelegate callback = new RpcCallBackDelegate(LoginRpcCallback);

            rpcCall("user.get_login_info", null, callback);

            // 启动发送和接收
            StartNetWorkService();
            return(true);
        }
Beispiel #5
0
        void SerializeRpc()
        {
            RpcCallBackDelegate callback = new RpcCallBackDelegate(LoginRequist.ucl.SerializeRpcCallback);

            LoginRequist.ucl.rpcCall("user.serialize", null, callback);
        }