本文整理汇总了C#中Message.GetBytes方法的典型用法代码示例。如果您正苦于以下问题:C# Message.GetBytes方法的具体用法?C# Message.GetBytes怎么用?C# Message.GetBytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Message
的用法示例。
在下文中一共展示了Message.GetBytes方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConstructorTakingByteArrayTest
public void ConstructorTakingByteArrayTest()
{
const string MsgContents = "Hello, World!";
var msg = new Message(Encoding.UTF8.GetBytes(MsgContents));
var stream = msg.GetBodyStream();
var sr = new StreamReader(stream);
Assert.AreEqual(sr.ReadToEnd(), MsgContents);
msg = new Message(Encoding.UTF8.GetBytes(MsgContents));
var msgBytes = msg.GetBytes();
Assert.AreEqual(Encoding.UTF8.GetString(msgBytes), MsgContents);
}
示例2: ConstructorTakingEmptyByteArrayTest
public void ConstructorTakingEmptyByteArrayTest()
{
var msg = new Message(new byte[0]);
var stream = msg.GetBodyStream();
Assert.IsNotNull(stream);
var ms = new MemoryStream();
stream.CopyTo(ms);
Assert.IsTrue(ms.GetBuffer().Length == 0);
msg = new Message(new byte[0]);
var bytes = msg.GetBytes();
Assert.AreEqual(0, bytes.Length);
}
示例3: GetBytesValidSequence
public void GetBytesValidSequence()
{
Message message = new Message(new byte[10], (byte)245);
byte[] bytes = message.GetBytes();
Assert.IsNotNull(bytes);
// len(payload) + 1 + 4
Assert.AreEqual(15, bytes.Length);
// first 4 bytes = the magic number
Assert.AreEqual((byte)245, bytes[0]);
// next 4 bytes = the checksum
Assert.IsTrue(message.Checksum.SequenceEqual(bytes.Skip(1).Take(4).ToArray<byte>()));
// remaining bytes = the payload
Assert.AreEqual(10, bytes.Skip(5).ToArray<byte>().Length);
}
示例4: DeserializableCommand
public DeserializableCommand(Message message, ISerialize serializer)
{
if (message == null)
{
throw new ArgumentNullException("message");
}
if (serializer == null)
{
throw new ArgumentNullException("serializer");
}
Debug.Assert(
!string.IsNullOrEmpty(message.LockToken),
"message.LockToken is a null reference or empty string.");
_lockToken = message.LockToken;
byte[] messageBytes = message.GetBytes(); // this needs to be saved if needed later, because it can only be read once from the original Message
_command = serializer.DeserializeObject<dynamic>(messageBytes);
}
示例5: RetrievingMessageBytesAfterGetBodyStreamTest
public void RetrievingMessageBytesAfterGetBodyStreamTest()
{
var msg = new Message(new byte[0]);
msg.GetBodyStream();
TestAssert.Throws<InvalidOperationException>(() => msg.GetBytes());
}
示例6: CallingGetBytesTwiceTest
public void CallingGetBytesTwiceTest()
{
var msg = new Message(new byte[0]);
msg.GetBytes();
TestAssert.Throws<InvalidOperationException>(() => msg.GetBytes());
}
示例7: TestNetwork
/// <summary>
/// Sends out network test messages
/// </summary>
public void TestNetwork()
{
#region send out network test messages
Message TestMsg = new Message();
TestMsg.cmd = NetMessageCommand.IncreaseThrust;
TestMsg.frame = networkTest_FrameCount;
TestMsg.playerNumber = Controller.myNetgamePlayerNumber;
// method #1: just send on this thread
// works well as long as socket "always seems ready",
// which is the case when on localhost.
// The simple SendAsync below however actually
// performs __much__ better in my tests.
//////socket.Send( TestMsg.GetBytes() );
// method #2: works well
// This way works very well because its _like_ sending
// with UDP, in that the main thread (this thread) won't
// block up in the event that the server is not "ready
// to receive". The block can be very short (~10ms) but
// its enough to give the game a "chunking" feel when the
// network connection isn't ideal.
// 60% chance to actually send, to kind of
// make the test more realistic (sending 100% of
// messages (1 message / frame ) really isn't how
// the game operates)
if( SPW.rand.NextDouble() < 0.6 )
{
SocketAsyncEventArgs saea = new SocketAsyncEventArgs();
saea.SetBuffer( TestMsg.GetBytes(), 0, Message.Size );
socket.SendAsync( saea );
}
#region debug output
// My verbose debug output
int startRightX = SPW.world.ScreenWidth - 200;
int y = 20;
SPW.sw[ "gameState" ] = new StringItem( "gameState: " + SPW.gameState, startRightX, y += 20, 1.0f, Color.Gray );
SPW.sw[ "netState" ] = new StringItem( "netState: " + SPW.netState, startRightX, y += 20, 1.0f, Color.Gray );
SPW.sw[ "teststring" ] = new StringItem( "avg delay: " + networkTest_DelayMetrics.averageMessageDelay, startRightX, y += 20, 1.0f, Color.Gray );
SPW.sw[ "teststring1" ] = new StringItem( "last transit: " + networkTest_DelayMetrics.LastMessageTransportTime, startRightX, y += 20, 1.0f, Color.Gray );
SPW.sw[ "teststring2" ] = new StringItem( "sum delay: " + networkTest_DelayMetrics.TotalFrameDelay, startRightX, y += 20, 1.0f, Color.Gray );
SPW.sw[ "teststring3" ] = new StringItem( "total msgs: " + networkTest_DelayMetrics.TotalMessagesReceived, startRightX, y += 20, 1.0f, Color.Gray );
#endregion
networkTest_FrameCount++;
if( networkTest_FrameCount > networkTest_TestLength )
{
StopTest();
}
#endregion
}
示例8: Send
/// <summary>
/// THE MESSAGE SEND METHOD. Sends a message across the network to the server
/// which will eventually be received by the other connected player.
/// </summary>
/// <param name="message">The message struct you want to send</param>
public void Send( Message message )
{
if( message.playerNumber == Controller.myNetgamePlayerNumber )
{
// Short cct. push directly to incoming queue
// before sending out across network.
// (which is obviously necessary for other player to see it!!)
lock( Controller.incoming )
{
Controller.incoming.Add( message );
}
// Because we're doing this here, in the Receive
// (the 'listen()' function), we only
// copy the messages that originated from THE OTHER PLAYER
// to the Controller.incoming queue, as our messages
// are already in our own queue (though the server
// will still send our own messages back down to us, due to
// the way the server is programmed).
}
// Proceed to send out across network so the other player
// will get it.
// Break the Message struct down into its byte-representation
// and then just send it.
byte[] messageBytes = message.GetBytes();
// send that array of bytes across the network, now
try
{
// We could just straight
// socket.Send it:
/////////socket.Send( messageBytes ) ; //!! Using socket.SendAsync() method instead, below
// socket.Send() works ok, but can make the game
// feel a bit "chunky" when tcp misbehaves a bit.
////
// Here's another way that performs better
// when the network is a bit laggy: we'll
// use the socket.SendAsync method.
SocketAsyncEventArgs saea = new SocketAsyncEventArgs();
saea.SetBuffer( message.GetBytes(), 0, Message.Size );
socket.SendAsync( saea ); // send off "asynchronously" -
// i.e. basically DO NOT BLOCK THIS THREAD is sending
// takes more than a couple of microseconds. Remember
// this is the MAIN GAME THREAD we're on here, so if this
// thread blocks, then THE WHOLE GAME STOPS (stops drawing,
// stops advancing game engine, stops EVERYTHING).
}
catch( Exception exc )
{
SPW.logger.Log( "Error sending the message " + message.ToString(), LogMessageType.Error, OutputDevice.ScreenAndFile );
SPW.logger.Log( "Exception text: " + exc.Message, LogMessageType.Error, OutputDevice.File );
}
}
示例9: PushPipelineData
public void PushPipelineData(IEndpoint endpoint, Message message)
{
Queue<IPipelineComponent> pipeline;
if (!_pipelines.TryGetValue(endpoint, out pipeline))
{
//TODO: Temporary for testing
endpoint.ProcessMessage(endpoint, message.GetBytes());
}
else
{
byte[] data = null;
foreach (IAssembler component in pipeline.OfType<IAssembler>())
{
component.AddMessage(message);
data = component.Assemble();
// Decide what to do if data is returned.
// Right now, we break out and go to encoders
if (data != null) break;
}
if (data == null) data = message.GetBytes();
foreach (IEncoder component in pipeline.OfType<IEncoder>())
{
data = component.Encode(data);
}
if (data != null)
{
endpoint.ProcessMessage(endpoint, data);
}
}
}