C# Class OpenNos.Core.Networking.Communication.Scs.Communication.Protocols.BinarySerialization.BinarySerializationProtocol

Default communication protocol between server and clients to send and receive a message. It uses .NET binary serialization to write and read messages. A Message format: [Message Length (4 bytes)][Serialized Message Content] If a message is serialized to byte array as N bytes, this protocol adds 4 bytes size information to head of the message bytes, so total length is (4 + N) bytes. This class can be derived to change serializer (default: BinaryFormatter). To do this, SerializeMessage and DeserializeMessage methods must be overrided.
Inheritance: IScsWireProtocol, IDisposable
显示文件 Open project: OpenNos/OpenNos

Public Methods

Method Description
BinarySerializationProtocol ( ) : System

Creates a new instance of BinarySerializationProtocol.

CreateMessages ( byte receivedBytes ) : IEnumerable

Builds messages from a byte array that is received from remote application. The Byte array may contain just a part of a message, the protocol must cumulate bytes to build messages. This method is synchronized. So, only one thread can call it concurrently.

Dispose ( ) : void
GetBytes ( IScsMessage message ) : byte[]

Serializes a message to a byte array to send to remote application. This method is synchronized. So, only one thread can call it concurrently.

Reset ( ) : void

This method is called when connection with remote application is reset (connection is renewing or first connecting). So, wire protocol must reset itself.

Protected Methods

Method Description
DeserializeMessage ( byte bytes ) : IScsMessage

This method is used to deserialize a IScsMessage from it's bytes. This method can be overrided by derived Classs to change deserialization strategy. It is a couple with SerializeMessage method and must be overrided together.

Dispose ( bool disposing ) : void
SerializeMessage ( IScsMessage message ) : byte[]

This method is used to serialize a IScsMessage to a byte array. This method can be overrided by derived Classs to change serialization strategy. It is a couple with DeserializeMessage method and must be overrided together.

Private Methods

Method Description
ReadByteArray ( Stream stream, int length ) : byte[]

Reads a byte array with specified length.

ReadSingleMessage ( ICollection messages ) : bool

This method tries to read a single message and add to the messages collection.

WriteInt32 ( byte buffer, int startIndex, int number ) : void

Writes a int value to a byte array from a starting index.

Method Details

BinarySerializationProtocol() public method

Creates a new instance of BinarySerializationProtocol.
public BinarySerializationProtocol ( ) : System
return System

CreateMessages() public method

Builds messages from a byte array that is received from remote application. The Byte array may contain just a part of a message, the protocol must cumulate bytes to build messages. This method is synchronized. So, only one thread can call it concurrently.
public CreateMessages ( byte receivedBytes ) : IEnumerable
receivedBytes byte Received bytes from remote application
return IEnumerable

DeserializeMessage() protected method

This method is used to deserialize a IScsMessage from it's bytes. This method can be overrided by derived Classs to change deserialization strategy. It is a couple with SerializeMessage method and must be overrided together.
protected DeserializeMessage ( byte bytes ) : IScsMessage
bytes byte /// Bytes of message to be deserialized (does not include message length. It consist of a /// single whole message) ///
return IScsMessage

Dispose() public method

public Dispose ( ) : void
return void

Dispose() protected method

protected Dispose ( bool disposing ) : void
disposing bool
return void

GetBytes() public method

Serializes a message to a byte array to send to remote application. This method is synchronized. So, only one thread can call it concurrently.
/// Throws CommunicationException if message is bigger than maximum allowed message length. ///
public GetBytes ( IScsMessage message ) : byte[]
message IScsMessage Message to be serialized
return byte[]

Reset() public method

This method is called when connection with remote application is reset (connection is renewing or first connecting). So, wire protocol must reset itself.
public Reset ( ) : void
return void

SerializeMessage() protected method

This method is used to serialize a IScsMessage to a byte array. This method can be overrided by derived Classs to change serialization strategy. It is a couple with DeserializeMessage method and must be overrided together.
protected SerializeMessage ( IScsMessage message ) : byte[]
message IScsMessage Message to be serialized
return byte[]