<commit_before>/**
 * Screenary: Real-Time Collaboration Redefined.
 * Transport Client
 *
 * Copyright 2011-2012 <NAME> <<EMAIL>>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.IO;
using System.Net;
using System.Threading;
using System.Net.Sockets;

namespace Screenary
{
	public class TransportClient
	{
		private Int32 port;
		private string hostname;
		private TcpClient tcpClient;
		private Thread thread = null;
		private ChannelDispatcher dispatcher;
		
		public const int PDU_HEADER_SIZE = 6;
		public const int PDU_MAX_FRAG_SIZE = 0x3FFF;
		public const int PDU_MAX_PAYLOAD_SIZE = (PDU_MAX_FRAG_SIZE - PDU_HEADER_SIZE);
		
		public const UInt16 PDU_CHANNEL_SESSION = 0x00;
		public const UInt16 PDU_CHANNEL_UPDATE = 0x01;
		public const UInt16 PDU_CHANNEL_INPUT = 0x02;
		
		public const byte PDU_UPDATE_SURFACE = 0x01;
		
		public const byte PDU_FRAGMENT_SINGLE = 0x00;
		public const byte PDU_FRAGMENT_FIRST = 0x01;
		public const byte PDU_FRAGMENT_NEXT = 0x02;
		public const byte PDU_FRAGMENT_LAST = 0x03;
		
		public TransportClient(TcpClient tcpClient)
		{
			this.dispatcher = null;
			this.tcpClient = tcpClient;
		}
		
		public TransportClient(ChannelDispatcher dispatcher)
		{
			this.dispatcher = dispatcher;
			this.tcpClient = null;
		}
		
		public TransportClient(ChannelDispatcher dispatcher, TcpClient tcpClient)
		{
			this.dispatcher = dispatcher;
			this.tcpClient = tcpClient;
		}
		
		public void SetChannelDispatcher(ChannelDispatcher dispatcher)
		{
			this.dispatcher = dispatcher;
		}
		
		public void StartThread()
		{
			if (thread == null)
			{
				thread = new Thread(() => ThreadProc(this));
			}
			
			thread.Start();
		}
		
		public void StopThread()
		{

		}
		
		public bool isThreadAlive()
		{
			if (thread != null)
				return thread.IsAlive;	
			else
				return false;
		}
		
		public bool Connect(string hostname, Int32 port)
		{
			this.hostname = hostname;
			this.port = port;
	
			if (tcpClient == null)
			{
				tcpClient = new TcpClient();
				tcpClient.NoDelay = true;
			}
			
			tcpClient.Connect(this.hostname, this.port);
			
			this.StartThread();
			
			dispatcher.OnConnect();
	
			return true;
		}
		
		public bool Disconnect()
		{
			dispatcher.OnDisconnect();
			
			if (tcpClient != null)
				tcpClient.Close();						
			
			return true;
		}
		
		public bool isConnected()
		{
			if (tcpClient == null)
				return false;
			else 
				return tcpClient.Connected;	
			
		}
		
		private void SendAll(Socket socket, byte[] buffer, int offset, int size)
		{
			int sent = 0;
			int total_sent = 0;
			int end = offset + size;
			
			while (offset < end)
			{
				sent = socket.Send(buffer, offset, (size - total_sent), 0);
				total_sent += sent;
				offset += sent;
			}
		}
		
		private void RecvAll(Socket socket, byte[] buffer, int offset, int size)
		{
			int recv = 0;
			int total_recv = 0;
			int end = offset + size;
			
			while (offset < end)
			{
				recv = socket.Receive(buffer, offset, (size - total_recv), 0);
				total_recv += recv;
				offset += recv;
			}
		}
		
		public void SendPDU(byte[] buffer, UInt16 channelId, byte pduType)
		{
			Socket socket;
			BinaryWriter s;<commit_msg>Issue #53 temporary solution<commit_after>/**
 * Screenary: Real-Time Collaboration Redefined.
 * Transport Client
 *
 * Copyright 2011-2012 <NAME> <<EMAIL>>
 * Copyright 2011-2012 <NAME> <<EMAIL>>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.IO;
using System.Net;
using System.Threading;
using System.Net.Sockets;

namespace Screenary
{
	public class TransportClient
	{
		private Int32 port;
		private string hostname;
		private TcpClient tcpClient;
		private Thread thread = null;
		private ChannelDispatcher dispatcher;
		
		public const int PDU_HEADER_SIZE = 6;
		public const int PDU_MAX_FRAG_SIZE = 0x3FFF;
		public const int PDU_MAX_PAYLOAD_SIZE = (PDU_MAX_FRAG_SIZE - PDU_HEADER_SIZE);
		
		public const UInt16 PDU_CHANNEL_SESSION = 0x00;
		public const UInt16 PDU_CHANNEL_UPDATE = 0x01;
		public const UInt16 PDU_CHANNEL_INPUT = 0x02;
		
		public const byte PDU_UPDATE_SURFACE = 0x01;
		
		public const byte PDU_FRAGMENT_SINGLE = 0x00;
		public const byte PDU_FRAGMENT_FIRST = 0x01;
		public const byte PDU_FRAGMENT_NEXT = 0x02;
		public const byte PDU_FRAGMENT_LAST = 0x03;
		
		public TransportClient(TcpClient tcpClient)
		{
			this.dispatcher = null;
			this.tcpClient = tcpClient;
		}
		
		public TransportClient(ChannelDispatcher dispatcher)
		{
			this.dispatcher = dispatcher;
			this.tcpClient = null;
		}
		
		public TransportClient(ChannelDispatcher dispatcher, TcpClient tcpClient)
		{
			this.dispatcher = dispatcher;
			this.tcpClient = tcpClient;
		}
		
		public void SetChannelDispatcher(ChannelDispatcher dispatcher)
		{
			this.dispatcher = dispatcher;
		}
		
		public void StartThread()
		{
			if (thread == null)
			{
				thread = new Thread(() => ThreadProc(this));
			}
			
			thread.Start();
		}
		
		public void StopThread()
		{

		}
		
		public bool isThreadAlive()
		{
			if (thread != null)
				return thread.IsAlive;	
			else
				return false;
		}
		
		public bool Connect(string hostname, Int32 port)
		{
			this.hostname = hostname;
			this.port = port;
	
			if (tcpClient == null)
			{
				tcpClient = new TcpClient();
				tcpClient.NoDelay = true;
			}
			
			tcpClient.Connect(this.hostname, this.port);
			
			this.StartThread();
			
			dispatcher.OnConnect();
	
			return true;
		}
		
		public bool Disconnect()
		{
			dispatcher.OnDisconnect();
			
			if (tcpClient != null)
				tcpClient.Close();						
			
			return true;
		}
		
		public bool isConnected()
		{
			if (tcpClient == null)
				return false;
			else 
				return tcpClient.Connected;	
			
		}
		
		private void SendAll(Socket socket, byte[] buffer, int offset, int size)
		{
			int sent = 0;
			int total_sent = 0;
			int end = offset + size;
			
			while (offset < end)
			{
				try
				{
					sent = socket.Send(buffer, offset, (size - total_sent), 0);
					total_sent += sent;
					offset += sent;
				}
				catch (SocketException e)
				{
					return;
				}
			}
		}
		
		private void RecvAll(Socket socket, byte[] buffer, int offset, int size)
		{
			int recv = 0;
			int total_recv = 0;
			int end = offset + size;
			
			while (offset < end)
			{
				recv = socket.Receive(buffer, offset, (size - total_recv), 0);
				total_recv += recv;
				offset += recv;
			}
		}
		
		public void SendPDU(byte[] buffer, UInt16 channelId, byte pduType)
		{
			Socket socket;
			BinaryWriter s;
{'fraction_non_alphanumeric': 0.08983702133298, 'fraction_numerical': 0.016430369683317875, 'mean_word_length': 3.0191693290734825, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2477053', 'n_tokens_mistral': 3027, 'n_tokens_neox': 2375, 'n_words': 717}