/// <summary> /// /// </summary> public override void Start(string[] args) { socketListener.StartListen(); if (EnableHttp) { httpListener.Start(); foreach (var prefix in httpListener.Prefixes) { TraceLog.WriteLine("{0} Http service:{1} is started.", DateTime.Now.ToString("HH:mm:ss"), prefix.TrimEnd('/')); } httpListener.BeginGetContext(OnHttpRequest, httpListener); } EntitySyncManger.SendHandle += (userId, data) => { GameSession session = GameSession.Get(userId); if (session != null) { var task = session.SendAsync(OpCode.Binary, data, 0, data.Length, result => { }); task.Wait(); return(task.Result); } return(false); }; TraceLog.WriteLine("{0} Socket service {1}:{2} is started.", DateTime.Now.ToString("HH:mm:ss"), _setting.GameIpAddress, _setting.GamePort); base.Start(args); }
public void ProcessPackage(RequestPackage package, GameSession session) { if (package == null) { return; } try { var watch = RunTimeWatch.StartNew(string.Format("当前线程{0},{1}协议执行时间", Thread.CurrentThread.ManagedThreadId, package.ActionId)); ActionGetter actionGetter; byte[] data = new byte[0]; if (!string.IsNullOrEmpty(package.RouteName)) { actionGetter = ActionDispatcher.GetActionGetter(package, session); if (CheckRemote(package.RouteName, actionGetter)) { MessageStructure response = new MessageStructure(); OnCallRemote(package.RouteName, actionGetter, response); data = response.PopBuffer(); } else { return; } } else { SocketGameResponse response = new SocketGameResponse(); response.WriteErrorCallback += ActionDispatcher.ResponseError; actionGetter = ActionDispatcher.GetActionGetter(package, session); DoAction(actionGetter, response); data = response.ReadByte(); } try { if (session != null && data.Length > 0) { session.SendAsync(actionGetter.OpCode, data, 0, data.Length, OnSendCompleted); } } catch (Exception ex) { TraceLog.WriteError("PostSend error:{0}", ex); } watch.Flush(true); } catch (Exception ex) { TraceLog.WriteError("Task error:{0}", ex); } finally { if (session != null) { session.ExitSession(); } } }
private void ProcessPackage(RequestPackage package, GameSession session) { if (package == null) { return; } try { ActionGetter actionGetter; byte[] data = new byte[0]; if (!string.IsNullOrEmpty(package.RouteName)) { actionGetter = ActionDispatcher.GetActionGetter(package, session); if (CheckRemote(package.RouteName, actionGetter)) { MessageStructure response = new MessageStructure(); OnCallRemote(package.RouteName, actionGetter, response); data = response.PopBuffer(); } else { return; } } else { SocketGameResponse response = new SocketGameResponse(); response.WriteErrorCallback += ActionDispatcher.ResponseError; actionGetter = ActionDispatcher.GetActionGetter(package, session); DoAction(actionGetter, response); data = response.ReadByte(); } try { if (session != null && data.Length > 0) { session.SendAsync(actionGetter.OpCode, data, 0, data.Length); } } catch (Exception ex) { TraceLog.WriteError("PostSend error:{0}", ex); } } catch (Exception ex) { TraceLog.WriteError("Task error:{0}", ex); } finally { if (session != null) { session.ExitSession(); } } }
/// <summary> /// 将指定的Action结果广播给指定范围的玩家 /// </summary> /// <typeparam name="T">BaseUser对象</typeparam> /// <param name="actionId">指定的Action</param> /// <param name="userList">指定范围的玩家</param> /// <param name="parameters">请求参数</param> /// <param name="successHandle">成功回调</param> /// <param name="onlineInterval">online interval second</param> public static void BroadcastAction <T>(int actionId, List <T> userList, Parameters parameters, Action <T> successHandle, int onlineInterval = 0) where T : IUser { try { if (userList.Count == 0) { return; } StringBuilder shareParam = new StringBuilder(); if (parameters != null) { foreach (var parameter in parameters) { shareParam.AppendFormat("&{0}={1}", parameter.Key, parameter.Value); } } IActionDispatcher actionDispatcher = new ScutActionDispatcher(); ActionGetter actionParam; byte[] sendData = GetActionResponse(actionDispatcher, actionId, userList[0], shareParam.ToString(), out actionParam); foreach (T user in userList) { if (object.Equals(user, null)) { continue; } try { GameSession session = GameSession.Get(user.GetUserId()); if (session != null) { if ((onlineInterval <= 0 || session.LastActivityTime > MathUtils.Now.AddSeconds(-onlineInterval)) && session.SendAsync(actionParam.OpCode, sendData, 0, sendData.Length)) { if (successHandle != null) { successHandle(user); } } } } catch (Exception ex) { TraceLog.WriteError("BroadcastAction action:{0} userId:{1} error:{2}", actionId, user.GetUserId(), ex); } } } catch (Exception ex) { TraceLog.WriteError("BroadcastAction action:{0} error:{1}", actionId, ex); } }
/// <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> /// /// </summary> public override void Start(string[] args) { socketLintener.StartListen(); if (EnableHttp) { httpListener.Start(); httpListener.BeginGetContext(OnHttpRequest, httpListener); } EntitySyncManger.SendHandle += (userId, data) => { GameSession session = GameSession.Get(userId); if (session != null) { return(session.SendAsync(data, 0, data.Length)); } return(false); }; OnStartAffer(); }
/// <summary> /// /// </summary> /// <param name="session"></param> /// <param name="actionId"></param> /// <param name="package"></param> /// <param name="complateHandle"></param> /// <param name="onlineInterval"></param> /// <returns></returns> public static async System.Threading.Tasks.Task SendAction(GameSession session, int actionId, RequestPackage package, Action <GameSession, SocketAsyncResult> complateHandle, int onlineInterval) { IActionDispatcher actionDispatcher = new ScutActionDispatcher(); GameSession temp = session; byte[] sendBuffer = null; try { sendBuffer = GetActionResponse(actionDispatcher, actionId, session, package); if ((onlineInterval <= 0 || session.LastActivityTime > MathUtils.Now.AddSeconds(-onlineInterval))) { await session.SendAsync(package.OpCode, sendBuffer, 0, sendBuffer.Length, result => { if (complateHandle != null) { complateHandle(temp, result); } }); } else { if (complateHandle != null) { complateHandle(temp, new SocketAsyncResult(sendBuffer) { Result = ResultCode.Close }); } } } catch (Exception ex) { if (complateHandle != null) { complateHandle(temp, new SocketAsyncResult(sendBuffer) { Result = ResultCode.Error, Error = ex }); } TraceLog.WriteError("SendToClient action:{0} userId:{1} error:{2}", actionId, session.UserId, ex); } }
/// <summary> /// 将指定的Action结果广播给指定范围的玩家 /// </summary> /// <typeparam name="T">BaseUser对象</typeparam> /// <param name="actionId">指定的Action</param> /// <param name="userList">指定范围的玩家</param> /// <param name="parameters">请求参数</param> /// <param name="successHandle">成功回调</param> public static void BroadcastAction <T>(int actionId, List <T> userList, Parameters parameters, Action <T> successHandle) where T : IUser { StringBuilder shareParam = new StringBuilder(); if (parameters != null) { foreach (var parameter in parameters) { shareParam.AppendFormat("&{0}={1}", parameter.Key, parameter.Value); } } IActionDispatcher actionDispatcher = new ScutActionDispatcher(); ActionGetter actionParam; byte[] sendData = GetActionResponse(actionDispatcher, actionId, null, shareParam.ToString(), out actionParam); foreach (T user in userList) { if (object.Equals(user, null)) { continue; } try { GameSession session = GameSession.Get(user.GetUserId()); if (session != null) { if (session.SendAsync(sendData, 0, sendData.Length)) { if (successHandle != null) { successHandle(user); } } } } catch (Exception ex) { TraceLog.WriteError("BroadcastAction action:{0} userId:{1} error:{2}", actionId, user.GetUserId(), ex); } } }
/// <summary> /// /// </summary> public override void Start(string[] args) { socketListener.StartListen(); if (EnableHttp) { httpListener.Start(); httpListener.BeginGetContext(OnHttpRequest, httpListener); } EntitySyncManger.SendHandle += (userId, data) => { GameSession session = GameSession.Get(userId); if (session != null) { var task = session.SendAsync(OpCode.Binary, data, 0, data.Length, result => { }); task.Wait(); return(task.Result); } return(false); }; base.Start(args); }
/// <summary> /// 将指定的Action结果广播给指定范围的玩家 /// </summary> /// <typeparam name="T">BaseUser对象</typeparam> /// <param name="actionId">指定的Action</param> /// <param name="userList">指定范围的玩家</param> /// <param name="parameters">请求参数</param> /// <param name="successHandle">成功回调</param> public static void BroadcastAction <T>(int actionId, List <T> userList, Parameters parameters, Action <T> successHandle) where T : BaseUser { StringBuilder shareParam = new StringBuilder(); if (parameters != null) { foreach (var parameter in parameters) { shareParam.AppendFormat("&{0}={1}", parameter.Key, parameter.Value); } } HttpGet httpGet; byte[] sendData = GetActionResponse(actionId, null, shareParam.ToString(), out httpGet); foreach (var user in userList) { if (user == default(T)) { continue; } try { GameSession session = GameSession.Get(user.GetUserId()); if (session != null) { if (session.SendAsync(sendData, 0, sendData.Length)) { if (successHandle != null) { successHandle(user); } } } } catch (Exception ex) { TraceLog.WriteError("BroadcastAction action:{0} userId:{1} error:{2}", actionId, user.PersonalId, ex); } } }
/// <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); } }
private async System.Threading.Tasks.Task ProcessPackage(RequestPackage package, GameSession session) { if (package == null) return; try { ActionGetter actionGetter; byte[] data = new byte[0]; if (!string.IsNullOrEmpty(package.RouteName)) { actionGetter = ActionDispatcher.GetActionGetter(package, session); if (CheckRemote(package.RouteName, actionGetter)) { MessageStructure response = new MessageStructure(); OnCallRemote(package.RouteName, actionGetter, response); data = response.PopBuffer(); } else { return; } } else { SocketGameResponse response = new SocketGameResponse(); response.WriteErrorCallback += ActionDispatcher.ResponseError; actionGetter = ActionDispatcher.GetActionGetter(package, session); DoAction(actionGetter, response); data = response.ReadByte(); } try { if (session != null && data.Length > 0) { await session.SendAsync(actionGetter.OpCode, data, 0, data.Length, OnSendCompleted); } } catch (Exception ex) { TraceLog.WriteError("PostSend error:{0}", ex); } } catch (Exception ex) { TraceLog.WriteError("Task error:{0}", ex); } finally { if (session != null) session.ExitSession(); } }