public static void MainLoop() { if (null == JClientSocket.socket) { JLog.Error("JClientDataReceiverThread.MainLoop JClientSocket.socket is null, please make sure JClientSocketManager is initialized. ", JGame.Log.JLogCategory.Network); return; } while (true) { if (_requireEnd) { break; } if (JClientSocket.socket.Available <= 0) { Thread.Sleep(100); continue; } byte[] buffer = new byte[JTcpDefines.max_buffer_size]; int recLen = JClientSocket.socket.Receive(buffer); if (recLen > 0) { JNetworkDataOperator.ReceiveData(recLen, buffer, JClientSocket.socket.RemoteEndPoint); } else { //说明socket已经断开连接 } } }
public void run(IDataSet dataSet) { IStreamObj roleObj = dataSet.getData(JObjectType.create_role); if (null == roleObj) { JLog.Error("JProcessorCreateRole.run roleObj is null."); return; } JCreateRoleReqObject createRoleReqObj = roleObj as JCreateRoleReqObject; if (null == createRoleReqObj) { JLog.Error("JProcessorCreateRole.run createRoleReqObj is null."); return; } try { JNetworkDataOperator.SendDataToServer(JPacketType.pt_createRole_req, createRoleReqObj); JLogicUserData.setLocalData(createRoleReqObj); } catch (Exception e) { JLog.Debug("JProcessorCreateRole 发送数据失败"); JLog.Error("JProcessorCreateRole 发送数据失败 " + e.Message); return; } }
public static void Logic() { //根据状态调用processer处理 //local List <JPacketType> addedLocalData = JLocalDataHelper.takeData(); if (addedLocalData.Count > 0) { JLog.Info("JLogic.Logic find local data, count : " + addedLocalData.Count.ToString(), JGame.Log.JLogCategory.Network); foreach (JPacketType data in addedLocalData) { ProcessLocalData(data); } } //network List <JNetworkData> receivedData = JNetworkDataOperator.TakeReceivedData(); if (receivedData.Count > 0) { JLog.Info("JLogic.Logic find network data, count : " + receivedData.Count.ToString(), JGame.Log.JLogCategory.Network); DeSerialize(receivedData); foreach (JNetworkData data in receivedData) { ProcessNetworkData(data); } } }
protected bool SendToServer(JObjAccountRegisterReq obj) { try { JNetworkDataOperator.SendDataToServer(JPacketType.npt_accountRegister_req, obj); return(true); } catch (Exception e) { JLog.Debug("发送数据失败"); JLog.Error(e.Message); return(false); } }
protected bool SendToServer(JObj_SignIn obj) { try { JNetworkDataOperator.SendData(JPacketType.npt_signin_req, obj); return(true); } catch (Exception e) { JLog.Debug("发送数据失败"); JLog.Error(e.Message); return(false); } }
public static void MainLoop() { if (null == JNetworkInteractiveData.SendData || null == JNetworkInteractiveData.SendData.Data) { JLog.Error("JNetworkDataSenderThread: JNetworkInteractiveData checked empty, please check initialize", JGame.Log.JLogCategory.Network); return; } if (null == JClientSocket.socket) { JLog.Error("JNetworkDataSenderThread: JClientSocket.socket is null, please check initialize", JGame.Log.JLogCategory.Network); return; } JLog.Info("JNetworkDataSenderThread: main loop started", JGame.Log.JLogCategory.Network); while (true) { if (_requireEnd) { break; } try { List <JNetworkData> dataList = JNetworkDataOperator.TakeSendData(1000); if (null == dataList) { continue; } foreach (JNetworkData data in dataList) { JClientSocket.socket.Send(data.Data); JLog.Debug("JNetworkDataSenderThread: send one network packet", JGame.Log.JLogCategory.Network); } } catch (Exception e) { JLog.Error(e.Message, JGame.Log.JLogCategory.Network); } } JLog.Info("JNetworkDataSenderThread main loop end.", JGame.Log.JLogCategory.Network); }
private void SendLoop() { JLog.Info("JServerSocketManager server send loop started", JGame.Log.JLogCategory.Network); while (true) { if (_forceEnd) { break; } List <JNetworkData> dataList = JNetworkDataOperator.TakeSendData(1000); if (null == dataList) { continue; } foreach (JNetworkData data in dataList) { lock (_socketLocker) { foreach (Socket socket in JConnectedClientSocket.sockets) { if (null == socket || null == socket.RemoteEndPoint) { continue; } if (JNetworkHelper.IsSameEndpoint(data.RemoteEndPoint as IPEndPoint, socket.RemoteEndPoint as IPEndPoint)) { try { socket.Send(data.Data); JLog.Info("send one data to " + (socket.RemoteEndPoint as IPEndPoint).Address.ToString(), JGame.Log.JLogCategory.Network); } catch (Exception e) { JLog.Error("SendLoop error message:" + e.Message, JGame.Log.JLogCategory.Network); } } } } } } JLog.Info("JServerSocketManager server send loop end", JGame.Log.JLogCategory.Network); }
public void run(IDataSet dataSet) { IStreamObj obj = dataSet.getData(JObjectType.sign_in); JObj_SignIn signInObj = obj as JObj_SignIn; if (signInObj == null) { return; } string account = signInObj._strAccount; string code = signInObj._strCode; JLog.Info("receive npt_signin_req packet from client: account:" + account + " code:" + code, JGame.Log.JLogCategory.Network); JObj_SignRet resultObj = new JObj_SignRet(); //ToDo:if account and code is in database then if (account == "test" && code == "123") { resultObj.Result = true; } else { resultObj.Result = false; } try { JNetworkDataOperator.SendData(JPacketType.npt_signin_ret, resultObj); JLog.Info("send npt_signin_ret packet to client", JGame.Log.JLogCategory.Network); return; } catch (Exception e) { JLog.Debug("发送数据失败"); JLog.Error(e.Message); return; } }
public void run(IDataSet dataSet) { IStreamObj obj = dataSet.getData(JObjectType.account_register); JObjAccountRegisterReq accountRegisterObj = obj as JObjAccountRegisterReq; if (accountRegisterObj == null) { return; } JMySqlAccess mysql = new JMySqlAccess(JDBUtil.ServerDatabaseName, JDBUtil.ServerDatasource, JDBUtil.ServerUser, JDBUtil.ServerUserCode); if (!mysql.Open()) { return; } if (!mysql.Connected) { return; } DataSet data = mysql.Select(string.Format( @"Select count(1) from {0} t where t.user_account = '{1}' or t.user_email = '{2}'", JDBUtil.TableName_Server_UserInfo, accountRegisterObj._strAccount, accountRegisterObj._strEmailAddress)); bool bAreadyExisted = false; do { if (null == data || null == data.Tables) { break; } if (data.Tables.Count <= 0) { break; } if (null == data.Tables[0].Rows || data.Tables[0].Rows.Count <= 0) { break; } if (int.Parse(data.Tables[0].Rows[0][0].ToString()) == 1) { bAreadyExisted = true; } }while(false); JObjAccountRegisterRet retObj = new JObjAccountRegisterRet(); try { if (!bAreadyExisted) { string resultMssage = ""; bool inserResult = mysql.DoSql( string.Format("INSERT into {0} ( user_account, user_code, user_email) VALUES( '{1}' , '{2}' , '{3}' );", JDBUtil.TableName_Server_UserInfo, accountRegisterObj._strAccount, accountRegisterObj._strCode, accountRegisterObj._strEmailAddress), ref resultMssage); JLog.Info("resultMssage"); if (inserResult) { retObj.Result = JObjAccountRegisterRet.AccountRegisterResultType.successed; JLog.Debug("Create account success, account: " + accountRegisterObj._strAccount); } else { retObj.Result = JObjAccountRegisterRet.AccountRegisterResultType.failed; JLog.Debug("Create account falied, account: " + accountRegisterObj._strAccount); } } else { retObj.Result = JObjAccountRegisterRet.AccountRegisterResultType.accountRepeated; } } catch (Exception e) { JLog.Error(e.Message); } finally { mysql.Close(); } try { JNetworkDataOperator.SendData(JPacketType.npt_accountRegister_ret, retObj, dataSet.EndPoint); JLog.Info("send npt_accountRegister_ret packet to client", JGame.Log.JLogCategory.Network); return; } catch (Exception e) { JLog.Debug("发送数据失败"); JLog.Error(e.Message); return; } }
private void ReceiveLoop() { JLog.Info("JServerSocketManager server receive loop started", JGame.Log.JLogCategory.Network); List <Socket> clientScokets = new List <Socket> (); while (true) { if (_forceEnd) { break; } if (_semaphore.WaitOne(1)) { _semaphore = new Semaphore(0, 10000); JLog.Info("JServerSocketManager ReceiveLoop _semaphore.WaitOne(1) success", JGame.Log.JLogCategory.Network); lock (_socketLocker) { foreach (Socket socket in JConnectedClientSocket.sockets) { clientScokets.Add(socket); } JLog.Info("JServerSocketManager ReceiveLoop add socket to clientsockets: count : " + clientScokets.Count.ToString(), JGame.Log.JLogCategory.Network); } JLog.Debug("connected client sockets : " + clientScokets.Count.ToString(), JGame.Log.JLogCategory.Network); } if (clientScokets.Count == 0) { _semaphore.WaitOne(); _semaphore.Release(); JLog.Debug("JServerSocketManager ReceiveLoop._semaphore.WaitOne success. ", JGame.Log.JLogCategory.Network); continue; } //JLog.Debug ("JServerSocketManager ReceiveLoop.Socket.Select begin Socket.Select clientsocket:count :" + clientScokets.Count.ToString (), JGame.Log.JLogCategory.Network); Socket.Select(clientScokets, null, null, 10000); if (clientScokets.Count > 0) { JLog.Debug("JServerSocketManager ReceiveLoop.Socket.Select selected clientsockets: count : " + clientScokets.Count.ToString(), JGame.Log.JLogCategory.Network); } List <Socket> disconnectedSockets = new List <Socket>(); foreach (Socket socket in clientScokets) { /*if (socket.Available <= 0) * continue;*/ //如果接收到了新的消息就重置包时间 lock (_heartbeatLocker) { if (_socketHeartDelayTime.ContainsKey(socket)) { _socketHeartDelayTime [socket] = JTime.CurrentTime; } else { _socketHeartDelayTime.Add(socket, JTime.CurrentTime); } } //receive form client socket bool bReceivedSuccess = false; if (socket.Connected) { try { byte[] recBuffer = new byte[JTcpDefines.max_buffer_size]; JLog.Info("try to receive from socket : " + (socket.RemoteEndPoint as IPEndPoint).Address.ToString(), JGame.Log.JLogCategory.Network); int recLen = socket.Receive(recBuffer); if (recLen > 0) { JLog.Info("receive one packet from client : IP" + (socket.RemoteEndPoint as IPEndPoint).Address.ToString() + " len:" + recLen.ToString(), JGame.Log.JLogCategory.Network); //save the received data JNetworkDataOperator.ReceiveData(recLen, recBuffer, socket.RemoteEndPoint); //add the selected socket to select sockets list //clientScokets.Add(socket); bReceivedSuccess = true; } } catch (Exception e) { JLog.Error("JServerSocketManager ReceiveLoop exception reveive error message:" + e.Message, JGame.Log.JLogCategory.Network); } } try { if (!bReceivedSuccess) { //client disconnect if (socket.Connected) { socket.Close(); } //record disconnected socket from list disconnectedSockets.Add(socket); JLog.Info("client socket disconnected : " + socket.RemoteEndPoint.ToString(), JGame.Log.JLogCategory.Network); } } catch (Exception e) { JLog.Error("JServerSocketManager ReceiveLoop exception error message1:" + e.Message, JGame.Log.JLogCategory.Network); } } try { //remove disconnected socket form list if (disconnectedSockets.Count > 0) { lock (_socketLocker) { foreach (Socket socket in disconnectedSockets) { JConnectedClientSocket.sockets.Remove(socket); clientScokets.Remove(socket); } } } //add old sockets to client sockets lock (_socketLocker) { foreach (Socket socket in JConnectedClientSocket.sockets) { clientScokets.Add(socket); } } } catch (Exception e) { JLog.Error("JServerSocketManager ReceiveLoop exception error message2:" + e.Message, JGame.Log.JLogCategory.Network); } } JLog.Info("JServerSocketManager server receive loop end.", JGame.Log.JLogCategory.Network); }
public void run(IDataSet dataSet) { IStreamObj obj = dataSet.getData(JObjectType.sign_in); JObj_SignIn signInObj = obj as JObj_SignIn; if (signInObj == null) { return; } string account = signInObj._strAccount; string code = signInObj._strCode; JLog.Info("JProcesserSignInServer.run receive npt_signin_req packet from client: account:" + account + " code:" + code, JGame.Log.JLogCategory.Network); JObj_SignRet resultObj = new JObj_SignRet(); JMySqlAccess sqlite = new JMySqlAccess("mysql", "127.0.0.1", "root", "684268"); if (!sqlite.Open()) { return; } if (!sqlite.Connected) { return; } DataSet data = sqlite.Select(string.Format( @"Select * from user_info t where t.user_account = '{0}' and t.user_code = '{1}'", signInObj._strAccount, signInObj._strCode)); bool bSuccess = false; do { if (null == data || null == data.Tables) { break; } if (data.Tables.Count <= 0) { break; } if (null == data.Tables[0].Rows || data.Tables[0].Rows.Count <= 0) { break; } if (data.Tables[0].Rows.Count > 0) { bSuccess = true; } }while(false); resultObj.Result = bSuccess; string[] items = new string[] { "role_name", "role_type", "role_level", "x", "y", "z", "x_rotation", "y_rotation", "z_rotation", "user_account" }; string[] where_cols = new string[] { "user_account" }; string[] operation = new string[] { "=" }; string[] values = new string[] { signInObj._strAccount }; string strAccount = null; DataSet roleInfo = sqlite.Select("role_info", items, where_cols, operation, values); do { if (null == roleInfo || null == roleInfo.Tables) { break; } if (roleInfo.Tables.Count <= 0) { break; } if (null == roleInfo.Tables[0].Rows || roleInfo.Tables[0].Rows.Count <= 0) { break; } try { if (roleInfo.Tables [0].Rows.Count > 0) { resultObj.RolesInfo = new System.Collections.Generic.List <JObjRoleInfo> (); foreach (DataRow dataRow in roleInfo.Tables[0].Rows) { JObjRoleInfo role = new JObjRoleInfo(); role.roleName = dataRow [0].ToString(); role.roleType = int.Parse(dataRow [1].ToString()); role.roleLevel = int.Parse(dataRow [2].ToString()); double coord; if (double.TryParse(dataRow [3].ToString(), out coord)) { role.x = coord; } if (double.TryParse(dataRow [4].ToString(), out coord)) { role.y = coord; } if (double.TryParse(dataRow [5].ToString(), out coord)) { role.z = coord; } if (double.TryParse(dataRow [6].ToString(), out coord)) { role.rotatex = coord; } if (double.TryParse(dataRow [7].ToString(), out coord)) { role.rotatey = coord; } if (double.TryParse(dataRow [8].ToString(), out coord)) { role.rotatez = coord; } resultObj.RolesInfo.Add(role); strAccount = dataRow[9].ToString(); } } } catch (Exception e) { JLog.Error("JProcesserSignInServer.run " + e.Message); } }while(false); if (null != strAccount) { //记录当前新增的登录信息到对应的dataSet IStreamObj clientobj = dataSet.getData(JObjectType.sign_in_info); if (null != clientobj) { JSignInClientInfoObject clientInfo = clientobj as JSignInClientInfoObject; clientInfo.Info.Account = strAccount; } else { JSignInClientInfoObject clientInfo = new JSignInClientInfoObject(); clientInfo.Info.Account = strAccount; clientobj = clientInfo; } JLog.Info("JProcesserSignInServer.run add connected client info dataset, account:" + strAccount, JGame.Log.JLogCategory.Network); dataSet.setData(clientobj); } try { JNetworkDataOperator.SendData(JPacketType.npt_signin_ret, resultObj, dataSet.EndPoint); JLog.Info("JProcesserSignInServer.run send npt_signin_ret packet to client", JGame.Log.JLogCategory.Network); return; } catch (Exception e) { JLog.Debug("发送数据失败"); JLog.Error(e.Message); return; } }
public void SendData(byte[] data) { JNetworkDataOperator.SendData(data); }
public void run(IDataSet dataSet) { JCreateRoleRetObject createRoleRetObj = new JCreateRoleRetObject(); createRoleRetObj.Result = JCreateRoleRetObject.CreateRoleResultType.failed; do { //check role info existed IStreamObj signinObj = dataSet.getData(JObjectType.sign_in_info); if (null == signinObj) { createRoleRetObj.Result = JCreateRoleRetObject.CreateRoleResultType.failed; JLog.Error("JProcessorCreateRole.run can not find sign in account info"); break; } JSignInClientInfoObject clientInfo = signinObj as JSignInClientInfoObject; string account = clientInfo.Info.Account; //process create role request IStreamObj obj = dataSet.getData(JObjectType.create_role); JCreateRoleReqObject createRoleReqObj = obj as JCreateRoleReqObject; if (createRoleReqObj == null) { createRoleRetObj.Result = JCreateRoleRetObject.CreateRoleResultType.failed; break; } string roleName = createRoleReqObj.RoleName; int roleType = createRoleReqObj.RoleType; //open data base JMySqlAccess mysql = new JMySqlAccess(JDBUtil.ServerDatabaseName, JDBUtil.ServerDatasource, JDBUtil.ServerUser, JDBUtil.ServerUserCode); try { if (!mysql.Open()) { break; } if (!mysql.Connected) { break; } } catch (Exception e) { JLog.Error("CheckRoleNameIsValud." + e.Message); } if (!mysql.Connected) { mysql.Close(); JLog.Error("JProcessorCreateRole.run open database fialied"); break; } //check name is valid or not int nErrorType = 0; if (!CheckRoleNameIsValud(mysql, account, roleName, ref nErrorType)) { createRoleRetObj.Result = (JCreateRoleRetObject.CreateRoleResultType)nErrorType; JLog.Info("JProcessorCreateRole.run Create role request not valid, error type:" + createRoleRetObj.Result.GetDescription()); mysql.Close(); break; } if (!CheckRoleTypeIsValud(roleType, ref nErrorType)) { createRoleRetObj.Result = (JCreateRoleRetObject.CreateRoleResultType)nErrorType; JLog.Info("Create role request not valid, error type:" + createRoleRetObj.Result.GetDescription()); mysql.Close(); break; } //insert new role record to role_info table string roleID = ""; if (!CreateRole(mysql, account, roleName, roleType, ref roleID, ref nErrorType)) { createRoleRetObj.Result = (JCreateRoleRetObject.CreateRoleResultType)nErrorType; JLog.Info("JProcessorCreateRole.run CreateRole falied, error type:" + createRoleRetObj.Result.GetDescription()); } createRoleRetObj.Result = JCreateRoleRetObject.CreateRoleResultType.successed; mysql.Close(); } while (false); try { JNetworkDataOperator.SendData(JPacketType.pt_createRole_ret, createRoleRetObj, dataSet.EndPoint); } catch (Exception e) { JLog.Debug("JProcessorCreateRole 发送数据失败"); JLog.Error("JProcessorCreateRole 发送数据失败 " + e.Message); } }