-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4340467
commit 8b0abd8
Showing
15 changed files
with
168 additions
and
298 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
package server | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package server | ||
|
||
import ( | ||
"context" | ||
"github.com/kercylan98/minotaur/server" | ||
"github.com/kercylan98/minotaur/vivid" | ||
) | ||
|
||
// New 创建一个新的服务器 | ||
func New() *Server { | ||
return &Server{} | ||
} | ||
|
||
type Server struct { | ||
ctx context.Context | ||
network server.Network | ||
} | ||
|
||
func (s *Server) OnPreStart(ctx vivid.ActorContext) (err error) { | ||
if err = s.network.OnSetup(s.ctx, nil); err != nil { | ||
return | ||
} | ||
|
||
ctx.Future(func() vivid.Message { | ||
return s.network.OnRun | ||
}) | ||
return | ||
} | ||
|
||
func (s *Server) OnReceived(ctx vivid.MessageContext) (err error) { | ||
switch m := ctx.GetMessage().(type) { | ||
case error: | ||
if err = s.network.OnShutdown(); err != nil { | ||
panic(err) | ||
} | ||
ctx.NotifyTerminated(m) | ||
} | ||
|
||
return | ||
} | ||
|
||
func (s *Server) OnDestroy(ctx vivid.ActorContext) (err error) { | ||
|
||
} | ||
|
||
func (s *Server) OnSaveSnapshot(ctx vivid.ActorContext) (snapshot []byte, err error) { | ||
|
||
} | ||
|
||
func (s *Server) OnRecoverSnapshot(ctx vivid.ActorContext, snapshot []byte) (err error) { | ||
|
||
} | ||
|
||
func (s *Server) OnChildTerminated(ctx vivid.ActorContext, child vivid.ActorTerminatedContext) { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package actors | ||
|
||
func NewActorTerminatedContext(c *actorCore, v ...Message) *ActorTerminatedContext { | ||
return &ActorTerminatedContext{ | ||
core: c, | ||
terminatedMessages: v, | ||
} | ||
} | ||
|
||
type ActorTerminatedContext struct { | ||
core *actorCore // Actor 核心 | ||
terminatedMessages []Message // 销毁消息 | ||
cancelTerminate bool // 是否取消销毁 | ||
} | ||
|
||
func (c *ActorTerminatedContext) GetActorId() ActorId { | ||
return c.core.id | ||
} | ||
|
||
func (c *ActorTerminatedContext) GetActor() any { | ||
return c.core.Actor | ||
} | ||
|
||
func (c *ActorTerminatedContext) HasTerminatedMessage() bool { | ||
return len(c.terminatedMessages) > 0 | ||
} | ||
|
||
func (c *ActorTerminatedContext) GetTerminatedMessage() Message { | ||
if len(c.terminatedMessages) == 1 { | ||
return c.terminatedMessages[0] | ||
} else { | ||
return c.terminatedMessages | ||
} | ||
} | ||
|
||
func (c *ActorTerminatedContext) Restart() error { | ||
c.core.restartNum++ | ||
if _, err := c.core.restart(false); err != nil { | ||
return err | ||
} | ||
c.core.restartNum = 0 | ||
c.cancelTerminate = true | ||
return nil | ||
} | ||
|
||
func (c *ActorTerminatedContext) Recover() error { | ||
c.cancelTerminate = true | ||
return nil | ||
} | ||
|
||
func (c *ActorTerminatedContext) IsPreStart() bool { | ||
return c.core.state == actorContextStatePreStart | ||
} | ||
|
||
func (c *ActorTerminatedContext) GetRestartNum() int { | ||
return c.core.restartNum | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package vivid | ||
|
||
type Receiver interface { | ||
Tell(v Message, opts ...MessageOption) error | ||
Ask(v Message, opts ...MessageOption) (any, error) | ||
} | ||
|
||
type Future interface { | ||
// IsCompleted 判断 Future 是否已完成 | ||
IsCompleted() bool | ||
} | ||
|
||
// NewFuture 创建一个新的 Future 对象,该对象用于异步获取 Actor 的返回值 | ||
func NewFuture(ctx ActorContext, handler func() Message) Future { | ||
f := &future{ | ||
ctx: ctx, | ||
} | ||
c := ctx.(*actorContext) | ||
var h = func() { | ||
m := handler() | ||
f.done = true | ||
c.core.Tell(m) | ||
} | ||
if err := c.system.gp.Submit(h); err != nil { | ||
go h() | ||
} | ||
return f | ||
} | ||
|
||
type future struct { | ||
ctx ActorContext | ||
done bool | ||
} | ||
|
||
func (f *future) IsCompleted() bool { | ||
return f.done | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.