<commit_before>import socket, errno
import sys
import os
import json
import time
from joueur.serializer import serialize, deserialize
import joueur.error_code as error_code
from joueur.game_manager import GameManager
import joueur.ansi_color_coder as color

EOT_CHAR = chr(4)

# Client: A singlton module that talks to the server receiving game information and sending commands to execute. Clients perform no game logic
class _Client:
    socket = None

_client = _Client()

def connect(server='localhost', port=3000, print_io=False):
    _client.server = server
    _client.port = int(port)

    _client._print_io = print_io
    _client._received_buffer = ""
    _client._events_stack = []
    _client._buffer_size = 1024
    _client._timeout_time = 1.0

    print(color.text("cyan") + "Connecting to:", _client.server + ":" + str(_client.port) + color.reset())

    try:
        _client.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        _client.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # stupid Windows
        _client.socket.settimeout(_client._timeout_time) # so the blocking on recv doesn't hang forever and other system interupts (e.g. keyboard) can be handled
        _client.socket.connect((_client.server, _client.port))
    except socket.error as e:
        error_code.handle_error(error_code.COULD_NOT_CONNECT, e, "Could not connect to " + _client.server + ":" + str(_client.port))

def setup(game, ai, manager):
    _client.game = game
    _client.ai = ai
    _client.manager = manager

def _send_raw(string):
    if _client._print_io:
        print(color.text("magenta") + "TO SERVER --> " + str(string) + color.reset())
    _client.socket.send(string)

 ## sends the server an event via socket
def send(event, data):
    _send_raw(
        (json.dumps({
            'sentTime': int(time.time()),
            'event': event,
            'data': serialize(data)
        })
        + EOT_CHAR).encode('utf-8')
    )

def disconnect(exit_code=None):
    if _client.socket:
        _client.socket.close()

def run_on_server(caller, function_name, args=None):
    send("run", {
        'caller': caller,
        'functionName': function_name,
        'args': args
    })

    ran_data = wait_for_event("ran")
    return deserialize(ran_data, _client.game)

def play():
    wait_for_event(None)

def wait_for_event(event):
    while True:
        wait_for_events()

        while len(_client._events_stack) > 0:
            sent = _client._events_stack.pop()
            if event != None and sent['event'] == event:
                return sent['data']
            else:
                _auto_handle(sent['event'], sent['data'] if 'data' in sent else None)

## loops to check the socket for incoming data and ends once some events get found
def wait_for_events():
    if len(_client._events_stack) > 0:
        return # as we already have events to handle, no need to wait for more

    try:
        while True:
            sent = None
            try:
                sent = _client.socket.recv(_client._buffer_size).decode("utf-8")
            except socket.timeout:
                pass # timed out so keyboard/system interupts can be handled, hence the while true loop above
            except socket.error as e:
                error_code.handle_error(error_code.CANNOT_READ_SOCKET, e, "Error reading socket while waiting for events")
<commit_msg>Handle no data on events<commit_after>import socket, errno
import sys
import os
import json
import time
from joueur.serializer import serialize, deserialize
import joueur.error_code as error_code
from joueur.game_manager import GameManager
import joueur.ansi_color_coder as color

EOT_CHAR = chr(4)

# Client: A singlton module that talks to the server receiving game information and sending commands to execute. Clients perform no game logic
class _Client:
    socket = None

_client = _Client()

def connect(server='localhost', port=3000, print_io=False):
    _client.server = server
    _client.port = int(port)

    _client._print_io = print_io
    _client._received_buffer = ""
    _client._events_stack = []
    _client._buffer_size = 1024
    _client._timeout_time = 1.0

    print(color.text("cyan") + "Connecting to:", _client.server + ":" + str(_client.port) + color.reset())

    try:
        _client.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        _client.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # stupid Windows
        _client.socket.settimeout(_client._timeout_time) # so the blocking on recv doesn't hang forever and other system interupts (e.g. keyboard) can be handled
        _client.socket.connect((_client.server, _client.port))
    except socket.error as e:
        error_code.handle_error(error_code.COULD_NOT_CONNECT, e, "Could not connect to " + _client.server + ":" + str(_client.port))

def setup(game, ai, manager):
    _client.game = game
    _client.ai = ai
    _client.manager = manager

def _send_raw(string):
    if _client._print_io:
        print(color.text("magenta") + "TO SERVER --> " + str(string) + color.reset())
    _client.socket.send(string)

 ## sends the server an event via socket
def send(event, data):
    _send_raw(
        (json.dumps({
            'sentTime': int(time.time()),
            'event': event,
            'data': serialize(data)
        })
        + EOT_CHAR).encode('utf-8')
    )

def disconnect(exit_code=None):
    if _client.socket:
        _client.socket.close()

def run_on_server(caller, function_name, args=None):
    send("run", {
        'caller': caller,
        'functionName': function_name,
        'args': args
    })

    ran_data = wait_for_event("ran")
    return deserialize(ran_data, _client.game)

def play():
    wait_for_event(None)

def wait_for_event(event):
    while True:
        wait_for_events()

        while len(_client._events_stack) > 0:
            sent = _client._events_stack.pop()
            data = sent['data'] if 'data' in sent else None
            if event != None and sent['event'] == event:
                return data
            else:
                _auto_handle(sent['event'], data)

## loops to check the socket for incoming data and ends once some events get found
def wait_for_events():
    if len(_client._events_stack) > 0:
        return # as we already have events to handle, no need to wait for more

    try:
        while True:
            sent = None
            try:
                sent = _client.socket.recv(_client._buffer_size).decode("utf-8")
            except socket.timeout:
                pass # timed out so keyboard/system interupts can be handled, hence the while true loop above
            except socket.error as e:
                error_code.handle_error(error_code.CANNOT_READ_SOCKET, e, "Error reading socket while waiting for events")

{'fraction_non_alphanumeric': 0.10165762065424673, 'fraction_numerical': 0.004694146985477482, 'mean_word_length': 2.8826879271070616, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2798817', 'n_tokens_mistral': 2085, 'n_tokens_neox': 2026, 'n_words': 572}