• 카테고리

    질문 & 답변
  • 세부 분야

    게임 프로그래밍

  • 해결 여부

    해결됨

GameRoom 관련 질문입니다.

20.07.01 15:18 작성 조회수 308

0

아무리 보고 또 봐도 생각을 아무리 해봐도 이해가 잘 안돼서 질문 남깁니다.

질문이 너무 잦아 부끄러운 마음이 먼저 드네요..ㅠ

class ClientSession : PacketSession
    {
        public GameRoom Room { get; set; }

        public override void OnConnected(EndPoint endPoint)
        {
            Console.WriteLine($"OnConnected: {endPoint}");
            Program.Room.Enter(this); // 1
        }

        public override void OnDisconnected(EndPoint endPoint)
        {
            SessionManager.Instance.Remove(this);
            // 2
            if (Room != null)
            {
                Room.Leave(this); 
                Room = null;
            }
            Console.WriteLine($"OnDisconnected: {endPoint}");
        }
...

OnConnected 메소드에서 주석 1번과 OnDisconnected 메소드에서 주석 2번 영역인데요.

OnConnected 에서는 Program 의 Room에  입장을 시키는데

어째서 OnDisconnected 에서는 Room에 Leave를 하는지 아무리봐도 모르겠습니다.
코드대로 읽어보면 Enter는 Program영역이고 Leave는 ClientSession의 영역이라 서로 다른 처리를 하고있는게 아닌가 하는 생각이 들고요.


또 하나 신기하면서도 이해가 안되는 부분은

public static void ClientChatHandler(PacketSession session, IPacket packet)
    {
        ClientChat chatPacket = packet as ClientChat;
        ClientSession clientSession = session as ClientSession;

        // 여기!
        if (clientSession.Room == null)
            return;

        clientSession.Room.Broadcast(clientSession, chatPacket.chat);
    }

clientSession에는 Room을 넣어준 적이 없는데 if를 통과하는 부분이에요. 분명 어디선가 초기화가 되고 있다는 뜻인데 암만 봐도 모르겠습니다... ㅠ

심지어 ClientSession의 Room 은 초기화 되는 곳이 그 어느곳을 찾아봐도 없어요.ㅠ

답변 2

·

답변을 작성해보세요.

0

ㅎㅎㅎ
사실 컨텐츠 로직은 그때 그때 다르기 때문에 엄청 중요한 부분은 아니고,
그냥 브로드캐스팅의 개념을 실습하기 위한 예제입니다.

0

장민창님의 프로필

장민창

질문자

2020.07.01

아, GameRoom 코드내에 있네요. 정신없이 작성하다보니 놓쳤던 부분입니다.

전부다 해결되었어요!!!!