Files
go3270/transactions.go
T
Matthew R. Wilson b6fa48f37f Alternate screen size support.
This is a relatively big new feature, which introduces small breaking changes
to the API:

 * NegotiateTelnet() now returns (DevInfo, error) instead of just error.  The
   function signature for the Tx type now has one additional DevInfo parameter.
 * RunTransactions() requires one additional parameter, the DevInfo (which is
   allowed to be nil).

Otherwise, existing behavior continues to work as-is and everything still
defaults to operating on the default 24x80 terminal size.

See example5 for a larger-terminal-aware application.

Pending some time to make sure no new bugs cropped up in this release and that
the alternate screen size support works in the wild, I anticipate this will
become the v1.0.0 release of go3270.
2025-06-29 12:58:18 -07:00

58 lines
2.0 KiB
Go

// This file is part of https://github.com/racingmars/go3270/
// Copyright 2025 by Matthew R. Wilson, licensed under the MIT license.
// See LICENSE in the project root for license information.
package go3270
import "net"
// Tx is a function that serves as one transaction in a go3270 application.
// The Tx function is called with the network connection to the client, the
// DevInfo for use with alternate screen writes, and a "data" value provided
// by the previous transaction. Tx functions return the next transaction to
// run (or nil to indicate the RunTransactions() function should terminate),
// the data to pass into the next transaction, and any error. If the error is
// non-nil, the RunTransactions() function will terminate and return the err.
// A non-nil error is _not_ passed between transactions, it terminates
// transaction processing.
type Tx func(conn net.Conn, dev DevInfo, data any) (
next Tx, newdata any, err error)
// RunTransactions begins running transaction functions, starting with the
// initial transaction, until a transaction eventually returns nil for the
// next transaction, or until a transaction function returns a non-nil error
// value. data (which may be nil, if the initial transaction does not require
// data) is passed in as the data to the initial transaction.
//
// dev is the DevInfo of the connected client, as obtained from
// NegotiateTelnet(). It is safe to pass a nil DevInfo, in which case all
// transactions will only be able to operate with the default 24x80 screen
// size.
func RunTransactions(conn net.Conn, dev DevInfo, initial Tx,
data any) error {
var next Tx
var err error
next = initial
if dev == nil {
dev = &deviceInfo{rows: 24, cols: 80, termtype: "DEFAULT"}
}
// We run transactions until there isn't a next transaction to run, or
// an error.
for {
next, data, err = next(conn, dev, data)
if err != nil {
// Error means we bail out and return the error to the caller.
return err
}
if next == nil {
// nil next transaction means we're done.
return nil
}
}
}