Ejemplo n.º 1
0
	protected bool Send( int connectionId, byte[] key, InternalMessage message, ChannelIndex channel, BitWriter writer, bool sendImmediately, out string error )
	{
		MessageSecurity security = AlpacaConstant.GetSecurity( channel );
		if( !WrapMessage( message, security, writer, key, out error ) )
		{
			return false;
		}

		//_profiler.StartEvent(TickType.Send, (uint)stream.Length, channelName, AlpacaConstant.INTERNAL_MESSAGE_NAME[messageType]);
		DataStream s = writer.GetStream();		
		byte errorAsByte;
		if( sendImmediately )
		{
			NetworkTransport.Send                  ( 0, connectionId, channel.GetIndex(), s.GetBuffer(), s.GetByteLength(), out errorAsByte);
		}
		else
		{
			NetworkTransport.QueueMessageForSending( 0, connectionId, channel.GetIndex(), s.GetBuffer(), s.GetByteLength(), out errorAsByte);
		}
		//_profiler.EndEvent();

		if( (NetworkError)errorAsByte != NetworkError.Ok )
		{
			error = $"Send {AlpacaConstant.GetName(message)} to node {connectionId} failed with error{StringFromError(errorAsByte)}";
			return false;
		}

		return true;
	}
Ejemplo n.º 2
0
	protected bool WrapMessage( InternalMessage message, MessageSecurity security, BitWriter writer, byte[] key, out string error )
	{
		if( !_commonSettings.enableEncryption && security != MessageSecurity.None )
		{
			error = "Attempted to send encrypted and/or authenticated message but encryption was not enabled";
			return false;
		}

		bool authenticated = ((security & MessageSecurity.Authenticated) != 0);
		bool encrypted     = ((security & MessageSecurity.Encrypted    ) != 0);
		
		if( authenticated || encrypted )
		{
			Debug.Assert( false );
			// TODO: handle encryption and authentication here
			error = "WrapMessage: tried to apply encryption, but it was not enabled.";
			return false;
		}

		DataStream writerStream = writer.GetStream();
		int lastBytePos = writerStream.GetBytePosition();
		
		
		// reset write head to write authentication and encryption bits
		writer.Normal(authenticated);
		writer.Normal(encrypted);
		writerStream.SetBytePosition( lastBytePos );
		byte messageByte = (byte)(((int)message) << 2);
		writer.Normal<byte>( messageByte );
		error = null;
		return true;
	}