/// <summary> /// Recover session /// </summary> /// <param name="session"></param> /// <param name="newSessionKey"></param> /// <param name="socket"></param> /// <param name="appServer"></param> /// <returns></returns> public static void Recover(GameSession session, Guid newSessionKey, TNSocket socket, ISocket appServer) { var newSession = Get(newSessionKey); if (session != null && newSession != null && session != newSession) { try { if (session._exSocket != null) { session._exSocket.Close(); } } catch { } //modify socket's keycod not found reason if (socket != null && appServer != null) { socket.Reset(session.KeyCode); session._exSocket = socket; session.AppServer = appServer; } GameSession temp; if (_globalSession.TryRemove(newSessionKey, out temp)) { OnChangedSave(session); } } }
/// <summary> /// /// </summary> /// <param name="socket"></param> /// <param name="handshakeData"></param> /// <returns></returns> protected override bool ResponseHandshake(TNSocket socket, HandshakeData handshakeData) { string secKey1; string secKey2; byte[] secKey3; if (handshakeData.ParamItems.TryGet(HandshakeHeadKeys.SecKey1, out secKey1) && handshakeData.ParamItems.TryGet(HandshakeHeadKeys.SecKey2, out secKey2) && handshakeData.ParamItems.TryGet(HandshakeHeadKeys.SecKey3, out secKey3)) { //The minimum version support StringBuilder response = new StringBuilder(); response.AppendLine(HandshakeHeadKeys.RespHead_00); response.AppendLine(HandshakeHeadKeys.RespUpgrade00); response.AppendLine(HandshakeHeadKeys.RespConnection); string origin; if (handshakeData.ParamItems.TryGet(HandshakeHeadKeys.Origin, out origin)) { response.AppendLine(string.Format(HandshakeHeadKeys.RespOriginLine, origin)); } response.AppendLine(string.Format(HandshakeHeadKeys.SecLocation, handshakeData.UriSchema, handshakeData.Host, handshakeData.UrlPath)); if (!string.IsNullOrEmpty(handshakeData.Protocol)) { response.AppendLine(string.Format(HandshakeHeadKeys.RespProtocol, handshakeData.Protocol)); } response.AppendLine(); Handler.SendMessage(socket, response.ToString(), Encoding, result => { }); //Encrypt message byte[] securityKey = GetResponseSecurityKey(secKey1, secKey2, secKey3); Handler.SendMessage(socket, securityKey, result => { }); return(true); } return(false); }
private static GameSession OnCreate(Guid keyCode, params object[] args) { GameSession session; if (args.Length == 0) { session = new GameSession(keyCode, null); } else if (args.Length == 1) { session = new GameSession(keyCode, args[0]); } else if (args.Length == 2 && args[0] is TNSocket) { TNSocket socket = args[0] as TNSocket; var appServer = args[1] as ISocket; session = new GameSession(keyCode, socket, appServer); } else { throw new ArgumentOutOfRangeException("param is error"); } _globalSession[keyCode] = session; return(session); }
/// <summary> /// /// </summary> /// <param name="exSocket"></param> /// <param name="opCode"></param> /// <param name="data"></param> /// <param name="offset"></param> /// <param name="count"></param> /// <returns></returns> public override byte[] BuildMessagePack(TNSocket exSocket, sbyte opCode, byte[] data, int offset, int count) { if (opCode == OpCode.Close) { return(ClosingBytes); } byte[] buffer = new byte[count + 2]; buffer[0] = StartByte; Buffer.BlockCopy(data, offset, buffer, 1, count); buffer[count + 1] = EndByte; return(buffer); }
internal void InitSocket(TNSocket exSocket, ISocket appServer) { _exSocket = exSocket; if (_exSocket != null) { _remoteAddress = _exSocket.RemoteEndPoint.ToNotNullString(); } AppServer = appServer; if (User != null) { //update userid with sid. _userHash[UserId] = KeyCode; } }
/// <summary> /// /// </summary> /// <param name="socket"></param> /// <param name="handshakeData"></param> /// <returns></returns> protected override bool ResponseHandshake(TNSocket socket, HandshakeData handshakeData) { if (handshakeData.WebSocketVersion < _version) { return(base.ResponseHandshake(socket, handshakeData)); } string secKeyAccept = GenreateKey(handshakeData); StringBuilder response = new StringBuilder(); response.AppendLine(HandshakeHeadKeys.RespHead_10); response.AppendLine(HandshakeHeadKeys.RespUpgrade); response.AppendLine(HandshakeHeadKeys.RespConnection); response.AppendLine(string.Format(HandshakeHeadKeys.RespAccept, secKeyAccept)); if (!string.IsNullOrEmpty(handshakeData.Protocol)) { response.AppendLine(string.Format(HandshakeHeadKeys.RespProtocol, handshakeData.Protocol)); } response.AppendLine(); Handler.SendMessage(socket, response.ToString(), Encoding, result => { }); return(true); }
/// <summary> /// /// </summary> /// <param name="socket"></param> /// <param name="reason"></param> public override void CloseHandshake(TNSocket socket, string reason) { requestHandler.SendCloseHandshake(socket, OpCode.Close, reason); }
/// <summary> /// /// </summary> /// <param name="exSocket"></param> /// <param name="opCode"></param> /// <param name="reason"></param> public override byte[] CloseMessage(TNSocket exSocket, sbyte opCode, string reason) { return(ClosingBytes); }
/// <summary> /// /// </summary> /// <param name="socket"></param> /// <param name="handshakeData"></param> protected abstract bool ResponseHandshake(TNSocket socket, HandshakeData handshakeData);
private GameSession(Guid sid, TNSocket exSocket, ISocket appServer) : this(sid, null) { InitSocket(exSocket, appServer); }
/// <summary> /// Add session to cache /// </summary> /// <param name="keyCode"></param> /// <param name="socket"></param> /// <param name="appServer"></param> public static GameSession CreateNew(Guid keyCode, TNSocket socket, ISocket appServer) { return(OnCreate(keyCode, socket, appServer)); }
/// <summary> /// /// </summary> /// <param name="exSocket"></param> /// <returns></returns> protected bool CheckVersion(TNSocket exSocket) { return(exSocket != null && exSocket.Handshake != null && exSocket.Handshake.WebSocketVersion < Version); }
/// <summary> /// /// </summary> /// <param name="exSocket"></param> /// <param name="opCode"></param> /// <param name="reason"></param> public override byte[] CloseMessage(TNSocket exSocket, sbyte opCode, string reason) { byte[] data = Encoding.UTF8.GetBytes(reason); return(BuildMessagePack(exSocket, opCode, data, 0, data.Length)); }
/// <summary> /// /// </summary> /// <param name="exSocket"></param> /// <param name="opCode"></param> /// <param name="data"></param> /// <param name="offset"></param> /// <param name="count"></param> /// <returns></returns> public override byte[] BuildMessagePack(TNSocket exSocket, sbyte opCode, byte[] data, int offset, int count) { if (CheckVersion(exSocket)) { return(base.BuildMessagePack(exSocket, opCode, data, offset, count)); } bool isMask = IsMask; int maskNum = isMask ? MaskLength : 0; byte[] buffer; if (count < 126) { buffer = new byte[count + maskNum + 2]; //buffer[0] = 0x81; buffer[1] = (byte)count; //Buffer.BlockCopy(data, offset, buffer, 2, count); } else if (count < 0xFFFF) { //uint16 bit buffer = new byte[count + maskNum + 4]; //buffer[0] = 0x81; buffer[1] = 126; buffer[2] = (byte)(count / 256); buffer[3] = (byte)(count % 256); //Buffer.BlockCopy(data, offset, buffer, 4, count); } else { //uint64 bit buffer = new byte[count + maskNum + 10]; //buffer[0] = 0x81; buffer[1] = 127; int num2 = count; int num3 = 256; for (int i = 9; i > 1; i--) { buffer[i] = (byte)(num2 % num3); num2 /= num3; if (num2 == 0) { break; } } } if (isMask) { //mask after of payloadLength byte[] mask = GenerateMask(); int maskPos = buffer.Length - maskNum - count; Buffer.BlockCopy(mask, 0, buffer, maskPos, mask.Length); EncodeMask(data, offset, count, mask, buffer, buffer.Length - count); } else { Buffer.BlockCopy(data, offset, buffer, buffer.Length - count, count); } buffer[0] = (byte)((byte)opCode | 0x80); if (isMask) { buffer[1] = (byte)(buffer[1] | 0x80); } return(buffer); }