<commit_before>﻿using System.Collections;
using System.Collections.Generic;
using System;
using System.IO;
using UnityEngine.Events;
using UnityEngine;
using System.Net;
using Hazel;
using Hazel.Udp;
using MessagePack;
using FlatBuffers;
using HazelTest;

/// <summary>
/// Network manager.
/// </summary>
public class NetworkManager : MonoBehaviour {

	public bool DEBUG = true;

	public int portNumber = 4296;
	public string ipAddress = "127.0.0.1";

    public GameObject CubeOne;

	public struct ReceiveMessageFromGameObject {
        public sbyte MessageType;
        public int GameObjectID;
		public Vector3 GameObjectPos;
		public Quaternion GameObjectRot;
	};

	private static NetworkManager _instance = null;

	//Events
	public delegate void ReceiveMessageUpdate(ReceiveMessageFromGameObject newMessage);
	public static event ReceiveMessageUpdate OnReceiveMessageFromGameObjectUpdate;

	/// <summary>
	/// Send type.
	/// </summary>
	public enum SendType {
		SENDTOALL = 0,
		SENDTOOTHER = 1,
		SENDTOSERVER = 2
	}

	/// <summary>
	/// Packet identifier.
	/// </summary>
	public enum PacketId: sbyte {
		PLAYER_JOIN = 0,
		OBJECT_MOVE = 1
	}

	// Client Data
	Connection serverConn;

	/// <summary>
	/// Gets the instance.
	/// </summary>
	/// <value>The instance.</value>
	public static NetworkManager instance {
		get {
			if (_instance == null) {
				_instance = FindObjectOfType<NetworkManager> ();
				if (_instance == null) {
					GameObject go = new GameObject ();
					go.name = "NetworkManager";
					instance = go.AddComponent<NetworkManager> ();
					DontDestroyOnLoad (go);
				}
				Debug.Assert (_instance != null);
			}
			return _instance;
		}
		set {
		}
	}

	/// <summary>
	/// Awake this instance.
	/// </summary>
	void Awake()
	{
		if(_instance == null)
		{
			_instance = this;
			DontDestroyOnLoad(this.gameObject);
		}
		else
		{
			Destroy(gameObject);
		}
	}
		
	/// <summary>
	/// Start this instance.
	/// </summary>
	void Start() {
		CustomLogger.LogIt ("--- Starting Logging ---");
		Debug.Log("Network idle.");
		StartClient (ipAddress);
		Debug.Log("Network Started.");
		serverConn.SendBytes(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, SendOption.Reliable);
		Debug.Log("Network Trasmitted.");
	}

	/// <summary>
	/// Raises the destroy event.
	/// </summary>
	void OnDestroy() {
		if (serverConn != null) serverConn.Close();
	}

	#region Client
	public void StartClient(string ipAddress) {
		NetworkEndPoint endPoint = new NetworkEndPoint(ipAddress, portNumber);
		serverConn = new UdpClientConnection(endPoint);
		serverConn.DataReceived += HandleDataFromServer;
        serverConn.Disconnected += HandleServerDisconnect;
        Debug.Log("Connecting to " + endPoint);
		serverConn.Connect();
	}

	void HandleDataFromServer(object sender, DataReceivedEventArgs args) {
		Connection connection = (Connection)sender;
		Debug.Log("Received " + args.Bytes.Length + " bytes from server at " + connection.EndPoint.ToString());
        //connection.SendBytes(args.Bytes, args.SendOption);
        ReceiveMessage(args.Bytes);
		args.Recycle();
	}

	void HandleServerDisconnect(object sender, DisconnectedEventArgs args) {
		Connection connection = (Connection)sender;
		Debug.Log("Server connection at " + connection.EndPoint + " lost");
		serverConn = null;
		args.Recycle();
	}

    void OnApplicationQuit()
    {
        Debug.Log("DisConnecting from: " + serverConn.EndPoint.ToString());
        if (serverConn != null) serverConn.Close();
    }
    #endregion

