gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/flipcall/flipcall_example_test.go (about)

     1  // Copyright 2019 The gVisor Authors.
     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 flipcall
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  
    21  	"gvisor.dev/gvisor/pkg/sync"
    22  )
    23  
    24  func Example() {
    25  	const (
    26  		reqPrefix     = "request "
    27  		respPrefix    = "response "
    28  		count         = 3
    29  		maxMessageLen = len(respPrefix) + 1 // 1 digit
    30  	)
    31  
    32  	pwa, err := NewPacketWindowAllocator()
    33  	if err != nil {
    34  		panic(err)
    35  	}
    36  	defer pwa.Destroy()
    37  	pwd, err := pwa.Allocate(PacketWindowLengthForDataCap(uint32(maxMessageLen)))
    38  	if err != nil {
    39  		panic(err)
    40  	}
    41  	var clientEP Endpoint
    42  	if err := clientEP.Init(ClientSide, pwd); err != nil {
    43  		panic(err)
    44  	}
    45  	defer clientEP.Destroy()
    46  	var serverEP Endpoint
    47  	if err := serverEP.Init(ServerSide, pwd); err != nil {
    48  		panic(err)
    49  	}
    50  	defer serverEP.Destroy()
    51  
    52  	var serverRun sync.WaitGroup
    53  	serverRun.Add(1)
    54  	go func() {
    55  		defer serverRun.Done()
    56  		i := 0
    57  		var buf bytes.Buffer
    58  		// wait for first request
    59  		n, err := serverEP.RecvFirst()
    60  		if err != nil {
    61  			return
    62  		}
    63  		for {
    64  			// read request
    65  			buf.Reset()
    66  			buf.Write(serverEP.Data()[:n])
    67  			fmt.Println(buf.String())
    68  			// write response
    69  			buf.Reset()
    70  			fmt.Fprintf(&buf, "%s%d", respPrefix, i)
    71  			copy(serverEP.Data(), buf.Bytes())
    72  			// send response and wait for next request
    73  			n, err = serverEP.SendRecv(uint32(buf.Len()))
    74  			if err != nil {
    75  				return
    76  			}
    77  			i++
    78  		}
    79  	}()
    80  	defer func() {
    81  		serverEP.Shutdown()
    82  		serverRun.Wait()
    83  	}()
    84  
    85  	// establish connection as client
    86  	if err := clientEP.Connect(); err != nil {
    87  		panic(err)
    88  	}
    89  	var buf bytes.Buffer
    90  	for i := 0; i < count; i++ {
    91  		// write request
    92  		buf.Reset()
    93  		fmt.Fprintf(&buf, "%s%d", reqPrefix, i)
    94  		copy(clientEP.Data(), buf.Bytes())
    95  		// send request and wait for response
    96  		n, err := clientEP.SendRecv(uint32(buf.Len()))
    97  		if err != nil {
    98  			panic(err)
    99  		}
   100  		// read response
   101  		buf.Reset()
   102  		buf.Write(clientEP.Data()[:n])
   103  		fmt.Println(buf.String())
   104  	}
   105  
   106  	// Output:
   107  	// request 0
   108  	// response 0
   109  	// request 1
   110  	// response 1
   111  	// request 2
   112  	// response 2
   113  }