WCF를 사용하여 메시지 프레임을 구현하는 방법을 이해하려고합니다. 목표는 Tcp를 통해 독점 형식을 처리 할 수있는 WCF의 서버를 만드는 것입니다. 나는 SOAP를 위해서만 있기 때문에 net.Tcp 바인딩을 사용할 수 없다.WCF : 메시지 프레이밍 및 사용자 지정 채널
형식의 메시지를 수신하는 맞춤 채널을 작성해야합니다. 메시지 예는 "5 abcde"입니다. 특히 맞춤 채널에서 프레이밍하는 방법을 잘 모르겠습니다. 여기
몇 가지 샘플 코드class CustomChannel: IDuplexSessionChannel
{
private class PendingRead
{
public NetworkStream Stream = null;
public byte[] Buffer = null;
public bool IsReading = false;
}
private CommunicationState state = CommunicationState.Closed;
private TcpClient tcpClient = null;
private MessageEncoder encoder = null;
private BufferManager bufferManager = null;
private TransportBindingElement bindingElement = null;
private Uri uri = null;
private PendingRead pendingRead;
public CustomChannel(Uri uri, TransportBindingElement bindingElement, MessageEncoderFactory encoderFactory, BufferManager bufferManager, TcpClient tcpClient)
{
this.uri = uri;
this.bindingElement = bindingElement;
this.tcpClient = tcpClient;
this.bufferManager = bufferManager;
state = CommunicationState.Created;
}
public IAsyncResult BeginTryReceive(TimeSpan timeout, AsyncCallback callback, object state)
{
if (this.state != CommunicationState.Opened) return null;
byte[] buffer = bufferManager.TakeBuffer(tcpClient.Available);
NetworkStream stream = tcpClient.GetStream();
pendingRead = new PendingRead { Stream = stream, Buffer = buffer, IsReading = true };
IAsyncResult result = stream.BeginRead(buffer, 0, buffer.Length, callback, state);
return result;
}
public bool EndTryReceive(IAsyncResult result, out Message message)
{
int byteCount = tcpClient.Client.EndReceive(result);
string content = Encoding.ASCII.GetString(pendingRead.buffer)
// framing logic here
Message.CreateMessage(...)
}
}
그래서 기본적으로 EndTryReceive 주위에 처음으로 그냥 보류중인 읽기 버퍼 "5 AB"에서 메시지의 조각을 얻을 수있다. 그런 다음 두 번째로 메시지의 나머지 부분을 얻을 수 있습니다. 문제는 EndTryReceive가 처음 호출 될 때, Message 객체를 생성해야하는 경우입니다. 이는 부분 메시지가 채널 스택 위로 올라가는 것을 의미합니다.
내가 정말로하고 싶은 것은 EndTryReceive에서 메시지를 작성할 때 전체 메시지라는 것을 확인하기 위해 버퍼에 내 전체 메시지 "5 abcde"가 있는지 확인하는 것입니다.
누구나 WCF를 사용하여 사용자 지정 프레이밍을 수행하는 방법에 대한 예가 있습니까?
감사, 와이어 수준에서 바딤