/****************************** * 说明:Socket传输基类 * 创建人:龚宇超 * 创建日期:2018-01-23 * 修改人: * 修改日期: * 修改备注: * 版本:1.0.0.0 ******************************/ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Lskj.SocketService; namespace Lskj.SocketClient { /// /// Socket传输基类 /// public class ClientBaseSocket : SyncSocketInvokeElement { protected string _errorString; /// /// 用户名 /// protected string _userName; /// /// 密码 /// protected string _password; /// /// 错误码 /// /// The error string. public string ErrorString { get { return _errorString; } } public ClientBaseSocket() : base() { } /// /// 说明:检查是否传输结果 /// 创建人:龚宇超 /// 创建日期:2018-01-24 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// true if XXXX, false otherwise. public bool CheckErrorCode() { int errorCode = 0; _incomingDataParser.GetValue(ProtocolKey.Code, ref errorCode); if (errorCode == ProtocolCode.Success) return true; else { _errorString = ProtocolCode.GetErrorCodeString(errorCode); return false; } } /// /// 说明:发送Active命令 /// 创建人:龚宇超 /// 创建日期:2018-01-24 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// true if XXXX, false otherwise. public bool DoActive() { try { _outgoingDataAssembler.Clear(); _outgoingDataAssembler.AddRequest(); _outgoingDataAssembler.AddCommand(ProtocolKey.Active); SendCommand(); bool bSuccess = RecvCommand(); if (bSuccess) return CheckErrorCode(); else return false; } catch (Exception E) { //记录日志 _errorString = E.Message; return false; } } /// /// 说明:发送登录命令 /// 创建人:龚宇超 /// 创建日期:2018-01-24 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// Name of the user. /// The password. /// true if XXXX, false otherwise. public bool DoLogin(string userName, string password) { try { _outgoingDataAssembler.Clear(); _outgoingDataAssembler.AddRequest(); _outgoingDataAssembler.AddCommand(ProtocolKey.Login); _outgoingDataAssembler.AddValue(ProtocolKey.UserName, userName); _outgoingDataAssembler.AddValue(ProtocolKey.Password, BasicFunc.MD5String(password)); SendCommand(); bool bSuccess = RecvCommand(); if (bSuccess) { bSuccess = CheckErrorCode(); if (bSuccess) { _userName = userName; _password = password; } return bSuccess; } else return false; } catch (Exception E) { //记录日志 _errorString = E.Message; return false; } } /// /// 说明:检查连接是否存在,不存在则重连服务器 /// 创建人:龚宇超 /// 创建日期:2018-01-24 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// true if XXXX, false otherwise. public bool ReConnect() { if (_tcpClient.Connected && (!DoActive())) return true; else { if (!_tcpClient.Connected) { try { Connect(_host, _port); return true; } catch (Exception) { return false; } } else return true; } } /// /// 说明:检查连接是否存在,不存在则重连并登录 /// 创建人:龚宇超 /// 创建日期:2018-01-24 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// true if XXXX, false otherwise. public bool ReConnectAndLogin() { if (_tcpClient.Connected && (!DoActive())) return true; else { if (!_tcpClient.Connected) { try { Disconnect(); Connect(_host, _port); return DoLogin(_userName, _password); } catch (Exception E) { return false; } } else return true; } } } }