diff options
author | Runxi Yu <me@runxiyu.org> | 2024-12-08 08:52:23 +0800 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2024-12-08 08:52:23 +0800 |
commit | d201e74fff4e6d82d639d858826bdb4190936c07 (patch) | |
tree | 7e5de8e206bf913fc0bbef5eba81b06fa9c1e06c /servers.go | |
parent | Add basic command handling (diff) | |
download | meseircd-d201e74fff4e6d82d639d858826bdb4190936c07.tar.gz meseircd-d201e74fff4e6d82d639d858826bdb4190936c07.tar.zst meseircd-d201e74fff4e6d82d639d858826bdb4190936c07.zip |
Server and self awareness
Diffstat (limited to 'servers.go')
-rw-r--r-- | servers.go | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/servers.go b/servers.go new file mode 100644 index 0000000..eb91072 --- /dev/null +++ b/servers.go @@ -0,0 +1,43 @@ +package main + +import ( + "net" +) + +type Server struct { + conn *net.Conn + SID [3]byte + Name string +} + +func (server *Server) Send(msg SMsg) error { + return server.SendRaw(msg.ServerSerialize()) +} + +func (server *Server) SendRaw(s string) error { + if server == &self { + return ErrSendToSelf + } + if server.conn == nil { + // TODO: Propagate across mesh + return ErrNotConnectedServer + } + _, err := (*server.conn).Write([]byte(s)) + if err != nil { + // TODO: Should shut down the netFd instead but the stdlib + // doesn't expose a way to do this. + (*server.conn).Close() + return err + } + return nil +} + +func (server Server) ClientSource() string { + return server.Name +} + +func (server Server) ServerSource() string { + return string(server.SID[:]) +} + +var self Server |