Пример #1
0
        public override void OnReceiveTimeout(string clientAddress, byte[] receiveData)
        {
            try
            {
                BufferReader reader = new BufferReader(receiveData);
                string paramString = reader.ReadPacketString();
                paramString = HttpUtility.UrlDecode(paramString, Encoding.UTF8);
                int index = paramString.IndexOf("?d=");
                if (index != -1)
                {
                    index += 3;
                    paramString = paramString.Substring(index, paramString.Length - index);
                }
                PacketMessage receivePacket = ParsePacketMessage(clientAddress, paramString, ConnectType.Tcp);
                var recHead = receivePacket.Head;

                int errorCode = LanguageHelper.GetLang().ErrorCode;
                string errorMsg = LanguageHelper.GetLang().RequestTimeout;
                MessageHead head = new MessageHead(recHead.MsgId, recHead.ActionId, "st", errorCode, errorMsg);
                head.HasGzip = true;
                MessageStructure ds = new MessageStructure();
                ds.WriteBuffer(head);
                byte[] data = ds.ReadBuffer();
                OnSendCompleted(clientAddress, data);
            }
            catch (Exception ex)
            {
                TraceLog.WriteError("Send to client {0} timeout error:{1}", clientAddress, ex);
            }
        }
Пример #2
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="package"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public byte[] ProcessRequest(object package, object param) {
            var httpresponse = new SocketGameResponse();
            ActionGetter actionGetter = null;
            try {
                httpresponse.WriteErrorCallback += GameEnvironment.Setting.ActionDispatcher.ResponseError;

                RequestPackage p = package as RequestPackage;
                actionGetter = param as ActionGetter;

                if (!string.IsNullOrEmpty(p.RouteName)) {
                    if (CheckRemote(p.RouteName, actionGetter)) {
                        MessageStructure response = new MessageStructure();
                        OnCallRemote(p.RouteName, actionGetter, response);
                        return response.PopBuffer();
                    }
                    httpresponse.WriteError(actionGetter, 10000, "No permission");
                } else {
                    DoAction(actionGetter, httpresponse);
                }
                return httpresponse.ReadByte();
            } catch (Exception ex) {
                TraceLog.WriteError("Request error:{0}", ex);
                MessageStructure response = new MessageStructure();
                response.WriteBuffer(new MessageHead(0, 10000, "request error"));
                return response.PopBuffer();
            }
        }
Пример #3
0
 public virtual void ResponseError(BaseGameResponse response, ActionGetter actionGetter, int errorCode, string errorInfo)
 {
     MessageHead head = new MessageHead(actionGetter.GetMsgId(), actionGetter.GetActionId(), errorCode, errorInfo);
     MessageStructure sb = new MessageStructure();
     sb.WriteBuffer(head);
     response.BinaryWrite(sb.PopBuffer());
 }
Пример #4
0
 public static byte[] CtorErrMsg(int error, string msg, NameValueCollection requestParam)
 {
     int msgId = Convert.ToInt32(requestParam["msgid"]);
     int actionid = Convert.ToInt32(requestParam["actionid"]);
     var ms = new MessageStructure();
     var head = new MessageHead(msgId, actionid, "st", error, msg);
     ms.WriteBuffer(head);
     return ms.PosGzipBuffer();
 }
Пример #5
0
        public void RequestTimeout(HttpConnection connection)
        {
            var param = connection.Param;
            int msgId = param.Get("MsgId").ToInt();
            int actionId = param.Get("ActionId").ToInt();
            int errorCode = LanguageHelper.GetLang().ErrorCode;
            string errorMsg = LanguageHelper.GetLang().RequestTimeout;
            var head = new MessageHead(msgId, actionId, "st", errorCode, errorMsg);
            head.HasGzip = true;
            var ms = new MessageStructure();
            ms.WriteBuffer(head);
            byte[] data = ms.ReadBuffer();

            string remoteAddress = connection.Context.Request.RemoteEndPoint.Address.ToString();
            string successMsg = string.Format("{0}>>发送超时到{1} {2}字节!",
                                              DateTime.Now.ToString("HH:mm:ss:ms"), remoteAddress, data.Length);
            Console.WriteLine(successMsg);
            OnResponseCompleted(connection, data);
        }
