This is a SocketWrapper written in C#. At the moment it only has the ClientSocket part done, ServerSocket part should be done later today. Both Client-, & ServerSocket will be async.
If you decide to use this, comment here and tell me if there are any problems I may have missed.
You can comment on the code even if you decide not to use this.
Example usage on bottom of this post.
Code:
/**
* Author : tanelipe
* Written in 18.5.2008
* */
using System;
using System.Net;
using System.Net.Sockets;
namespace SocketWrapper
{
#region Delegates
delegate void SocketEvent(BaseSocket Socket);
delegate void SocketErrorEvent(BaseSocket Socket, String Error);
#endregion
#region BaseSocket
class BaseSocket
{
#region Private Variables
private int m_UID = -1;
private byte[] m_Buffer;
private byte[] m_Packet;
private Socket m_Socket;
#endregion
/// <summary>
/// Initializes a new BaseSocket.
/// </summary>
/// <param name="BufferSize">How much data the buffer can handle.</param>
public BaseSocket(uint BufferSize)
{
m_Buffer = new byte[BufferSize];
}
/// <summary>
/// UniqueID
/// </summary>
public int UID
{
get { return m_UID; }
set { m_UID = value; }
}
/// <summary>
/// Packet data is first stored here.
/// </summary>
public byte[] Buffer
{
get { return m_Buffer; }
set { m_Buffer = value; }
}
/// <summary>
/// Packet data copied from Buffer
/// </summary>
public byte[] Packet
{
get { return m_Packet; }
set { m_Packet = value; }
}
public Socket Client
{
get { return m_Socket; }
set { m_Socket = value; }
}
}
#endregion
#region ClientSocket
class ClientSocket
{
public event SocketEvent onClientConnected;
public event SocketEvent onReceivePacket;
public event SocketErrorEvent onSocketError;
public String Host = "";
public UInt16 Port = 0;
private BaseSocket Socket;
private Boolean Initialised = false;
public ClientSocket()
{
Socket = new BaseSocket(0xFFFF);
Socket.Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Initialised = true;
}
public void Send(byte[] Packet)
{
if (!Socket.Client.Connected)
return;
Socket.Client.Send(Packet);
}
public void Connect()
{
if (Host == "" || Port == 0)
{
throw new Exception(Host == "" ? "'Host' can't be an emptry string." : "'Port' can't be 0.");
}
IPAddress RemoteHost;
if (!IPAddress.TryParse(Host, out RemoteHost))
throw new Exception("Could not parse 'Host' into IPAddress.");
if (!Initialised)
{
Socket = new BaseSocket(0xFFFF);
Socket.Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
Socket.Client.BeginConnect(RemoteHost, Port, new AsyncCallback(SocketConnecting), Socket);
}
private void SocketConnecting(IAsyncResult res)
{
BaseSocket Socket = (BaseSocket)res.AsyncState;
try
{
Socket.Client.EndConnect(res);
if (onClientConnected != null)
onClientConnected.Invoke(Socket);
Socket.Client.BeginReceive(Socket.Buffer, 0, 0xFFFF, SocketFlags.None, new AsyncCallback(ReceivePacket), Socket);
}
catch (Exception ConnectError)
{
if (onSocketError != null)
onSocketError.Invoke(Socket, ConnectError.Message);
}
}
private void ReceivePacket(IAsyncResult res)
{
BaseSocket Socket = (BaseSocket)res.AsyncState;
try
{
SocketError Mode;
int Size = Socket.Client.EndReceive(res, out Mode);
if (Mode == SocketError.Success && Size > 0)
{
Socket.Packet = new byte[Size];
for (int i = 0; i < Size; i++)
Socket.Packet[i] = Socket.Buffer[i];
if (onReceivePacket != null)
onReceivePacket.Invoke(Socket);
Socket.Client.BeginReceive(Socket.Buffer, 0, 0xFFFF, SocketFlags.None, new AsyncCallback(ReceivePacket), Socket);
}
}
catch (Exception e)
{
if (onSocketError != null)
onSocketError.Invoke(Socket, e.Message);
}
}
}
#endregion
}
Code:
ClientSocket Socket = new ClientSocket();
Socket.Host = "123.45.67.89";
Socket.Port = 9958;
Socket.Connect();
Socket.onClientConnected += new SocketEvent(Socket_onClientConnected);
Socket.onReceivePacket += new SocketEvent(Socket_onReceivePacket);
Socket.onSocketError += new SocketErrorEvent(Socket_onSocketError);







