Exemplo n.º 1
0
func (this *KCPConfig) Build() (*loader.TypedSettings, error) {
	config := new(kcp.Config)

	if this.Mtu != nil {
		mtu := *this.Mtu
		if mtu < 576 || mtu > 1460 {
			return nil, fmt.Errorf("KCP|Config: Invalid MTU size: %d", mtu)
		}
		config.Mtu = &kcp.MTU{Value: mtu}
	}
	if this.Tti != nil {
		tti := *this.Tti
		if tti < 10 || tti > 100 {
			return nil, fmt.Errorf("KCP|Config: Invalid TTI: %d", tti)
		}
		config.Tti = &kcp.TTI{Value: tti}
	}
	if this.UpCap != nil {
		config.UplinkCapacity = &kcp.UplinkCapacity{Value: *this.UpCap}
	}
	if this.DownCap != nil {
		config.DownlinkCapacity = &kcp.DownlinkCapacity{Value: *this.DownCap}
	}
	if this.Congestion != nil {
		config.Congestion = *this.Congestion
	}
	if this.ReadBufferSize != nil {
		size := *this.ReadBufferSize
		if size > 0 {
			config.ReadBuffer = &kcp.ReadBuffer{Size: size * 1024 * 1024}
		} else {
			config.ReadBuffer = &kcp.ReadBuffer{Size: 512 * 1024}
		}
	}
	if this.WriteBufferSize != nil {
		size := *this.WriteBufferSize
		if size > 0 {
			config.WriteBuffer = &kcp.WriteBuffer{Size: size * 1024 * 1024}
		} else {
			config.WriteBuffer = &kcp.WriteBuffer{Size: 512 * 1024}
		}
	}
	if len(this.HeaderConfig) > 0 {
		headerConfig, _, err := kcpHeaderLoader.Load(this.HeaderConfig)
		if err != nil {
			return nil, errors.New("KCP|Config: Failed to parse header config: " + err.Error())
		}
		ts, err := headerConfig.(Buildable).Build()
		if err != nil {
			return nil, errors.New("Failed to get KCP authenticator config: " + err.Error())
		}
		config.HeaderConfig = ts
	}

	return loader.NewTypedSettings(config), nil
}
Exemplo n.º 2
0
func (v *KCPConfig) Build() (*serial.TypedMessage, error) {
	config := new(kcp.Config)

	if v.Mtu != nil {
		mtu := *v.Mtu
		if mtu < 576 || mtu > 1460 {
			return nil, errors.Format("KCP|Config: Invalid MTU size: %d", mtu)
		}
		config.Mtu = &kcp.MTU{Value: mtu}
	}
	if v.Tti != nil {
		tti := *v.Tti
		if tti < 10 || tti > 100 {
			return nil, errors.Format("KCP|Config: Invalid TTI: %d", tti)
		}
		config.Tti = &kcp.TTI{Value: tti}
	}
	if v.UpCap != nil {
		config.UplinkCapacity = &kcp.UplinkCapacity{Value: *v.UpCap}
	}
	if v.DownCap != nil {
		config.DownlinkCapacity = &kcp.DownlinkCapacity{Value: *v.DownCap}
	}
	if v.Congestion != nil {
		config.Congestion = *v.Congestion
	}
	if v.ReadBufferSize != nil {
		size := *v.ReadBufferSize
		if size > 0 {
			config.ReadBuffer = &kcp.ReadBuffer{Size: size * 1024 * 1024}
		} else {
			config.ReadBuffer = &kcp.ReadBuffer{Size: 512 * 1024}
		}
	}
	if v.WriteBufferSize != nil {
		size := *v.WriteBufferSize
		if size > 0 {
			config.WriteBuffer = &kcp.WriteBuffer{Size: size * 1024 * 1024}
		} else {
			config.WriteBuffer = &kcp.WriteBuffer{Size: 512 * 1024}
		}
	}
	if len(v.HeaderConfig) > 0 {
		headerConfig, _, err := kcpHeaderLoader.Load(v.HeaderConfig)
		if err != nil {
			return nil, errors.Base(err).Message("Invalid mKCP header config.")
		}
		ts, err := headerConfig.(Buildable).Build()
		if err != nil {
			return nil, errors.Base(err).Message("Invalid mKCP header config.")
		}
		config.HeaderConfig = ts
	}

	return serial.ToTypedMessage(config), nil
}