FleetIQ 서버 - 클라이언트 기능은 GameLift 에 종속되지않는 독립형 기능입니다.
현재 CLI 및 SDK 형태로 구현을 제공하고 있고, 본 실습에서는 Python 기반의 TCP 통신을 구현해보도록 하겠습니다.
간단하면서도 직접 FleetIQ 의 동작 원리를 이해하고 서버 - 클라이언트 구조를 생성해볼 수 있는 좋은 예제입니다.
Cloud9 콘솔로 접속합니다. (https://console.aws.amazon.com/cloud9/home)
다음과 같이 새 IDE 를 만듭니다. 이 IDE 를 통해 원격 게임 서버의 코드를 편집할 것 입니다.
접속에 대한 상세한 가이드는 다음 링크를 참고합니다.
vi ~/.ssh/authorized_keys
커맨드를 입력하고, 나오는 인증키 정보 창에서 맨 뒤에 복사한 값을 붙여넣기 합니다.
ssh-rsa ~~~ key
ssh-rsa @@@ xxx@amazon.com
과 같은 형태로 텍스트 파일이 구성되게 됩니다.
쉽게 구성하시기 위해서는 vi 기준으로 A 를 입력하시면 파일의 맨 끝으로 이동합니다. 엔터키를 치신 후 붙여넣기하고 저장합니다.
curl -L https://raw.githubusercontent.com/c9/install/master/install.sh | bash
위의 스크립트를 통해 간편하게 EC2 인스턴스에 Cloud9 이 설치됩니다.
import socket
import boto3
import requests
GameServerGroupName = 'GameServerGroups'
GameServerId = 'game-server-1'
InstanceId = requests.get('http://169.254.169.254/latest/meta-data/instance-id').text
ConnectionInfo = requests.get('http://169.254.169.254/latest/meta-data/public-hostname').text
client = boto3.client('gamelift', 'ap-northeast-2')
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("", 5000))
server_socket.listen(5)
def list_game_servers():
pass
# Register GameServer
def register_game_server():
pass
#
# Update GameServer
def update_game_server(flag):
pass
#
# Deregister GameServer
def deregister_game_server():
pass
#
print("TCP server waiting for Client on port 5000")
while True:
# List Game Server
listResponse = list_game_servers()
if not 'GameServers' in listResponse or not any(server['GameServerId'] == GameServerId for server in listResponse['GameServers']):
# Register Game Server to Game Server Group
register_game_server()
#
# Update Game Server Status "AVAILABLE"
update_game_server(True)
#
client_socket, address = server_socket.accept()
print("I got connection from ", address)
# Update Game Server Status "UTILIZED"
update_game_server(False)
#
while True:
data = client_socket.recv(512).decode()
print("RECEIVED:", data)
if(data == 'q' or data == 'Q'):
client_socket.close()
break
else:
client_socket.send(data.encode())
# Deregister Game Server
deregister_game_server()
#
server_socket.close()
print("SOCKET Closed... End")
먼저 위의 코드에서 리전을 실습을 진행하는 리전으로 변경해줍니다. 그리고 맨 위의 GameServerGroupName 을 앞서 구성하신 게임 서버 그룹 이름으로 변경합니다. GameServerId는 임의로 유니크하게 지정해줍니다
def list_game_servers():
response = client.list_game_servers(
GameServerGroupName=GameServerGroupName
)
print(response)
return response
이 함수는 현재 가용한 게임 서버 그룹 내의 게임 서버들을 확인합니다. 비어있다면 새로 등록해줘야 합니다.
def register_game_server():
response = client.register_game_server(
GameServerGroupName=GameServerGroupName,
GameServerId=GameServerId,
InstanceId=InstanceId,
ConnectionInfo=ConnectionInfo
)
print(response)
def update_game_server(flag):
if flag == True:
return client.update_game_server(
GameServerGroupName=GameServerGroupName,
GameServerId=GameServerId,
HealthCheck="HEALTHY",
UtilizationStatus="AVAILABLE"
)
else:
return client.update_game_server(
GameServerGroupName=GameServerGroupName,
GameServerId=GameServerId,
HealthCheck="HEALTHY",
UtilizationStatus="UTILIZED"
)
def deregister_game_server():
response = client.deregister_game_server(
GameServerGroupName=GameServerGroupName,
GameServerId=GameServerId
)
print(response)
import socket
import boto3
import requests
GameServerGroupName = 'GameServerGroups'
GameServerId = 'game-server-1'
InstanceId = requests.get('http://169.254.169.254/latest/meta-data/instance-id').text
ConnectionInfo = requests.get('http://169.254.169.254/latest/meta-data/public-hostname').text
client = boto3.client('gamelift', 'ap-northeast-2')
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("", 5000))
server_socket.listen(5)
def list_game_servers():
response = client.list_game_servers(
GameServerGroupName=GameServerGroupName
)
print(response)
return response
# Register GameServer
def register_game_server():
response = client.register_game_server(
GameServerGroupName=GameServerGroupName,
GameServerId=GameServerId,
InstanceId=InstanceId,
ConnectionInfo=ConnectionInfo
)
print(response)
#
# Update GameServer
def update_game_server(flag):
if flag == True:
return client.update_game_server(
GameServerGroupName=GameServerGroupName,
GameServerId=GameServerId,
HealthCheck="HEALTHY",
UtilizationStatus="AVAILABLE"
)
else:
return client.update_game_server(
GameServerGroupName=GameServerGroupName,
GameServerId=GameServerId,
HealthCheck="HEALTHY",
UtilizationStatus="UTILIZED"
)
#
# Deregister GameServer
def deregister_game_server():
response = client.deregister_game_server(
GameServerGroupName=GameServerGroupName,
GameServerId=GameServerId
)
print(response)
#
print("TCP server waiting for Client on port 5000")
while True:
# List Game Server
listResponse = list_game_servers()
if not 'GameServers' in listResponse or not any(server['GameServerId'] == GameServerId for server in listResponse['GameServers']):
# Register Game Server to Game Server Group
register_game_server()
#
# Update Game Server Status "AVAILABLE"
update_game_server(True)
#
client_socket, address = server_socket.accept()
print("I got connection from ", address)
# Update Game Server Status "UTILIZED"
update_game_server(False)
#
while True:
data = client_socket.recv(512).decode()
print("RECEIVED:", data)
if(data == 'q' or data == 'Q'):
client_socket.close()
break
else:
client_socket.send(data.encode())
# Deregister Game Server
deregister_game_server()
#
server_socket.close()
print("SOCKET Closed... End")