open-match.dev/open-match@v1.8.1/examples/functions/golang/soloduel/mmf/server.go (about)

     1  // Copyright 2019 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package mmf provides a sample match function that uses the GRPC harness to set up 1v1 matches.
    16  // This sample is a reference to demonstrate the usage of the GRPC harness and should only be used as
    17  // a starting point for your match function. You will need to modify the
    18  // matchmaking logic in this function based on your game's requirements.
    19  package mmf
    20  
    21  import (
    22  	"fmt"
    23  	"log"
    24  	"net"
    25  
    26  	"google.golang.org/grpc"
    27  	"open-match.dev/open-match/pkg/pb"
    28  )
    29  
    30  // Start creates and starts the Match Function server and also connects to Open
    31  // Match's queryService service. This connection is used at runtime to fetch tickets
    32  // for pools specified in MatchProfile.
    33  func Start(queryServiceAddr string, serverPort int) {
    34  	// Connect to QueryService.
    35  	conn, err := grpc.Dial(queryServiceAddr, grpc.WithInsecure())
    36  	if err != nil {
    37  		log.Fatalf("Failed to connect to Open Match, got %s", err.Error())
    38  	}
    39  	defer conn.Close()
    40  
    41  	mmfService := matchFunctionService{
    42  		queryServiceClient: pb.NewQueryServiceClient(conn),
    43  	}
    44  
    45  	// Create and host a new gRPC service on the configured port.
    46  	server := grpc.NewServer()
    47  	pb.RegisterMatchFunctionServer(server, &mmfService)
    48  	ln, err := net.Listen("tcp", fmt.Sprintf(":%d", serverPort))
    49  	if err != nil {
    50  		log.Fatalf("TCP net listener initialization failed for port %v, got %s", serverPort, err.Error())
    51  	}
    52  
    53  	log.Printf("TCP net listener initialized for port %v", serverPort)
    54  	err = server.Serve(ln)
    55  	if err != nil {
    56  		log.Fatalf("gRPC serve failed, got %s", err.Error())
    57  	}
    58  }