@@ -199,69 +199,44 @@ func (s *Server) serveConn(ctx context.Context, c net.Conn) {
199199 }
200200}
201201
202+ // decodeAnd unmarshals the envelope body into a fresh T and hands it to fn.
203+ // It collapses the decode-then-handle boilerplate that every typed message
204+ // case would otherwise repeat, keeping dispatch a flat one-line-per-type table.
205+ func decodeAnd [T any ](env protocol.Envelope , fn func (T ) error ) error {
206+ var req T
207+ if err := protocol .DecodeBody (env , & req ); err != nil {
208+ return err
209+ }
210+ return fn (req )
211+ }
212+
202213func (s * Server ) dispatch (ctx context.Context , c net.Conn , env protocol.Envelope ) error {
203214 switch env .Type {
204215 case protocol .TypeOpen :
205- var req protocol.OpenReq
206- if err := protocol .DecodeBody (env , & req ); err != nil {
207- return err
208- }
209- return s .handleOpen (c , req )
216+ return decodeAnd (env , func (req protocol.OpenReq ) error { return s .handleOpen (c , req ) })
210217 case protocol .TypeSend :
211- var req protocol.SendReq
212- if err := protocol .DecodeBody (env , & req ); err != nil {
213- return err
214- }
215- return s .handleSend (c , req )
218+ return decodeAnd (env , func (req protocol.SendReq ) error { return s .handleSend (c , req ) })
216219 case protocol .TypeRecv :
217- var req protocol.RecvReq
218- if err := protocol .DecodeBody (env , & req ); err != nil {
219- return err
220- }
221- return s .handleRecv (c , req )
220+ return decodeAnd (env , func (req protocol.RecvReq ) error { return s .handleRecv (c , req ) })
222221 case protocol .TypeClose :
223- var req protocol.CloseReq
224- if err := protocol .DecodeBody (env , & req ); err != nil {
225- return err
226- }
227- return s .handleClose (c , req )
222+ return decodeAnd (env , func (req protocol.CloseReq ) error { return s .handleClose (c , req ) })
228223 case protocol .TypeRegisterPeers :
229224 s .registerWithPeers (context .Background ())
230225 return protocol .WriteMessage (c , protocol .TypeRegisterPeers , map [string ]bool {"ok" : true })
231226 case protocol .TypeVerbsOpen , protocol .TypeVerbsWrite , protocol .TypeVerbsRead , protocol .TypeVerbsClose :
232227 return s .dispatchVerbs (c , env )
233228 case protocol .TypeVerbsQPCreate :
234- var req protocol.VerbsQPCreateReq
235- if err := protocol .DecodeBody (env , & req ); err != nil {
236- return err
237- }
238- return s .handleVerbsQPCreate (req )
229+ return decodeAnd (env , s .handleVerbsQPCreate )
239230 case protocol .TypeVerbsQPConnect :
240- var req protocol.VerbsQPConnectReq
241- if err := protocol .DecodeBody (env , & req ); err != nil {
242- return err
243- }
244- return s .handleVerbsQPConnect (req )
231+ return decodeAnd (env , s .handleVerbsQPConnect )
245232 case protocol .TypeVerbsQPDestroy :
246- var req protocol.VerbsQPDestroyReq
247- if err := protocol .DecodeBody (env , & req ); err != nil {
248- return err
249- }
250- return s .handleVerbsQPDestroy (req )
233+ return decodeAnd (env , s .handleVerbsQPDestroy )
251234 case protocol .TypeVerbsAttach :
252- var req protocol.VerbsAttachReq
253- if err := protocol .DecodeBody (env , & req ); err != nil {
254- return err
255- }
256- return s .handleVerbsAttach (ctx , c , req )
235+ return decodeAnd (env , func (req protocol.VerbsAttachReq ) error { return s .handleVerbsAttach (ctx , c , req ) })
257236 case protocol .TypeVerbsOp :
258- var op protocol.VerbsOp
259- if err := protocol .DecodeBody (env , & op ); err != nil {
260- return err
261- }
262237 // Egress is fire-and-forget: never write a per-op reply (that would
263238 // serialize the data path and can deadlock the apply thread).
264- return s .routeVerbsEgress ( op )
239+ return decodeAnd ( env , s .routeVerbsEgress )
265240 default :
266241 return protocol .WriteMessage (c , env .Type , map [string ]string {
267242 "error" : fmt .Sprintf ("unknown message type %q" , env .Type ),
0 commit comments