github.com/xmplusdev/xmcore@v1.8.11-0.20240412132628-5518b55526af/infra/conf/transport.go (about)

     1  package conf
     2  
     3  import (
     4  	"github.com/xmplusdev/xmcore/common/serial"
     5  	"github.com/xmplusdev/xmcore/transport/global"
     6  	"github.com/xmplusdev/xmcore/transport/internet"
     7  )
     8  
     9  type TransportConfig struct {
    10  	TCPConfig         *TCPConfig          `json:"tcpSettings"`
    11  	KCPConfig         *KCPConfig          `json:"kcpSettings"`
    12  	WSConfig          *WebSocketConfig    `json:"wsSettings"`
    13  	HTTPConfig        *HTTPConfig         `json:"httpSettings"`
    14  	DSConfig          *DomainSocketConfig `json:"dsSettings"`
    15  	QUICConfig        *QUICConfig         `json:"quicSettings"`
    16  	GRPCConfig        *GRPCConfig         `json:"grpcSettings"`
    17  	GUNConfig         *GRPCConfig         `json:"gunSettings"`
    18  	HTTPUPGRADEConfig *HttpUpgradeConfig  `json:"httpupgradeSettings"`
    19  }
    20  
    21  // Build implements Buildable.
    22  func (c *TransportConfig) Build() (*global.Config, error) {
    23  	config := new(global.Config)
    24  
    25  	if c.TCPConfig != nil {
    26  		ts, err := c.TCPConfig.Build()
    27  		if err != nil {
    28  			return nil, newError("failed to build TCP config").Base(err).AtError()
    29  		}
    30  		config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
    31  			ProtocolName: "tcp",
    32  			Settings:     serial.ToTypedMessage(ts),
    33  		})
    34  	}
    35  
    36  	if c.KCPConfig != nil {
    37  		ts, err := c.KCPConfig.Build()
    38  		if err != nil {
    39  			return nil, newError("failed to build mKCP config").Base(err).AtError()
    40  		}
    41  		config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
    42  			ProtocolName: "mkcp",
    43  			Settings:     serial.ToTypedMessage(ts),
    44  		})
    45  	}
    46  
    47  	if c.WSConfig != nil {
    48  		ts, err := c.WSConfig.Build()
    49  		if err != nil {
    50  			return nil, newError("failed to build WebSocket config").Base(err)
    51  		}
    52  		config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
    53  			ProtocolName: "websocket",
    54  			Settings:     serial.ToTypedMessage(ts),
    55  		})
    56  	}
    57  
    58  	if c.HTTPConfig != nil {
    59  		ts, err := c.HTTPConfig.Build()
    60  		if err != nil {
    61  			return nil, newError("Failed to build HTTP config.").Base(err)
    62  		}
    63  		config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
    64  			ProtocolName: "http",
    65  			Settings:     serial.ToTypedMessage(ts),
    66  		})
    67  	}
    68  
    69  	if c.DSConfig != nil {
    70  		ds, err := c.DSConfig.Build()
    71  		if err != nil {
    72  			return nil, newError("Failed to build DomainSocket config.").Base(err)
    73  		}
    74  		config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
    75  			ProtocolName: "domainsocket",
    76  			Settings:     serial.ToTypedMessage(ds),
    77  		})
    78  	}
    79  
    80  	if c.QUICConfig != nil {
    81  		qs, err := c.QUICConfig.Build()
    82  		if err != nil {
    83  			return nil, newError("Failed to build QUIC config.").Base(err)
    84  		}
    85  		config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
    86  			ProtocolName: "quic",
    87  			Settings:     serial.ToTypedMessage(qs),
    88  		})
    89  	}
    90  
    91  	if c.GRPCConfig == nil {
    92  		c.GRPCConfig = c.GUNConfig
    93  	}
    94  	if c.GRPCConfig != nil {
    95  		gs, err := c.GRPCConfig.Build()
    96  		if err != nil {
    97  			return nil, newError("Failed to build gRPC config.").Base(err)
    98  		}
    99  		config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
   100  			ProtocolName: "grpc",
   101  			Settings:     serial.ToTypedMessage(gs),
   102  		})
   103  	}
   104  
   105  	if c.HTTPUPGRADEConfig != nil {
   106  		hs, err := c.HTTPUPGRADEConfig.Build()
   107  		if err != nil {
   108  			return nil, newError("failed to build HttpUpgrade config").Base(err)
   109  		}
   110  		config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
   111  			ProtocolName: "httpupgrade",
   112  			Settings:     serial.ToTypedMessage(hs),
   113  		})
   114  	}
   115  
   116  	return config, nil
   117  }