forked from heistp/irtt
-
Notifications
You must be signed in to change notification settings - Fork 1
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
2a351e1
commit 5430db2
Showing
7 changed files
with
1,568 additions
and
5 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
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,41 @@ | ||
package nlmt | ||
|
||
import ( | ||
"net" | ||
"sync" | ||
) | ||
|
||
// NetPrint represents a connection manager | ||
type NetPrint struct { | ||
conn net.Conn | ||
mu sync.Mutex // Mutex for synchronization | ||
} | ||
|
||
// NewConnectionManager creates a new NetPrint and establishes a connection to the given address | ||
func NewNetPrint(destAddr string) (*NetPrint, error) { | ||
conn, err := net.Dial("tcp", destAddr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &NetPrint{conn: conn}, nil | ||
} | ||
|
||
// WriteToConnection writes the provided message to the connection managed by NetPrint | ||
func (cm *NetPrint) WriteToConnection(message string) error { | ||
// Lock the mutex before writing to the connection | ||
cm.mu.Lock() | ||
defer cm.mu.Unlock() | ||
|
||
_, err := cm.conn.Write([]byte(message)) | ||
return err | ||
} | ||
|
||
// CloseConnection closes the connection managed by NetPrint | ||
func (cm *NetPrint) CloseConnection() error { | ||
// Lock the mutex before closing the connection | ||
cm.mu.Lock() | ||
defer cm.mu.Unlock() | ||
|
||
return cm.conn.Close() | ||
} |
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