This document is about: SERVER 4
SWITCH TO

Server-To-Server 소개

이 튜토리얼은 고급 개발자를 위한 것 입니다. 이제 막 Photon 을 사용하기 시작 한 경우라면 이 튜토리얼을 건너 뛰어도 좋습니다.

복잡한 게임을 개발 중이라면 서로 다른 서버간에 기능을 분산시키는 것을 원할 수 도 있습니다. Photon 의 Server-To-Server (S2S) 기능을 이용하여 Photon 서버간에 커뮤니케이션을 구현할 수 있습니다.

개념

Photon 의 S2S-커뮤니케이션은 클라이언트-서버 커뮤니케이션과 매우 유사합니다. 하나의 Photon 인스턴스가 (연결의 측면에서는 "outbound" 가 됩니다) 타겟 Photon 서버(연결의 측면에서는 "inbound") 연결을 초기화합니다. 각 측에서는 피어가 생성되고 오퍼레이션/이벤트를 S2S 연결로 전송 할 수 있습니다.

outbound 측

ApplicationBase 에서 상속 받은 "MyOutboundApplication" 이라는 어플리케이션이 있다고 가정하도록 하겠습니다.

S2S-연결을 초기화 하기 위해서, OutboundS2SPeer.ConnectXXX 계열의 메소드 하나를 호출 해야 합니다. S2S 커뮤니케이션에서는 TCP 연결을 사용하는 것을 권장 합니다.

이 코드는 S2S 연결을 설정하기 위하여 필요 합니다:

C#

    public class MyOutboundApplication : ApplicationBase
    {
        private MyOutboundPeer outboundPeer;
        protected override void Setup()
        {
            this.outboundPeer = new MyOutboundPeer(this);
            this.outboundPeer.ConnectTcp(new IPEndPoint(IPAddress.Parse("123.456.789.123"), 4520), "MyInboundApplication"); 
        }
    }

위 코드는 Setup() 하는 동안 S2S 연결을 성립합니다. 물론 나중에 코드에서 수행할 수 도 있습니다. 아무튼, 두개 서버간에는 단 하나의 연결만이 있어야 합니다 - 연결을 계속 오픈상태로 유지시키고 하나의 연결을 통하여 모든 데이터를 전송하세요.

타겟 서버로 데이터를 전송하기 위해서는 그 피어의 SendOperation 메소드를 아래 처럼 단순하게 호출 하면 됩니다:

C#

    public void SendMessageToMaster(string message)
    {
        var parameters = new Dictionary<byte, object>();
        parameters[0] = message;
        this.outboundPeer.SendOperationRequest(new OperationRequest { OperationCode = 1, Parameters = parameters }, new SendParameters());
    }

"OutboundPeer" 의 구현도 또한 매우 간단합니다. 다음은 가장 최소한의 샘플이며 나만의 코드로 맞추어 주세요:

C#

    public class MyOutboundPeer : OutboundS2SPeer
    {
        public MyOutboundPeer(ApplicationBase application)
            : base(application)
        {
        }
        protected override void OnConnectionEstablished(object responseObject)
        {
        }
        protected override void OnConnectionFailed(int errorCode, string errorMessage)
        {
            // add some custom error handling here 
        }
        protected override void OnOperationResponse(OperationResponse operationResponse, SendParameters sendParameters)
        {
        }
        protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)
        {
        }
    }

만약 연결이 성공적으로 성립되었다면 OnConnectionEstablished 메소드가 트리거 됩니다. 그렇지 않으면 OnConnectionFailed() 메소드가 호출 됩니다. 이 메소드를 오버라이드 하여 일부 에러 핸들링을 추가, 로깅 등을 추가 할 수 도 있습니다. 이것이 S2S 연결의 "outbound" 측에서 필요한 모든 것 입니다.

"inbound" 측

"outbound" 측이 완료 되었으므로, S2S 연결에 대한 "inbound" 측을 구현 해야 합니다. 우선, 피어 객체를 생성해야 합니다. incoming 연결 소스를 결정하기 위해서는("클라이언트" 또는"서버") 다른 포트를 리슨(listen)하는 것을 권장 합니다.

ApplicationBase 로 부터 상속 받은 어플리케이션이 있다고 가정 해 보겠습니다. 다음의 코드를 추가하세요:

C#

      protected override PeerBase CreatePeer(InitRequest initRequest)
    {
        if (initRequest.LocalPort == 4520)
        {
            // for S2S connections
            return new MyInboundPeer(initRequest); 
        }
    }

The MyInboundPeer is just a simple class that inherits of the PeerBase class:

C#

    public class MyInboundPeer : InboundS2SPeer
    {
        public MyInboundPeer(InitRequest initRequest)
            : base(initRequest)
        {
        }
        protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
        {
            // implement this to receive operation data
        }
        protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)
        {
        }
    }

S2S 연결의 "inbound"측에서 할 모든 것 입니다.

환경 설정

inbound 측에서 S2S 연결의 Listen할 포트를 분리하는 것으로 결정 했다면 PhotonServer.config 에 다음과 같이 새로운 리스너를 정의 해 주어야 합니다:

XML

    <!-- DON'T EDIT THIS. TCP listener for S2S connections on MyOutbound application -->
    <TCPListener
        IPAddress="0.0.0.0"
        Port="4520">
    </TCPListener>
Back to top