void Awake() { if (Instance == null) { Instance = this; DontDestroyOnLoad(transform.gameObject); } else { if (Instance != this) { Destroy(transform.gameObject); } } }
public override void ChannelRead(IChannelHandlerContext context, object message) { var session = context.Channel.GetAttribute(NettyAttributes.SessionKey).Get(); var packet = (IPacketReader)message; if (session != null) { session.OnPacket(packet); } else { var version = packet.ReadShort(); var patch = packet.ReadString(); var seqSend = packet.ReadUInt(); var seqRecv = packet.ReadUInt(); var locale = packet.ReadByte(); if (version != _connector.Version) { return; } if (patch != _connector.Patch) { return; } if (locale != _connector.Locale) { return; } var newSocket = new NettySocket( context.Channel, seqSend, seqRecv ); var newSession = _connector.SessionInitializer.Initialize(newSocket); lock (_connector) _connector.Session = newSession; context.Channel.GetAttribute(NettyAttributes.SocketKey).Set(newSocket); context.Channel.GetAttribute(NettyAttributes.SessionKey).Set(newSession); } }
public override void ChannelActive(IChannelHandlerContext context) { var random = new Random(); var newSocket = new NettySocket( context.Channel, (uint)random.Next(), (uint)random.Next() ); var newSession = _acceptor.SessionInitializer.Initialize(newSocket); var handshake = new UnstructuredOutgoingPacket(); handshake.WriteShort(_acceptor.Version); handshake.WriteString(_acceptor.Patch); handshake.WriteInt((int)newSocket.SeqRecv); handshake.WriteInt((int)newSocket.SeqSend); handshake.WriteByte(_acceptor.Locale); _ = newSocket.Dispatch(handshake); context.Channel.GetAttribute(NettyAttributes.SocketKey).Set(newSocket); context.Channel.GetAttribute(NettyAttributes.SessionKey).Set(newSession); lock (_acceptor) _acceptor.Sessions.Add(newSession.Socket.ID, newSession); }