瀏覽代碼

refactor core

Darien Raymond 8 年之前
父節點
當前提交
a32063dfb5
共有 5 個文件被更改,包括 27 次插入8 次删除
  1. 3 0
      config.proto
  2. 2 1
      core.go
  3. 5 3
      loader.go
  4. 15 2
      v2ray.go
  5. 2 2
      v2ray_test.go

+ 3 - 0
config.proto

@@ -16,6 +16,7 @@ enum ConfigFormat {
   JSON = 1;
 }
 
+// Master config of V2Ray. V2Ray Core takes this config as input and functions accordingly.
 message Config {
   // Inbound handler configurations. Must have at least one item.
   repeated v2ray.core.app.proxyman.InboundHandlerConfig inbound = 1;
@@ -27,5 +28,7 @@ message Config {
 
   // App configuration. Must be one in the app directory.
   repeated v2ray.core.common.serial.TypedMessage app = 4;
+
+  // Transport settings.
   v2ray.core.transport.Config transport = 5;
 }

+ 2 - 1
core.go

@@ -19,8 +19,9 @@ func Version() string {
 	return version
 }
 
+// PrintVersion prints current version into console.
 func PrintVersion() {
-	fmt.Printf("V2Ray %s (%s) %s%s", version, codename, build, platform.LineSeparator())
+	fmt.Printf("V2Ray %s (%s) %s%s", Version(), codename, build, platform.LineSeparator())
 	fmt.Printf("%s%s", intro, platform.LineSeparator())
 }
 

+ 5 - 3
loader.go

@@ -5,19 +5,21 @@ import (
 	"io/ioutil"
 
 	"github.com/golang/protobuf/proto"
-
 	"v2ray.com/core/common/errors"
 )
 
+// ConfigLoader is an utility to load V2Ray config from external source.
 type ConfigLoader func(input io.Reader) (*Config, error)
 
 var configLoaderCache = make(map[ConfigFormat]ConfigLoader)
 
+// RegisterConfigLoader add a new ConfigLoader.
 func RegisterConfigLoader(format ConfigFormat, loader ConfigLoader) error {
 	configLoaderCache[format] = loader
 	return nil
 }
 
+// LoadConfig loads config with given format from given source.
 func LoadConfig(format ConfigFormat, input io.Reader) (*Config, error) {
 	loader, found := configLoaderCache[format]
 	if !found {
@@ -26,7 +28,7 @@ func LoadConfig(format ConfigFormat, input io.Reader) (*Config, error) {
 	return loader(input)
 }
 
-func LoadProtobufConfig(input io.Reader) (*Config, error) {
+func loadProtobufConfig(input io.Reader) (*Config, error) {
 	config := new(Config)
 	data, _ := ioutil.ReadAll(input)
 	if err := proto.Unmarshal(data, config); err != nil {
@@ -36,5 +38,5 @@ func LoadProtobufConfig(input io.Reader) (*Config, error) {
 }
 
 func init() {
-	RegisterConfigLoader(ConfigFormat_Protobuf, LoadProtobufConfig)
+	RegisterConfigLoader(ConfigFormat_Protobuf, loadProtobufConfig)
 }

+ 15 - 2
v2ray.go

@@ -11,11 +11,26 @@ import (
 	"v2ray.com/core/common/net"
 )
 
+// Server is an instance of V2Ray. At any time, there must be at most one Server instance running.
+type Server interface {
+	// Start starts the V2Ray server, and return any error during the process.
+	// In the case of any errors, the state of the server is unpredicatable.
+	Start() error
+
+	// Close closes the V2Ray server. All inbound and outbound connections will be closed immediately.
+	Close()
+}
+
 // Point shell of V2Ray.
 type Point struct {
 	space app.Space
 }
 
+// New creates a new V2Ray server with given config.
+func New(config *Config) (Server, error) {
+	return NewPoint(config)
+}
+
 // NewPoint returns a new Point server based on given configuration.
 // The server is not started at this point.
 func NewPoint(config *Config) (*Point, error) {
@@ -125,8 +140,6 @@ func (v *Point) Close() {
 	v.space.Close()
 }
 
-// Start starts the Point server, and return any error during the process.
-// In the case of any errors, the state of the server is unpredicatable.
 func (v *Point) Start() error {
 	if err := v.space.Start(); err != nil {
 		return err

+ 2 - 2
v2ray_test.go

@@ -58,8 +58,8 @@ func TestV2RayClose(t *testing.T) {
 		},
 	}
 
-	point, err := NewPoint(config)
+	server, err := New(config)
 	assert.Error(err).IsNil()
 
-	point.Close()
+	server.Close()
 }