v2ray-core/vpoint.go

51 lines
1.2 KiB
Go
Raw Normal View History

2015-09-05 17:48:38 +02:00
package core
import (
2015-09-06 22:10:42 +02:00
"fmt"
2015-09-05 17:48:38 +02:00
)
// VPoint is an single server in V2Ray system.
2015-09-05 17:48:38 +02:00
type VPoint struct {
2015-09-10 00:50:21 +02:00
config VConfig
ichFactory InboundConnectionHandlerFactory
ochFactory OutboundConnectionHandlerFactory
2015-09-05 17:48:38 +02:00
}
2015-09-07 12:00:46 +02:00
// NewVPoint returns a new VPoint server based on given configuration.
// The server is not started at this point.
2015-09-05 17:48:38 +02:00
func NewVPoint(config *VConfig) (*VPoint, error) {
2015-09-07 12:00:46 +02:00
var vpoint = new(VPoint)
vpoint.config = *config
2015-09-06 22:10:42 +02:00
return vpoint, nil
2015-09-05 17:48:38 +02:00
}
2015-09-10 00:50:21 +02:00
type InboundConnectionHandlerFactory interface {
Create(vPoint *VPoint) (InboundConnectionHandler, error)
}
type InboundConnectionHandler interface {
2015-09-06 22:10:42 +02:00
Listen(port uint16) error
2015-09-05 17:48:38 +02:00
}
2015-09-10 00:50:21 +02:00
type OutboundConnectionHandlerFactory interface {
Create(vPoint *VPoint) (OutboundConnectionHandler, error)
}
type OutboundConnectionHandler interface {
Start(vray *OutboundVRay) error
}
2015-09-07 12:00:46 +02:00
// Start starts the VPoint server, and return any error during the process.
// In the case of any errors, the state of the server is unpredicatable.
2015-09-05 17:48:38 +02:00
func (vp *VPoint) Start() error {
2015-09-06 22:10:42 +02:00
if vp.config.Port <= 0 {
return fmt.Errorf("Invalid port %d", vp.config.Port)
}
2015-09-10 00:50:21 +02:00
inboundConnectionHandler, err := vp.ichFactory.Create(vp)
if err != nil {
return err
}
err = inboundConnectionHandler.Listen(vp.config.Port)
2015-09-06 22:10:42 +02:00
return nil
}