ISocketService implementation:

by gowtham 2010-02-09 09:53:00

public class SimpleEchoService : ISocketService
{
public void OnConnected(ConnectionEventArgs e)
{
//----- Check the host!

if (e.Connection.HostType == HostType.htServer)
{
//----- Enqueue receive!

e.Connection.BeginReceive();
}
else
{
//----- Enqueue send a custom message!

byte[] b =
GetMessage(e.Connection.SocketHandle.ToInt32());
e.Connection.BeginSend(b);
}
}

public void OnSent(MessageEventArgs e)
{
//----- Check the host. In this case both start a receive!

if (e.Connection.HostType == HostType.htServer)
{
//----- Enqueue receive!

e.Connection.BeginReceive();
}
else
{
//----- Enqueue receive!

e.Connection.BeginReceive();
}
}

public override void OnReceived(MessageEventArgs e)
{
//----- Check the host!

if (e.Connection.HostType == HostType.htServer)
{
//----- If server, send the data buffer received!

byte[] b = e.Buffer;
e.Connection.BeginSend(b);
}
else
{
//----- If client, generate another

//----- custom message and send it!

byte[] b = GetMessage(e.Connection.SocketHandle.ToInt32());
e.Connection.BeginSend(b);
}
}

public override void OnDisconnected(DisconnectedEventArgs e)
{
//----- Check the host!

if (e.Connection.HostType == HostType.htServer)
{
//----- Nothing!

}
else
{
//----- Reconnect with server!

e.Connection.AsClientConnection().BeginReconnect();
}
}
}

Tagged in:

1335
like
0
dislike
0
mail
flag

You must LOGIN to add comments