wa-lang.org/wazero@v1.0.2/imports/proxywasm/_proxytest/network.go (about)

     1  // Copyright 2020-2021 Tetrate
     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 proxytest
    16  
    17  import (
    18  	"log"
    19  
    20  	"wa-lang.org/wazero/imports/proxywasm/internal"
    21  	"wa-lang.org/wazero/imports/proxywasm/types"
    22  )
    23  
    24  type networkHostEmulator struct {
    25  	streamStates map[uint32]*streamState
    26  }
    27  
    28  type streamState struct {
    29  	upstream, downstream []byte
    30  }
    31  
    32  func newNetworkHostEmulator() *networkHostEmulator {
    33  	host := &networkHostEmulator{
    34  		streamStates: map[uint32]*streamState{},
    35  	}
    36  
    37  	return host
    38  }
    39  
    40  // impl internal.ProxyWasmHost: delegated from hostEmulator
    41  func (n *networkHostEmulator) networkHostEmulatorProxyGetBufferBytes(bt internal.BufferType, start int, maxSize int,
    42  	returnBufferData **byte, returnBufferSize *int) internal.Status {
    43  
    44  	active := internal.VMStateGetActiveContextID()
    45  	stream := n.streamStates[active]
    46  	var buf []byte
    47  	switch bt {
    48  	case internal.BufferTypeUpstreamData:
    49  		buf = stream.upstream
    50  	case internal.BufferTypeDownstreamData:
    51  		buf = stream.downstream
    52  	default:
    53  		panic("unreachable: maybe a bug in this host emulation or SDK")
    54  	}
    55  
    56  	if len(buf) == 0 {
    57  		return internal.StatusNotFound
    58  	} else if start >= len(buf) {
    59  		log.Printf("start index out of range: %d (start) >= %d ", start, len(buf))
    60  		return internal.StatusBadArgument
    61  	}
    62  
    63  	*returnBufferData = &buf[start]
    64  	if maxSize > len(buf)-start {
    65  		*returnBufferSize = len(buf) - start
    66  	} else {
    67  		*returnBufferSize = maxSize
    68  	}
    69  	return internal.StatusOK
    70  }
    71  
    72  // impl HostEmulator
    73  func (n *networkHostEmulator) CallOnUpstreamData(contextID uint32, data []byte) types.Action {
    74  	stream, ok := n.streamStates[contextID]
    75  	if !ok {
    76  		log.Fatalf("invalid context id: %d", contextID)
    77  	}
    78  
    79  	if len(data) > 0 {
    80  		stream.upstream = append(stream.upstream, data...)
    81  	}
    82  
    83  	action := internal.ProxyOnUpstreamData(contextID, len(stream.upstream), false)
    84  	switch action {
    85  	case types.ActionPause:
    86  	case types.ActionContinue:
    87  		stream.upstream = []byte{}
    88  	default:
    89  		log.Fatalf("invalid action type: %d", action)
    90  	}
    91  	return action
    92  }
    93  
    94  // impl HostEmulator
    95  func (n *networkHostEmulator) CallOnDownstreamData(contextID uint32, data []byte) types.Action {
    96  	stream, ok := n.streamStates[contextID]
    97  	if !ok {
    98  		log.Fatalf("invalid context id: %d", contextID)
    99  	}
   100  	if len(data) > 0 {
   101  		stream.downstream = append(stream.downstream, data...)
   102  	}
   103  
   104  	action := internal.ProxyOnDownstreamData(contextID, len(stream.downstream), false)
   105  	switch action {
   106  	case types.ActionPause:
   107  	case types.ActionContinue:
   108  		stream.downstream = []byte{}
   109  	default:
   110  		log.Fatalf("invalid action type: %d", action)
   111  	}
   112  	return action
   113  }
   114  
   115  // impl HostEmulator
   116  func (n *networkHostEmulator) InitializeConnection() (contextID uint32, action types.Action) {
   117  	contextID = getNextContextID()
   118  	internal.ProxyOnContextCreate(contextID, PluginContextID)
   119  	action = internal.ProxyOnNewConnection(contextID)
   120  	n.streamStates[contextID] = &streamState{}
   121  	return
   122  }
   123  
   124  // impl HostEmulator
   125  func (n *networkHostEmulator) CloseUpstreamConnection(contextID uint32) {
   126  	internal.ProxyOnUpstreamConnectionClose(contextID, types.PeerTypeLocal) // peerType will be removed in the next ABI
   127  }
   128  
   129  // impl HostEmulator
   130  func (n *networkHostEmulator) CloseDownstreamConnection(contextID uint32) {
   131  	internal.ProxyOnDownstreamConnectionClose(contextID, types.PeerTypeLocal) // peerType will be removed in the next ABI
   132  }
   133  
   134  // impl HostEmulator
   135  func (n *networkHostEmulator) CompleteConnection(contextID uint32) {
   136  	internal.ProxyOnLog(contextID)
   137  	internal.ProxyOnDelete(contextID)
   138  	delete(n.streamStates, contextID)
   139  }