private void WriteRemote() { int msgId = paramGetter.GetMsgId(); int actionId = paramGetter.GetActionId(); var head = new MessageHead(msgId, actionId, ErrorCode, ErrorInfo); response.WriteBuffer(head); }
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); } }
public override void TakeActionAffter(bool state) { //ms.WriteByte(_message); MessageStructure ms = new MessageStructure(); //ms.PushIntoStack(); ms.PushIntoStack(true); ms.WriteBuffer(new MessageHead(2010)); byte[] buffer = ms.PopBuffer(); //Current.SendAsync(buffer, 0, buffer.Length); var list = GameSession.GetOnlineAll(10 * 1000); list.ForEach(r => { if (r.SessionId != Current.SessionId) { r.SendAsync(OpCode.Text, buffer, 0, buffer.Length, asyncResult => { Console.WriteLine("The results of data send:{0}", asyncResult.Result == ResultCode.Success ? "ok" : "fail"); }); } }); base.TakeActionAffter(state); }
/// <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) { MessageHead head = new MessageHead(actionGetter.GetMsgId(), actionGetter.GetActionId(), errorCode, errorInfo); MessageStructure sb = new MessageStructure(); sb.WriteBuffer(head); response.BinaryWrite(sb.PopBuffer()); }
/// <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.PopBuffer()); }
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, enableErrorMsg ? msg : ""); ms.WriteBuffer(head); return(ms.PosGzipBuffer()); }
public IByteBuffer TryEncodePackage(ResponsePackage package, byte[] data = null) { MessageHead head = new MessageHead(package.MsgId, package.ActionId, package.St, package.ErrorCode, package.ErrorInfo); MessageStructure sb = new MessageStructure(); sb.WriteBuffer(head); if (data != null && data.Length != 0) { sb.WriteByte(data); } return(Unpooled.WrappedBuffer(sb.PopBuffer())); }
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()); } }
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); }
/// <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()); }
public override void TakeActionAffter(bool state) { //ms.WriteByte(_message); var cUser = (GameUser)Current.User; MessageStructure ms = new MessageStructure(); //ms.PushIntoStack(); ms.PushIntoStack(true); ms.WriteBuffer(new MessageHead(2010)); byte[] buffer = ms.PopBuffer(); //Current.SendAsync(buffer, 0, buffer.Length); var rUser = GameSession.GetOnlineAll(10 * 1000).FirstOrDefault(r => r.UserId == cUser.RivalId); //list.ForEach(r => //{ // if (r.SessionId != Current.SessionId) // r.SendAsync(OpCode.Text, buffer, 0, buffer.Length, asyncResult => // { // Console.WriteLine("The results of data send:{0}", asyncResult.Result == ResultCode.Success ? "ok" : "fail"); // }); //}); if (rUser != null) { rUser.SendAsync(OpCode.Text, buffer, 0, buffer.Length, asyncResult => { Console.WriteLine("The results of data send:{0}", asyncResult.Result == ResultCode.Success ? "ok" : "fail"); }); } if (cUser != null) { cUser.ModifyLocked(() => { cUser.RoomId = string.Empty; cUser.Searching = false; cUser.RivalId = -1; }); } base.TakeActionAffter(state); }
private void DoWriteError(string errorInfo) { var head = new MessageHead { ErrorCode = (int)MessageError.SystemError, Action = ActionId }; if (EnableError) { head.ErrorInfo = errorInfo; } _buffer.WriteBuffer(head); WriteBuffer(_buffer.ReadBuffer()); }
/// <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); } }
/// <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(TNet.Runtime.GameZone.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); } }
protected override void OnDisconnected(GameSession session) { Console.WriteLine("客户端UserId:[{0}]已与服务器断开", session.UserId); var cUser = (GameUser)session.User; if (cUser != null) { MessageStructure ms = new MessageStructure(); //ms.PushIntoStack(); ms.PushIntoStack(true); ms.WriteBuffer(new MessageHead(2010)); byte[] buffer = ms.PopBuffer(); //Current.SendAsync(buffer, 0, buffer.Length); Console.WriteLine("客户端RivalId:[{0}]将会收到离开消息", cUser.RivalId); var rUser = GameSession.GetOnlineAll(10 * 1000).Find(r => r.UserId == cUser.RivalId); if (rUser != null) { rUser.SendAsync(OpCode.Text, buffer, 0, buffer.Length, asyncResult => { Console.WriteLine("The results of data send:{0}", asyncResult.Result == ResultCode.Success ? "ok" : "fail"); }); } else { Console.WriteLine("客户端RivalUser为空"); } } else { Console.WriteLine("客户端GameUser为空"); } base.OnDisconnected(session); }
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); } }
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()); }
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); }
/// <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 += TNet.Runtime.GameZone.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()); } }
public override void TakeActionAffter(bool state) { GameSession gameSession = null; GameSession.GetOnlineAll(10 * 1000).ForEach(r => { var user = (GameUser)r.User; if (r.UserId != Current.UserId && user.Searching) { gameSession = r; return; } }); if (gameSession != null) { MessageStructure ms = new MessageStructure(); //ms.PushIntoStack(); ms.PushIntoStack(true); ms.WriteBuffer(new MessageHead(2009)); byte[] buffer = ms.PopBuffer(); Current.SendAsync(OpCode.Text, buffer, 0, buffer.Length, asyncResult => { Console.WriteLine("The results of data send:{0}", asyncResult.Result == ResultCode.Success ? "ok" : "fail"); }); gameSession.SendAsync(OpCode.Text, buffer, 0, buffer.Length, asyncResult => { Console.WriteLine("The results of data send:{0}", asyncResult.Result == ResultCode.Success ? "ok" : "fail"); }); var cUser = (GameUser)Current.User; var vUser = (GameUser)gameSession.User; cUser.ModifyLocked(() => { cUser.Searching = false; cUser.RivalId = vUser.UserId; }); vUser.ModifyLocked(() => { vUser.Searching = false; vUser.RivalId = cUser.UserId; }); } //ms.WriteByte(_message); //var cache = new PersonalCacheStruct<GameUser>(); //var persons= cache.FindGlobal(r=>r.Searching==true);//.Where(r => r.Searching == true); //bool start = false; //if (persons.Count()>1) //{ // start = true; //} //MessageStructure ms = new MessageStructure(); ////ms.PushIntoStack(); //ms.PushIntoStack(start); //ms.WriteBuffer(new MessageHead(2009)); //byte[] buffer = ms.PopBuffer(); ////Current.SendAsync(buffer, 0, buffer.Length); //var list = GameSession.GetOnlineAll(10 * 1000); //if (start) //{ // list.ForEach(r => // { // //if (r.SessionId != Current.SessionId) // r.SendAsync(OpCode.Text, buffer, 0, buffer.Length, asyncResult => // { // Console.WriteLine("The results of data send:{0}", asyncResult.Result == ResultCode.Success ? "ok" : "fail"); // }); // }); // foreach (var p in persons) // { // p.ModifyLocked(() => // { // //_user.RoomId = Guid.NewGuid().ToString(); // _user.Searching = false; // }); // } //} base.TakeActionAffter(state); }