Advertisement
Nikoh77

Untitled

Mar 14th, 2023
1,924
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends Node
  2.  
  3. var client: StreamPeerTCP = StreamPeerTCP.new()
  4. var host: String = 'smtp.gmail.com'
  5. var port: int = 25
  6. var _status: int
  7. const RECONNECT_TIMEOUT: float = 3.0
  8.  
  9. signal connected
  10. signal data
  11. signal disconnected
  12. signal error
  13.  
  14. func _ready():
  15.     _status = client.get_status()
  16.     prints('status:', _status)
  17.     self.connected.connect(self._handle_client_connected)
  18.     self.disconnected.connect(self._handle_client_disconnected)
  19.     self.error.connect(self._handle_client_error)
  20.     self.data.connect(self._handle_client_data)
  21.     connect_to_host(host, port)
  22.    
  23. func _process(delta):
  24.     var new_status: int = client.get_status()
  25.     if new_status != _status:
  26.         _status = new_status
  27.         match _status:
  28.             client.STATUS_NONE:
  29.                 print("Disconnected from host.")
  30.                 emit_signal("disconnected")
  31.             client.STATUS_CONNECTING:
  32.                 print("Connecting to host.")
  33.             client.STATUS_CONNECTED:
  34.                 print("Connected to host.")
  35.                 emit_signal("connected")
  36.             client.STATUS_ERROR:
  37.                 print("Error with socket stream.")
  38.                 emit_signal("error")
  39.  
  40.     if _status == client.STATUS_CONNECTED:
  41.         var available_bytes: int = client.get_available_bytes()
  42.         if available_bytes > 0:
  43.             print("available bytes: ", available_bytes)
  44.             var data: Array = client.get_partial_data(available_bytes)
  45.             # Check for read error.
  46.             if data[0] != OK:
  47.                 print("Error getting data from stream: ", data[0])
  48.                 emit_signal("error")
  49.             else:
  50.                 emit_signal("data", data[1])
  51.        
  52. func connect_to_host(host: String, port: int) -> void:
  53.     print("Connecting to %s:%d" % [host, port])
  54.     # Reset status so we can tell if it changes to error again.
  55.     _status = client.STATUS_NONE
  56.     if client.connect_to_host(host, port) != OK:
  57.         print("Error connecting to host.")
  58.         emit_signal("error")
  59.  
  60. func send(data: PackedByteArray) -> bool:
  61.     if _status != client.STATUS_CONNECTED:
  62.         print("Error: Stream is not currently connected.")
  63.         return false
  64.     var error: int = client.put_data(data)
  65.     if error != OK:
  66.         print("Error writing to stream: ", error)
  67.         return false
  68.     return true
  69.  
  70. func _connect_after_timeout(timeout: float) -> void:
  71.     await get_tree().create_timer(timeout).timeout # Delay for timeout
  72.     connect_to_host(host, port)
  73.  
  74. func _handle_client_connected() -> void:
  75.     print("Client connected to server.")
  76.  
  77. func _handle_client_data(data: PackedByteArray) -> void:
  78.     print("Client data: ", data.get_string_from_utf8())
  79.     var message: PackedByteArray = [97, 99, 107] # Bytes for "ack" in ASCII
  80.     client.send(message)
  81.  
  82. func _handle_client_disconnected() -> void:
  83.     print("Client disconnected from server.")
  84.     _connect_after_timeout(RECONNECT_TIMEOUT) # Try to reconnect after 3 seconds
  85.  
  86. func _handle_client_error() -> void:
  87.     print("Client error.")
  88.     _connect_after_timeout(RECONNECT_TIMEOUT) # Try to reconnect after 3 seconds
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement