callback to another class (sockets)

A

Adie

I'm using async-callbacks for a socket client.

I have a class doing the work with the server - includes the socket,
AsyncResult, AsynCallback etc.. (call it client for arguments sake)

Also a class that is calling the above but acting like a message broker and
wants the result from it's call to the above class. (call it broker)

The question is, can I pass a delegate from the broker to the client that I
can call from the client when data is recieved?

Here's my client code for now:

using System;
using System.Net;
using System.Text;
using System.Net.Sockets;

namespace NNTP_Test
{

public class Pipe
{
private IAsyncResult _asyncResult;
private AsyncCallback _asyncCallback;
private Socket _skt = null;
private int _port = 0;
private string _ip = "";
private Byte[] _buffer;
private IPEndPoint _endpoint;


public Pipe(string ip, int port)
{
_ip = ip;
_port = port;
_skt = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
_buffer = new byte[75];
IPAddress ipAddress = IPAddress.Parse(_ip);
_endpoint = new IPEndPoint(ipAddress, _port);

try
{
_skt.Connect(_endpoint);
WaitForData();
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}

public void SendData(string message)
{

}

private void WaitForData()
{
if ( _asyncCallback == null )
{
_asyncCallback = new AsyncCallback (OnDataReceived);
}

// begin waiting for data
_asyncResult = _skt.BeginReceive(_buffer,
0,_buffer.Length,SocketFlags.None,_asyncCallback,null);
}

public void OnDataReceived(IAsyncResult asyn)
{
//end receive...
int iRx = 0 ;
iRx = _skt.EndReceive(asyn);
char[] chars = new char[iRx + 1];
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(_buffer, 0, iRx, chars, 0);
String szData = new System.String(chars);
Console.WriteLine(szData);
WaitForData();
}
}
}

I'd like to be able to pass a delegate from the broker that can be called
from the client with the string szData in the public void
OnDataReceived(IAsyncResult asyn) method.

But I'm not sure how to wire it all up.
 
A

Adie

Adie said:
I'm using async-callbacks for a socket client.

I have a class doing the work with the server - includes the socket,
AsyncResult, AsynCallback etc.. (call it client for arguments sake)

Also a class that is calling the above but acting like a message broker and
wants the result from it's call to the above class. (call it broker)

The question is, can I pass a delegate from the broker to the client that I
can call from the client when data is recieved?

Here's my client code for now:

using System;
using System.Net;
using System.Text;
using System.Net.Sockets;

namespace NNTP_Test
{

public class Pipe
{
private IAsyncResult _asyncResult;
private AsyncCallback _asyncCallback;
private Socket _skt = null;
private int _port = 0;
private string _ip = "";
private Byte[] _buffer;
private IPEndPoint _endpoint;


public Pipe(string ip, int port)
{
_ip = ip;
_port = port;
_skt = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
_buffer = new byte[75];
IPAddress ipAddress = IPAddress.Parse(_ip);
_endpoint = new IPEndPoint(ipAddress, _port);

try
{
_skt.Connect(_endpoint);
WaitForData();
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}

public void SendData(string message)
{

}

private void WaitForData()
{
if ( _asyncCallback == null )
{
_asyncCallback = new AsyncCallback (OnDataReceived);
}

// begin waiting for data
_asyncResult = _skt.BeginReceive(_buffer,
0,_buffer.Length,SocketFlags.None,_asyncCallback,null);
}

public void OnDataReceived(IAsyncResult asyn)
{
//end receive...
int iRx = 0 ;
iRx = _skt.EndReceive(asyn);
char[] chars = new char[iRx + 1];
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(_buffer, 0, iRx, chars, 0);
String szData = new System.String(chars);
Console.WriteLine(szData);
WaitForData();
}
}
}

I'd like to be able to pass a delegate from the broker that can be called
from the client with the string szData in the public void
OnDataReceived(IAsyncResult asyn) method.

But I'm not sure how to wire it all up.

Looking at it further, I think i need to create an event in client and pass
it a function from broker, sort of publish/subcribe stuff.
 
A

Adie

A... ha! Got it.

this goes in the client

public delegate void DataHandler(string message);
public event DataHandler GetData;

then call the OnGetData event,
from there call GetData(string)

and in the message broker -

static void Main(string[] args)
{
Pipe pipe = new Pipe("blah bha",119);
pipe.GetData += new NNTP_Test.Pipe.DataHandler(DataHandler);
}

private static void DataHandler(string message)
{
Console.WriteLine(message);
}

// groovy!
 
W

William Stacey [MVP]

I'd like to be able to pass a delegate from the broker that can be called
from the client with the string szData in the public void
OnDataReceived(IAsyncResult asyn) method.

What you mean by "client" in this case? The network client across the wire
or a client object in the server? Your callback delegate could call another
registered delegate and/or event.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top