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

     1  // Copyright 2020 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 backfill 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  func Start(queryServiceAddr string, serverPort int) {
    31  	// Connect to QueryService.
    32  	conn, err := grpc.Dial(queryServiceAddr, grpc.WithInsecure())
    33  
    34  	if err != nil {
    35  		log.Fatalf("Failed to connect to Open Match, got %s", err.Error())
    36  	}
    37  
    38  	defer conn.Close()
    39  
    40  	mmfService := matchFunctionService{
    41  		queryServiceClient: pb.NewQueryServiceClient(conn),
    42  	}
    43  
    44  	// Create and host a new gRPC service on the configured port.
    45  	server := grpc.NewServer()
    46  	pb.RegisterMatchFunctionServer(server, &mmfService)
    47  	ln, err := net.Listen("tcp", fmt.Sprintf(":%d", serverPort))
    48  
    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  
    56  	if err != nil {
    57  		log.Fatalf("gRPC serve failed, got %s", err.Error())
    58  	}
    59  }