<commit_before>package openfl.net;


import haxe.io.Bytes;
import haxe.io.BytesBuffer;
import haxe.io.Error;
import haxe.Timer;
import openfl.errors.IOError;
import openfl.errors.SecurityError;
import openfl.events.Event;
import openfl.events.EventDispatcher;
import openfl.events.IOErrorEvent;
import openfl.events.ProgressEvent;
import openfl.utils.ByteArray;
import openfl.utils.Endian;
import openfl.utils.IDataInput;
import openfl.utils.IDataOutput;
import openfl.Lib;

#if (js && html5)
import js.html.ArrayBuffer;
#end

#if sys
import sys.net.Host;
import sys.net.Socket in SysSocket;
#end


class Socket extends EventDispatcher implements IDataInput implements IDataOutput {
	
	
	public var bytesAvailable (get, never):Int;
	public var bytesPending (get, never):Int;
	public var connected (get, never):Bool;
	public var objectEncoding:UInt;
	public var secure:Bool;
	public var timeout:Int;
	public var endian (get, set):Endian;
	
	private var __buffer:Bytes;
	private var __connected:Bool;
	private var __endian:Endian;
	private var __host:String;
	private var __input:ByteArray;
	private var __inputBuffer:ByteArray;
	private var __output:ByteArray;
	private var __port:Int;
	private var __socket:#if sys SysSocket #else Dynamic #end;
	private var __timestamp:Float;
	
	
	public function new (host:String = null, port:Int = 0) {
		
		super ();
		
		endian = Endian.BIG_ENDIAN;
		timeout = 20000;
		
		__buffer = Bytes.alloc (4096);
		
		if (port > 0 && port < 65535) {
			
			connect (host, port);
			
		}
		
	}
	
	
	public function connect (host:String = null, port:Int = 0):Void {
		
		if (__socket != null) {
			
			close ();
			
		}
		
		if (port < 0 || port > 65535) {
			
			throw new SecurityError ("Invalid socket port number specified.");
			
		}
		
		#if (js && html5)
		
		__timestamp = Timer.stamp ();
		
		#else
		
		var h:Host = null;
		
		try {
			
			h = new Host (host);
			
		} catch (e:Dynamic) {
			
			dispatchEvent (new IOErrorEvent (IOErrorEvent.IO_ERROR, true, false, "Invalid host"));
			return;
			
		}
		
		__timestamp = Sys.time ();
		
		#end
		
		__host = host;
		__port = port;
		
		__output = new ByteArray ();
		__output.endian = __endian;
		
		__input = new ByteArray ();
		__input.endian = __endian;
		
		#if (js && html5)
		__inputBuffer = new ByteArray ();
		__inputBuffer.endian = __endian;
		#end
		
		#if (js && html5)
		
		var schema = secure ? "wss" : "ws";
		var urlReg = ~/^(.*:\/\/)?([A-Za-z0-9\-\.]+)\/?(.*)/g;
		urlReg.match (host);
		var __webHost = urlReg.matched (2);
		var __webPath = urlReg.matched (3);
		
		__socket = untyped __js__("new WebSocket(schema + \"://\" + __webHost + \":\" + port + \"/\" + __webPath)");
		
		__socket.onopen = socket_onOpen;<commit_msg>Set secure WebSocket if page is served with HTTPS<commit_after>package openfl.net;


import haxe.io.Bytes;
import haxe.io.BytesBuffer;
import haxe.io.Error;
import haxe.Timer;
import openfl.errors.IOError;
import openfl.errors.SecurityError;
import openfl.events.Event;
import openfl.events.EventDispatcher;
import openfl.events.IOErrorEvent;
import openfl.events.ProgressEvent;
import openfl.utils.ByteArray;
import openfl.utils.Endian;
import openfl.utils.IDataInput;
import openfl.utils.IDataOutput;
import openfl.Lib;

#if (js && html5)
import js.html.ArrayBuffer;
import js.Browser;
#end

#if sys
import sys.net.Host;
import sys.net.Socket in SysSocket;
#end


class Socket extends EventDispatcher implements IDataInput implements IDataOutput {
	
	
	public var bytesAvailable (get, never):Int;
	public var bytesPending (get, never):Int;
	public var connected (get, never):Bool;
	public var objectEncoding:UInt;
	public var secure:Bool;
	public var timeout:Int;
	public var endian (get, set):Endian;
	
	private var __buffer:Bytes;
	private var __connected:Bool;
	private var __endian:Endian;
	private var __host:String;
	private var __input:ByteArray;
	private var __inputBuffer:ByteArray;
	private var __output:ByteArray;
	private var __port:Int;
	private var __socket:#if sys SysSocket #else Dynamic #end;
	private var __timestamp:Float;
	
	
	public function new (host:String = null, port:Int = 0) {
		
		super ();
		
		endian = Endian.BIG_ENDIAN;
		timeout = 20000;
		
		__buffer = Bytes.alloc (4096);
		
		if (port > 0 && port < 65535) {
			
			connect (host, port);
			
		}
		
	}
	
	
	public function connect (host:String = null, port:Int = 0):Void {
		
		if (__socket != null) {
			
			close ();
			
		}
		
		if (port < 0 || port > 65535) {
			
			throw new SecurityError ("Invalid socket port number specified.");
			
		}
		
		#if (js && html5)
		
		__timestamp = Timer.stamp ();
		
		#else
		
		var h:Host = null;
		
		try {
			
			h = new Host (host);
			
		} catch (e:Dynamic) {
			
			dispatchEvent (new IOErrorEvent (IOErrorEvent.IO_ERROR, true, false, "Invalid host"));
			return;
			
		}
		
		__timestamp = Sys.time ();
		
		#end
		
		__host = host;
		__port = port;
		
		__output = new ByteArray ();
		__output.endian = __endian;
		
		__input = new ByteArray ();
		__input.endian = __endian;
		
		#if (js && html5)
		__inputBuffer = new ByteArray ();
		__inputBuffer.endian = __endian;
		#end
		
		#if (js && html5)
		
		if (Browser.location.protocol == "https:") {
			
			secure = true;
			
		}
		
		var schema = secure ? "wss" : "ws";
		var urlReg = ~/^(.*:\/\/)?([A-Za-z0-9\-\.]+)\/?(.*)/g;
		urlReg.match (host);
		var __webHost = urlReg.matched (2);
		var __webPath = urlReg.matched (3);
		
		__socket = untyped __js__("new WebSocket(schema + \"://\" + __webHost + \":\" + port + \"/\" + __webPath)");
		
		__socket.onopen = socket_onOpen;
{'fraction_non_alphanumeric': 0.1282418172062243, 'fraction_numerical': 0.01108925058129136, 'mean_word_length': 3.3248259860788862, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 7, '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': '194063', 'n_tokens_mistral': 2270, 'n_tokens_neox': 1876, 'n_words': 508}