can you use delegates to pass a copy of an object when an event happens?

D

darthghandi

I am creating a listener class that listens for incoming connections.
When I get an incoming connection, I want to signal an event to
happen and pass that connected socket to a different thread for the
real work. I want to continue to listen for more connections. So is
the socket I pass with the delegate a reference or a value? I thought
this might work:

public delegate void AcceptConnection(Socket sock);

public class Listener
{
private Socket m_listener;

public event AcceptConnection ConnectionReceived;
public Listener()
{
IPEndPoint theEnd = new IPEndPoint(IPAddress.Any,
12345);
m_listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
m_listener.Bind(theEnd);
m_listener.Listen(20);
}

public Listener(int port)
{
m_hasSocketsReady = false;
IPEndPoint theEnd = new IPEndPoint(IPAddress.Any, port);
m_listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
m_listener.Bind(theEnd);
m_listener.Listen(20);
}

public void AcceptNewConnections()
{
AsyncCallback callme = new AsyncCallback(AddWorkerSocket);
m_listener.BeginAccept(callme, m_listener);
}

public void AddWorkerSocket(IAsyncResult ar)
{
Socket hold = (Socket)ar.AsyncState;
OnConnectionReceived(hold);
AsyncCallback callMe = new AsyncCallback(AddWorkerSocket);
hold.BeginAccept(callMe, hold);
}

public void OnConnectionReceived(Socket ret)
{
//ret = m_workers.Dequeue();
if (ConnectionReceived != null)
{
ConnectionReceived(ret);
}
}
}
Is there a better way? Am I going about this the wrong way?
Thanks for your time,
Chris
 
M

Mike Labosh

I am creating a listener class that listens for incoming connections.
When I get an incoming connection, I want to signal an event to
happen and pass that connected socket to a different thread for the
real work. I want to continue to listen for more connections. So is
the socket I pass with the delegate a reference or a value?

Since the Socket is an instance of a class, it is always a reference. NOT a
copy.

If you want to get a copy into a second reference, you could use the
MemberwiseClone() method.
--

Peace & happy computing,

Mike Labosh, MCSD MCT
Owner, vbSensei.Com

"Escriba coda ergo sum." -- vbSensei
 
D

darthghandi

Since the Socket is an instance of a class, it is always a reference. NOT a
copy.

If you want to get a copy into a second reference, you could use the
MemberwiseClone() method.
--

Peace & happy computing,

Mike Labosh, MCSD MCT
Owner, vbSensei.Com

"Escriba coda ergo sum." -- vbSensei

After looking more at the Socket Class I noticed that the EndReceive()
method returns a new Socket that handles the remote communication. So
I modified the method like so:
public void AddWorkerSocket(IAsyncResult ar)
{
Socket hold = (Socket)ar.AsyncState;
Socket work = hold.EndAccept(ar);
OnConnectionReceived(work);
AsyncCallback callMe = new AsyncCallback(AddWorkerSocket);
hold.BeginAccept(callMe, hold);
}
So from what I understand, that new socket should be passed to any
subscribers of the event. Is that correct? When I do the following
in another class, I run into a problem:
private void button6_Click(object sender, EventArgs e)
{
Listener listen = new Listener();
listen.ConnectionReceived += new
AcceptConnection(AcceptComplete);
listen.AcceptNewConnections();
}

private void AcceptComplete(Socket returnSock)
{
if (returnSock.Connected)
{
//now connected
DoWork();
}
}
I never get that Socket in the AcceptComplete() method. What am I
doing wrong?
Thanks for your time,
Chris
 

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