<commit_before># -*- coding: latin-1 -*-

''' 
 Freeciv - Copyright (C) 2009-2013 - <NAME>   <EMAIL>
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
'''

import socket
import select
from struct import *
from threading import Thread;
import logging
import time

HOST = '127.0.0.1';
logger = logging.getLogger("freeciv-proxy");

# The CivCom handles communication between freeciv-proxy and the Freeciv C server.
class CivCom(Thread):

  def __init__ (self, username, civserverport, civwebserver):
    Thread.__init__(self)
    self.socket = None;
    self.username = username;
    self.civserverport = civserverport;
    self.key = username + str(civserverport);
    self.send_buffer = [];
    self.connect_time = time.time();
    self.civserver_messages = [];
    self.stopped = False;
    self.packet_size = -1;
    self.net_buf = bytearray(0);
    self.header_buf = bytearray(0);
    self.daemon = True;
    self.civwebserver = civwebserver;

  def run(self):
    #setup connection to civserver
    if (logger.isEnabledFor(logging.INFO)):
      logger.info("Start connection to civserver for " + self.username 
                  + " from IP " + self.civwebserver.ip)
    self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    self.socket.setblocking(True);
    self.socket.settimeout(2);
    try:
        self.socket.connect((HOST, self.civserverport))
        self.socket.settimeout(0.01);
    except socket.error as reason:
      self.send_error_to_client("Proxy unable to connect to civserver. Error: %s" % (reason));
      return;

    # send initial login packet to civserver
    self.civserver_messages = [self.civwebserver.loginpacket];
    self.send_packets_to_civserver();

    #receive packets from server
    while 1:
      packet = self.read_from_connection(); 

      if (self.stopped):
        return;
              
      if (packet != None): 
        self.net_buf += packet;
        if (len(self.net_buf) >= self.packet_size 
            and chr(self.net_buf[-2]) != '}' 
            and self.net_buf[-1] != 0):
          logger.error("Invalid packet recieved!");
          logger.info(repr(self.net_buf));

        if (len(self.net_buf) == self.packet_size):
          # valid packet received from freeciv server, send it to client.
          self.send_buffer_append(self.net_buf[:-1]);
          self.packet_size = -1;
          self.net_buf = bytearray(0);
          continue;


      time.sleep(0.01); #prevent max CPU usage in case of error

  def read_from_connection(self):
    try:
      if (self.socket != None and not self.stopped):    
        if (self.packet_size == -1):
          self.header_buf += self.socket.recv(4 - len(self.header_buf));
          if (len(self.header_buf) == 0): 
            self.close_connection();
            return None;
          if (len(self.header_buf) == 4):
            header_pck = unpack('>HH', self.header_buf);
            self.header_buf = bytearray(0);
            self.packet_size = header_pck[0] - 4;
            if (self.packet_size <= 0 or self.packet_size > 32767): 
              logger.error("Invalid packet size.");
          else:
            logger.error("Incomplete packet header: " + repr(self.header_buf));
            return None;

      if (self.socket != None and self.net_buf != None and self.packet_size > 0):
        data = self.socket.recv(self.packet_size - len(self.net_buf));
        if (len(data) == 0):<commit_msg>Remove debugging statements which are not needed.<commit_after># -*- coding: latin-1 -*-

''' 
 Freeciv - Copyright (C) 2009-2013 - <NAME>   <EMAIL>
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
'''

import socket
import select
from struct import *
from threading import Thread;
import logging
import time

HOST = '127.0.0.1';
logger = logging.getLogger("freeciv-proxy");

# The CivCom handles communication between freeciv-proxy and the Freeciv C server.
class CivCom(Thread):

  def __init__ (self, username, civserverport, civwebserver):
    Thread.__init__(self)
    self.socket = None;
    self.username = username;
    self.civserverport = civserverport;
    self.key = username + str(civserverport);
    self.send_buffer = [];
    self.connect_time = time.time();
    self.civserver_messages = [];
    self.stopped = False;
    self.packet_size = -1;
    self.net_buf = bytearray(0);
    self.header_buf = bytearray(0);
    self.daemon = True;
    self.civwebserver = civwebserver;

  def run(self):
    #setup connection to civserver
    if (logger.isEnabledFor(logging.INFO)):
      logger.info("Start connection to civserver for " + self.username 
                  + " from IP " + self.civwebserver.ip)
    self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    self.socket.setblocking(True);
    self.socket.settimeout(2);
    try:
        self.socket.connect((HOST, self.civserverport))
        self.socket.settimeout(0.01);
    except socket.error as reason:
      self.send_error_to_client("Proxy unable to connect to civserver. Error: %s" % (reason));
      return;

    # send initial login packet to civserver
    self.civserver_messages = [self.civwebserver.loginpacket];
    self.send_packets_to_civserver();

    #receive packets from server
    while 1:
      packet = self.read_from_connection(); 

      if (self.stopped):
        return;
              
      if (packet != None): 
        self.net_buf += packet;

        if (len(self.net_buf) == self.packet_size and self.net_buf[-1] == 0):
          # valid packet received from freeciv server, send it to client.
          self.send_buffer_append(self.net_buf[:-1]);
          self.packet_size = -1;
          self.net_buf = bytearray(0);
          continue;


      time.sleep(0.01); #prevent max CPU usage in case of error

  def read_from_connection(self):
    try:
      if (self.socket != None and not self.stopped):    
        if (self.packet_size == -1):
          self.header_buf += self.socket.recv(4 - len(self.header_buf));
          if (len(self.header_buf) == 0): 
            self.close_connection();
            return None;
          if (len(self.header_buf) == 4):
            header_pck = unpack('>HH', self.header_buf);
            self.header_buf = bytearray(0);
            self.packet_size = header_pck[0] - 4;
            if (self.packet_size <= 0 or self.packet_size > 32767): 
              logger.error("Invalid packet size.");
          else:
            # complete header not read yet. return now, and read the rest next time.
            return None;

      if (self.socket != None and self.net_buf != None and self.packet_size > 0):
        data = self.socket.recv(self.packet_size - len(self.net_buf));
        if (len(data) == 0):
{'fraction_non_alphanumeric': 0.10968340177506955, 'fraction_numerical': 0.012584448271294211, 'mean_word_length': 2.624579932789246, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '5967174', 'n_tokens_mistral': 2335, 'n_tokens_neox': 2236, 'n_words': 639}