Пример #6
0
 private void OnCallRemote(string route, HttpGet httpGet, MessageStructure response)
 {
     try
     {
         string[] mapList = route.Split('.');
         string funcName = "";
         string routeName = "";
         if (mapList.Length > 1)
         {
             funcName = mapList[mapList.Length - 1];
             routeName = string.Join("/", mapList, 0, mapList.Length - 1);
         }
         string routeFile = "";
         string typeName = string.Format("Game.Script.Remote.{0}", routeName);
         int actionId = httpGet.ActionId;
         MessageHead head = new MessageHead(actionId);
         if (!ScriptEngines.DisablePython)
         {
             routeFile = string.Format("{0}/Remote/{1}.py", ScriptEngines.PythonDirName, routeName);
             dynamic scope = ScriptEngines.Execute(routeFile, typeName);
             if (scope != null)
             {
                 var funcHandle = scope.GetVariable<RemoteHandle>(funcName);
                 if (funcHandle != null)
                 {
                     funcHandle(httpGet, head, response);
                     response.WriteBuffer(head);
                     return;
                 }
             }
         }
         routeFile = string.Format("{0}/Remote/{1}.cs", ScriptEngines.CSharpDirName, routeName);
         var instance = (object)ScriptEngines.Execute(routeFile, typeName);
         if (instance != null)
         {
             var result = ObjectAccessor.Create(instance, true)[funcName];
         }
     }
     catch (Exception ex)
     {
         TraceLog.WriteError("{0}", ex);
     }
 }
Пример #7
0
 /// <summary>
 /// Call remote method
 /// </summary>
 /// <param name="routePath"></param>
 /// <param name="actionGetter"></param>
 /// <param name="response"></param>
 protected virtual void OnCallRemote(string routePath, ActionGetter actionGetter, MessageStructure response)
 {
     try
     {
         string[] mapList = routePath.Split('.');
         string funcName = "";
         string routeName = routePath;
         if (mapList.Length > 1)
         {
             funcName = mapList[mapList.Length - 1];
             routeName = string.Join("/", mapList, 0, mapList.Length - 1);
         }
         string routeFile = "";
         int actionId = actionGetter.GetActionId();
         MessageHead head = new MessageHead(actionId);
         if (!ScriptEngines.SettupInfo.DisablePython)
         {
             routeFile = string.Format("Remote.{0}", routeName);
             dynamic scope = ScriptEngines.ExecutePython(routeFile);
             if (scope != null)
             {
                 var funcHandle = scope.GetVariable<RemoteHandle>(funcName);
                 if (funcHandle != null)
                 {
                     funcHandle(actionGetter, head, response);
                     response.WriteBuffer(head);
                     return;
                 }
             }
         }
         string typeName = string.Format(GameEnvironment.Setting.RemoteTypeName, routeName);
         routeFile = string.Format("Remote.{0}", routeName);
         var args = new object[] { actionGetter, response };
         var instance = (object)ScriptEngines.Execute(routeFile, typeName, args);
         if (instance is RemoteStruct)
         {
             var target = instance as RemoteStruct;
             target.DoRemote();
         }
     }
     catch (Exception ex)
     {
         TraceLog.WriteError("OnCallRemote error:{0}", ex);
     }
 }
