Files
go3270/example4/example4.go
T
Claude 8539da3147 Licence: AGPL-3.0-only, or a commercial licence from the D-Modem Authors
The suite's licence, as D-Modem, bbs, menu and brand: the AGPL-3.0-only
by default, and a commercial licence from the D-Modem Authors for uses
that can't meet its terms (COMMERCIAL-LICENSE.md).  The contributor
agreement grants the licence for both.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B82eRSQaj3bYPQDZU4qf9U
2026-11-07 11:01:37 +00:00

93 lines
2.1 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-only AND MIT
// From https://github.com/racingmars/go3270/: Copyright 2025 by
// Matthew R. Wilson, under the MIT license (LICENSE-MIT in the project
// root). D-Modem's changes: Copyright (C) 2026 D-Modem Authors, under
// the AGPL-3.0-only (LICENSE).
// This example demonstrates using the RunTransactions() approach to
// structuring a go3270 application.
package main
import (
"fmt"
"net"
"sync"
"github.com/D-Modem/go3270"
)
type global struct {
usercount int // number of connected users
countlock sync.Mutex
}
// session is the structure which holds global state for a user session of our
// application. The various Transactions will be methods on this struct.
type session struct {
db DB
user User
// We can also share global state between users
g *global
}
// main creates the mock database and the shared state, then listens on port
// 3270 and handles each connection in its own goroutine.
func main() {
// "Connect" to our database (for this example, it's just a mock in-memory
// database).
dbconn := Connect()
gblstate := new(global)
ln, err := net.Listen("tcp", ":3270")
if err != nil {
panic(err)
}
fmt.Println("LISTENING ON PORT 3270 FOR CONNECTIONS")
fmt.Println("Press Ctrl-C to end server.")
for {
conn, err := ln.Accept()
if err != nil {
panic(err)
}
go handle(conn, dbconn, gblstate)
}
}
// handle is the handler for individual user connections.
func handle(conn net.Conn, db DB, gblstate *global) {
defer conn.Close()
// Create the state for this client's connection
state := session{
db: db,
g: gblstate,
}
// Add to the global user counter
gblstate.countlock.Lock()
gblstate.usercount++
gblstate.countlock.Unlock()
// When the session ends, reduce the user count
defer func() {
gblstate.countlock.Lock()
gblstate.usercount--
gblstate.countlock.Unlock()
}()
// Always begin new connection by negotiating the telnet options
devinfo, err := go3270.NegotiateTelnet(conn)
if err != nil {
fmt.Println(err)
return
}
err = go3270.RunTransactions(conn, devinfo, state.login, nil)
if err != nil {
fmt.Println(err)
}
}