    #region NetworkLogic
    /// <summary>
    /// SendMessage
    /// </summary>
    /// <param name="Type"></param>
    /// <param name="IDObject"></param>
    /// <param name="Pos"></param>
    /// <param name="Rot"></param>
    public void SendMessage(SendType SType, PacketId Type, int IDObject, Vector3 Pos, Quaternion Rot) {
        sbyte TypeBuffer = 0;
        byte STypeBuffer = 0;

        //Choose who to send message
        switch (SType)
        {
            case SendType.SENDTOALL:
                STypeBuffer = 0;
                break;
            case SendType.SENDTOOTHER:
                STypeBuffer = 1;
                break;
            case SendType.SENDTOSERVER:
                STypeBuffer = 2;
                break;
            default:
                STypeBuffer = 0;
                break;<commit_msg>Convert function SendMessage from int to short<commit_after>﻿using System.Collections;
using System.Collections.Generic;
using System;
using System.IO;
using UnityEngine.Events;
using UnityEngine;
using System.Net;
using Hazel;
using Hazel.Udp;
using MessagePack;
using FlatBuffers;
using HazelTest;

/// <summary>
/// Network manager.
/// </summary>
public class NetworkManager : MonoBehaviour {

	public bool DEBUG = true;

	public int portNumber = 4296;
	public string ipAddress = "127.0.0.1";

    public GameObject CubeOne;

	public struct ReceiveMessageFromGameObject {
        public sbyte MessageType;
        public ushort GameObjectID;
		public Vector3 GameObjectPos;
		public Quaternion GameObjectRot;
	};

	private static NetworkManager _instance = null;

	//Events
	public delegate void ReceiveMessageUpdate(ReceiveMessageFromGameObject newMessage);
	public static event ReceiveMessageUpdate OnReceiveMessageFromGameObjectUpdate;

	/// <summary>
	/// Send type.
	/// </summary>
	public enum SendType {
		SENDTOALL = 0,
		SENDTOOTHER = 1,
		SENDTOSERVER = 2
	}

	/// <summary>
	/// Packet identifier.
	/// </summary>
	public enum PacketId: sbyte {
		PLAYER_JOIN = 0,
		OBJECT_MOVE = 1
	}

	// Client Data
	Connection serverConn;

	/// <summary>
	/// Gets the instance.
	/// </summary>
	/// <value>The instance.</value>
	public static NetworkManager instance {
		get {
			if (_instance == null) {
				_instance = FindObjectOfType<NetworkManager> ();
				if (_instance == null) {
					GameObject go = new GameObject ();
					go.name = "NetworkManager";
					instance = go.AddComponent<NetworkManager> ();
					DontDestroyOnLoad (go);
				}
				Debug.Assert (_instance != null);
			}
			return _instance;
		}
		set {
		}
	}

	/// <summary>
	/// Awake this instance.
	/// </summary>
	void Awake()
	{
		if(_instance == null)
		{
			_instance = this;
			DontDestroyOnLoad(this.gameObject);
		}
		else
		{
			Destroy(gameObject);
		}
	}
		
	/// <summary>
	/// Start this instance.
	/// </summary>
	void Start() {
		CustomLogger.LogIt ("--- Starting Logging ---");
		Debug.Log("Network idle.");
		StartClient (ipAddress);
		Debug.Log("Network Started.");
		serverConn.SendBytes(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, SendOption.Reliable);
		Debug.Log("Network Trasmitted.");
	}

	/// <summary>
	/// Raises the destroy event.
	/// </summary>
	void OnDestroy() {
		if (serverConn != null) serverConn.Close();
	}

	#region Client
	public void StartClient(string ipAddress) {
		NetworkEndPoint endPoint = new NetworkEndPoint(ipAddress, portNumber);
		serverConn = new UdpClientConnection(endPoint);
		serverConn.DataReceived += HandleDataFromServer;
        serverConn.Disconnected += HandleServerDisconnect;
        Debug.Log("Connecting to " + endPoint);
		serverConn.Connect();
	}

	void HandleDataFromServer(object sender, DataReceivedEventArgs args) {
		Connection connection = (Connection)sender;
		Debug.Log("Received " + args.Bytes.Length + " bytes from server at " + connection.EndPoint.ToString());
        //connection.SendBytes(args.Bytes, args.SendOption);
        ReceiveMessage(args.Bytes);
		args.Recycle();
	}

	void HandleServerDisconnect(object sender, DisconnectedEventArgs args) {
		Connection connection = (Connection)sender;
		Debug.Log("Server connection at " + connection.EndPoint + " lost");
		serverConn = null;
		args.Recycle();
	}

    void OnApplicationQuit()
    {
        Debug.Log("DisConnecting from: " + serverConn.EndPoint.ToString());
        if (serverConn != null) serverConn.Close();
    }
    #endregion

    #region NetworkLogic
    /// <summary>
    /// SendMessage
    /// </summary>
    /// <param name="Type"></param>
    /// <param name="IDObject"></param>
    /// <param name="Pos"></param>
    /// <param name="Rot"></param>
    public void SendMessage(SendType SType, PacketId Type, ushort IDObject, Vector3 Pos, Quaternion Rot) {
        sbyte TypeBuffer = 0;
        byte STypeBuffer = 0;

        //Choose who to send message
        switch (SType)
        {
            case SendType.SENDTOALL:
                STypeBuffer = 0;
                break;
            case SendType.SENDTOOTHER:
                STypeBuffer = 1;
                break;
            case SendType.SENDTOSERVER:
                STypeBuffer = 2;
                break;
            default:
                STypeBuffer = 0;
                break;
{'fraction_non_alphanumeric': 0.12504301938740392, 'fraction_numerical': 0.007571412183090513, 'mean_word_length': 2.788787483702738, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 59, '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': '949229', 'n_tokens_mistral': 2870, 'n_tokens_neox': 2596, 'n_words': 653}