Пример #8
0
 /// <summary>
 /// Response hearbeat stream.
 /// </summary>
 /// <param name="package"></param>
 /// <param name="session"></param>
 protected void ResponseHearbeat(RequestPackage package, GameSession session)
 {
     try
     {
         MessageStructure response = new MessageStructure();
         response.WriteBuffer(new MessageHead(package.MsgId, package.ActionId, 0));
         var data = response.PopBuffer();
         if (session != null && data.Length > 0)
         {
             session.SendAsync(OpCode.Binary, data, 0, data.Length, OnSendCompleted).Wait();
         }
     }
     catch (Exception ex)
     {
         TraceLog.WriteError("Post Heartbeat error:{0}", ex);
     }
 }
Пример #9
0
 private void DoWriteError(MessageStructure ms, MessageHead head)
 {
     head.ErrorCode = (int)MessageError.SystemError;
     head.ClientVersion = 1;
     string msg = string.Format("Request action:{0} wcfserver error:{1}-{2},MsgId:{3}",
                                head.Action, head.ErrorCode, head.ErrorInfo, head.MsgId);
     TraceLog.WriteError(msg);
     #if DEBUG
     Console.WriteLine(msg);
     #endif
     ms.WriteBuffer(head);
 }
Пример #10
0
 protected byte[] OnCallRemoteComplated(ChannelContext context, string route, string param, string remoteAddress)
 {
     lock (context)
     {
         HttpGet httpGet = new HttpGet(param, remoteAddress);
         MessageStructure structure = new MessageStructure();
         MessageHead head = new MessageHead();
         OnCallRemote(route, httpGet, head, structure);
         structure.WriteBuffer(head);
         return structure.ReadBuffer();
     }
 }
Пример #11
0
        private static byte[] DoSerialize(params BaseEntity[] entityList)
        {
            var rootWriter = new MessageStructure();
            rootWriter.PushIntoStack(entityList.Length);
            object fieldValue = null;
            foreach (var entity in entityList)
            {
                var schema = entity.GetSchema();
                if (schema == null)
                {
                    continue;
                }
                var recordWriter = new MessageStructure();
                recordWriter.PushIntoStack(schema.EntityName);
                //write columns
                var columns = schema.GetColumns();
                foreach (var column in columns)
                {
                    fieldValue = entity.GetPropertyValue(column.Name);
                    if (EntitySchemaSet.IsSupportType(column.ColumnType))
                    {
                        recordWriter.PushIntoStack(column.ColumnType, fieldValue);
                    }
                    else if (column.HasChild)
                    {
                        PushChildStack(recordWriter, column, fieldValue);
                    }
                }

                rootWriter.PushIntoStack(recordWriter);
            }

            var head = new MessageHead(SyncActionId);
            rootWriter.WriteBuffer(head);
            return rootWriter.PopBuffer();
        }
Пример #12
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="response"></param>
 /// <param name="actionGetter"></param>
 /// <param name="errorCode"></param>
 /// <param name="errorInfo"></param>
 public virtual void ResponseError(BaseGameResponse response, ActionGetter actionGetter, int errorCode, string errorInfo)
 {
     string st = actionGetter.GetSt();
     ProtocolVersion prtcl = actionGetter.GetPtcl();
     MessageHead head = new MessageHead(actionGetter.GetMsgId(), actionGetter.GetActionId(), st, errorCode, errorInfo);
     MessageStructure sb = new MessageStructure();
     if (prtcl >= ProtocolVersion.ExtendHead)
     {
         sb.PushIntoStack(0); //不输出扩展头属性
     }
     sb.WriteBuffer(head);
     response.BinaryWrite(sb.PopBuffer());
 }
Пример #13
0
 /// <summary>
 /// 出错处理
 /// </summary>
 /// <param name="response"></param>
 /// <param name="actionID"></param>
 /// <param name="errorCode"></param>
 /// <param name="errorInfo"></param>
 public static void RequestError(IGameResponse response, int actionID, int errorCode, string errorInfo)
 {
     MessageHead head = new MessageHead(actionID, errorCode, errorInfo);
     MessageStructure sb = new MessageStructure();
     sb.WriteBuffer(head);
     response.BinaryWrite(sb.ReadBuffer());
 }