/// <summary> Function to start the SocketServer </summary> /// <param name="ipAddress"> The IpAddress to listening on </param> /// <param name="port"> The Port to listen on </param> /// <param name="sizeOfRawBuffer"> Size of the Raw Buffer </param> /// <param name="sizeOfByteBuffer"> Size of the byte buffer </param> /// <param name="userArg"> User supplied arguments </param> /// <param name="messageHandler"> Function pointer to the user MessageHandler function </param> /// <param name="acceptHandler"> Function pointer to the user AcceptHandler function </param> /// <param name="closeHandler"> Function pointer to the user CloseHandler function </param> /// <param name="errorHandler"> Function pointer to the user ErrorHandler function </param> public void Start(string ipAddress, int port, int sizeOfRawBuffer, int sizeOfByteBuffer, object userArg, MESSAGE_HANDLER messageHandler, ACCEPT_HANDLER acceptHandler, CLOSE_HANDLER closeHandler, ERROR_HANDLER errorHandler) { // Is an AcceptThread currently running if (AcceptThread == null) { // Set connection values IpAddress = ipAddress; Port = port; // Save the Handler Functions MessageHandler = messageHandler; AcceptHandler = acceptHandler; CloseHandler = closeHandler; ErrorHandler = errorHandler; // Save the buffer size and user arguments SizeOfRawBuffer = sizeOfRawBuffer; SizeOfByteBuffer = sizeOfByteBuffer; UserArg = userArg; // Start the listening thread if one is currently not running ThreadStart tsThread = new ThreadStart(AcceptConnections); AcceptThread = new Thread(tsThread); AcceptThread.Name = string.Format("SocketAccept-{0}", ipAddress); AcceptThread.Start(); } }
/// <summary> /// Starts the SocketServer /// </summary> /// <param name="ipAddress">IP Address to listen on</param> /// <param name="port">Port to listen on</param> /// <param name="maxClientConnections">Maximum number of client connections</param> /// <param name="bufferSize">Size of incoming data buffer</param> /// <param name="userArg">(Optional) User supplied args. If not in use, pass in null</param> /// <param name="msgHandler">Pointer to custom MessageHandler function</param> /// <param name="acptHandler">Pointer to custom AcceptHandler function</param> /// <param name="clsHandler">Pointer to custom CloseHAndler function</param> /// <param name="errHandler">Pointer to custom ErrorHandler function</param> public void Start(string ipAddress, short port, int maxClientConnections, int bufferSize, Object userArg, MESSAGE_HANDLER msgHandler, ACCEPT_HANDLER acptHandler, CLOSE_HANDLER clsHandler, ERROR_HANDLER errHandler) { if (acceptThread == null) { this.ipAddress = ipAddress; this.port = port; this.maxClientConnections = maxClientConnections; socketClientList = new SocketClient[maxClientConnections]; this.bufferSize = bufferSize; this.userArg = userArg; messageHandler = msgHandler; acceptHandler = acptHandler; closeHandler = clsHandler; errorHandler = errHandler; ThreadStart ts = new ThreadStart(AcceptThread); acceptThread = new Thread(ts) { Name = "Accept" }; acceptThread.Start(); } }
public SocketClient(SocketServer socketServer, Socket clientSocket, int socketListIndex, string ipAddress, short port, int bufferSize, Object userArg, MESSAGE_HANDLER msgHandler, CLOSE_HANDLER clsHandler, ERROR_HANDLER errHandler) { this.bufferSize = bufferSize; rawBuffer = new byte[bufferSize]; this.userArg = userArg; messageHandler = msgHandler; closeHandler = clsHandler; errorHandler = errHandler; callbackReadMethod = new AsyncCallback(ReceiveComplete); callbackWriteMethod = new AsyncCallback(SendComplete); disposedFlag = false; this.socketServer = socketServer; this.clientSocket = clientSocket; this.socketListIndex = socketListIndex; this.ipAddress = ipAddress; this.port = port; netStream = new NetworkStream(this.clientSocket); this.clientSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, 1048576); this.clientSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, 1048576); this.clientSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true); this.clientSocket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true); Receive(); }
// Constructor, Finalize, Dispose //******************************************************************** /// <summary> Constructor for client support </summary> /// <param name="iSizeOfRawBuffer"> SimType: The size of the raw buffer </param> /// <param name="pUserArg"> RefType: A Reference to the Users arguments </param> /// <param name="pfnMessageHandler"> DelType: Reference to the user defined message handler method </param> /// <param name="pfnCloseHandler"> DelType: Reference to the user defined close handler method </param> /// <param name="pfnErrorHandler"> DelType: Reference to the user defined error handler method </param> public CSocketClient(Int32 iSizeOfRawBuffer, Object pUserArg, MESSAGE_HANDLER pfnMessageHandler, CLOSE_HANDLER pfnCloseHandler, ERROR_HANDLER pfnErrorHandler) { LogLib.WriteLine("Entering in CSocketClient.CSocketClient()", LogLevel.Trace); // Create the raw buffer GetSizeOfRawBuffer = iSizeOfRawBuffer; GetRawBuffer = new Byte[GetSizeOfRawBuffer]; // Save the user argument GetUserArg = pUserArg; // Set the handler methods GetMessageHandler = pfnMessageHandler; GetCloseHandler = pfnCloseHandler; GetErrorHandler = pfnErrorHandler; // Set the async socket method handlers GetCallbackReadMethod = new AsyncCallback(ReceiveComplete); GetCallbackWriteMethod = new AsyncCallback(SendComplete); // Init the dispose flag IsDisposed = false; LogLib.WriteLine("Exiting in CSocketClient.CSocketClient()", LogLevel.Trace); }
public SocketClient(int bufferSize, Object userArg, MESSAGE_HANDLER msgHandler, CLOSE_HANDLER clsHandler, ERROR_HANDLER errHandler) { this.bufferSize = bufferSize; rawBuffer = new byte[bufferSize]; this.userArg = userArg; messageHandler = msgHandler; closeHandler = clsHandler; errorHandler = errHandler; callbackReadMethod = new AsyncCallback(ReceiveComplete); callbackWriteMethod = new AsyncCallback(SendComplete); disposedFlag = false; }
// Constructor, Finalize, Dispose //******************************************************************** /// <summary> Constructor for client support </summary> /// <param name="iSizeOfRawBuffer"> SimType: The size of the raw buffer </param> /// <param name="pfnMessageHandler"> DelType: Reference to the user defined message handler method </param> /// <param name="pfnCloseHandler"> DelType: Reference to the user defined close handler method </param> public CSocketClient(int iSizeOfRawBuffer, MESSAGE_HANDLER pfnMessageHandler, CLOSE_HANDLER pfnCloseHandler) { // Create the raw buffer GetSizeOfRawBuffer = iSizeOfRawBuffer; GetRawBuffer = new byte[iSizeOfRawBuffer]; // Set the handler methods GetMessageHandler = pfnMessageHandler; GetCloseHandler = pfnCloseHandler; // Set the async socket method handlers GetCallbackReadMethod = new AsyncCallback(ReceiveComplete); GetCallbackWriteMethod = new AsyncCallback(SendComplete); }
/// <summary> Constructor for client support </summary> /// <param name="sizeOfRawBuffer"> The size of the raw buffer </param> /// <param name="sizeOfByteBuffer"> The size of the byte buffer </param> /// <param name="userArg"> A Reference to the Users arguments </param> /// <param name="messageHandler"> Reference to the user defined message handler function </param> /// <param name="closeHandler"> Reference to the user defined close handler function </param> /// <param name="errorHandler"> Reference to the user defined error handler function </param> public SocketClient(int sizeOfRawBuffer, int sizeOfByteBuffer, object userArg, MESSAGE_HANDLER messageHandler, CLOSE_HANDLER closeHandler, ERROR_HANDLER errorHandler) { // Create the raw buffer SizeOfRawBuffer = sizeOfRawBuffer; RawBuffer = new byte[SizeOfRawBuffer]; // Save the user argument UserArg = userArg; // Allocate a String Builder class for Application developer use StringBuffer = new StringBuilder(); // Allocate a Memory Stream class for Application developer use MessageBuffer = new MemoryStream(); // Allocate a byte buffer for Application developer use ByteBuffer = new byte[sizeOfByteBuffer]; BufferedBytes = 0; // Allocate a list buffer for Application developer use ListBuffer = new List <byte>(); // Set the handler functions MessageHandler = messageHandler; CloseHandler = closeHandler; ErrorHandler = errorHandler; // Set the async socket function handlers CallbackReadFunction = ReceiveComplete; CallbackWriteFunction = SendComplete; // Set available flags IsAvailable = true; // Set the unique key for this object UniqueKey = NewUniqueKey(); }
/// <summary> Function to stop the SocketServer. It can be restarted with Start </summary> public void Stop() { // Abort the accept thread if (AcceptThread != null) { TcpListener.Stop(); AcceptThread.Join(); AcceptThread = null; } lock (SocketClientList) { // Dispose of all of the socket connections foreach (SocketClient socketClient in SocketClientList) { socketClient.Dispose(); } } // Wait for all of the socket client objects to be destroyed GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); // Empty the Socket Client List SocketClientList.Clear(); // Clear the Handler Functions MessageHandler = null; AcceptHandler = null; CloseHandler = null; ErrorHandler = null; // Clear the buffer size and user arguments SizeOfRawBuffer = 0; UserArg = null; }
/// <summary> Constructor for SocketServer Suppport </summary> /// <param name="socketServer"> A Reference to the parent SocketServer </param> /// <param name="clientSocket"> The Socket object we are encapsulating </param> /// <param name="ipAddress"> The IpAddress of the remote server </param> /// <param name="port"> The Port of the remote server </param> /// <param name="sizeOfRawBuffer"> The size of the raw buffer </param> /// <param name="sizeOfByteBuffer"> The size of the byte buffer </param> /// <param name="userArg"> A Reference to the Users arguments </param> /// <param name="messageHandler"> Reference to the user defined message handler function </param> /// <param name="closeHandler"> Reference to the user defined close handler function </param> /// <param name="errorHandler"> Reference to the user defined error handler function </param> public SocketClient(SocketServer socketServer, Socket clientSocket, string ipAddress, int port, int sizeOfRawBuffer, int sizeOfByteBuffer, object userArg, MESSAGE_HANDLER messageHandler, CLOSE_HANDLER closeHandler, ERROR_HANDLER errorHandler) { // Set reference to SocketServer SocketServer = socketServer; // Set when this socket came from a SocketServer Accept ClientSocket = clientSocket; // Set the Ipaddress and Port IpAddress = ipAddress; Port = port; // Set the server index SocketIndex = clientSocket.Handle.ToInt32(); // Set the handler functions MessageHandler = messageHandler; CloseHandler = closeHandler; ErrorHandler = errorHandler; // Create the raw buffer SizeOfRawBuffer = sizeOfRawBuffer; RawBuffer = new byte[SizeOfRawBuffer]; // Save the user argument UserArg = userArg; // Allocate a String Builder class for Application developer use StringBuffer = new StringBuilder(); // Allocate a Memory Stream class for Application developer use MessageBuffer = new MemoryStream(); // Allocate a byte buffer for Application developer use ByteBuffer = new byte[sizeOfByteBuffer]; BufferedBytes = 0; // Allocate a list buffer for Application developer use ListBuffer = new List <byte>(); // Init the NetworkStream reference NetworkStream = new NetworkStream(ClientSocket); // Set the async socket function handlers CallbackReadFunction = new AsyncCallback(ReceiveComplete); CallbackWriteFunction = new AsyncCallback(SendComplete); // Set Available flags IsAvailable = true; // Set the unique key for this object UniqueKey = NewUniqueKey(); // Set these socket options ClientSocket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.Socket, System.Net.Sockets.SocketOptionName.ReceiveBuffer, sizeOfRawBuffer); ClientSocket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.Socket, System.Net.Sockets.SocketOptionName.SendBuffer, sizeOfRawBuffer); ClientSocket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.Socket, System.Net.Sockets.SocketOptionName.KeepAlive, 1); ClientSocket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.Socket, System.Net.Sockets.SocketOptionName.DontLinger, 1); ClientSocket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.Tcp, System.Net.Sockets.SocketOptionName.NoDelay, 1); }
/// <summary> Constructor for SocketServer Suppport </summary> /// <param name="socketServer"> A Reference to the parent SocketServer </param> /// <param name="clientSocket"> The Socket object we are encapsulating </param> /// <param name="ipAddress"> The IpAddress of the remote server </param> /// <param name="port"> The Port of the remote server </param> /// <param name="sizeOfRawBuffer"> The size of the raw buffer </param> /// <param name="sizeOfByteBuffer"> The size of the byte buffer </param> /// <param name="userArg"> A Reference to the Users arguments </param> /// <param name="messageHandler"> Reference to the user defined message handler function </param> /// <param name="closeHandler"> Reference to the user defined close handler function </param> /// <param name="errorHandler"> Reference to the user defined error handler function </param> public SocketClient(SocketServer socketServer, Socket clientSocket, string ipAddress, int port, int sizeOfRawBuffer, int sizeOfByteBuffer, object userArg, MESSAGE_HANDLER messageHandler, CLOSE_HANDLER closeHandler, ERROR_HANDLER errorHandler) { // Set reference to SocketServer SocketServer = socketServer; // Set when this socket came from a SocketServer Accept ClientSocket = clientSocket; // Set the Ipaddress and Port IpAddress = ipAddress; Port = port; // Set the server index SocketIndex = clientSocket.Handle.ToInt32(); // Set the handler functions MessageHandler = messageHandler; CloseHandler = closeHandler; ErrorHandler = errorHandler; // Create the raw buffer SizeOfRawBuffer = sizeOfRawBuffer; RawBuffer = new byte[SizeOfRawBuffer]; // Save the user argument UserArg = userArg; // Allocate a String Builder class for Application developer use StringBuffer = new StringBuilder(); // Allocate a Memory Stream class for Application developer use MessageBuffer = new MemoryStream(); // Allocate a byte buffer for Application developer use ByteBuffer = new byte[sizeOfByteBuffer]; BufferedBytes = 0; // Allocate a list buffer for Application developer use ListBuffer = new List<byte>(); // Init the NetworkStream reference NetworkStream = new NetworkStream(ClientSocket); // Set the async socket function handlers CallbackReadFunction = new AsyncCallback(ReceiveComplete); CallbackWriteFunction = new AsyncCallback(SendComplete); // Set Available flags IsAvailable = true; // Set the unique key for this object UniqueKey = NewUniqueKey(); // Set these socket options ClientSocket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.Socket, System.Net.Sockets.SocketOptionName.ReceiveBuffer, sizeOfRawBuffer); ClientSocket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.Socket, System.Net.Sockets.SocketOptionName.SendBuffer, sizeOfRawBuffer); ClientSocket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.Socket, System.Net.Sockets.SocketOptionName.KeepAlive, 1); ClientSocket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.Socket, System.Net.Sockets.SocketOptionName.DontLinger, 1); ClientSocket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.Tcp, System.Net.Sockets.SocketOptionName.NoDelay, 1); }
/// <summary> Constructor for client support </summary> /// <param name="sizeOfRawBuffer"> The size of the raw buffer </param> /// <param name="sizeOfByteBuffer"> The size of the byte buffer </param> /// <param name="userArg"> A Reference to the Users arguments </param> /// <param name="messageHandler"> Reference to the user defined message handler function </param> /// <param name="closeHandler"> Reference to the user defined close handler function </param> /// <param name="errorHandler"> Reference to the user defined error handler function </param> public SocketClient(int sizeOfRawBuffer, int sizeOfByteBuffer, object userArg, MESSAGE_HANDLER messageHandler, CLOSE_HANDLER closeHandler, ERROR_HANDLER errorHandler) { // Create the raw buffer SizeOfRawBuffer = sizeOfRawBuffer; RawBuffer = new byte[SizeOfRawBuffer]; // Save the user argument UserArg = userArg; // Allocate a String Builder class for Application developer use StringBuffer = new StringBuilder(); // Allocate a Memory Stream class for Application developer use MessageBuffer = new MemoryStream(); // Allocate a byte buffer for Application developer use ByteBuffer = new byte[sizeOfByteBuffer]; BufferedBytes = 0; // Allocate a list buffer for Application developer use ListBuffer = new List<byte>(); // Set the handler functions MessageHandler = messageHandler; CloseHandler = closeHandler; ErrorHandler = errorHandler; // Set the async socket function handlers CallbackReadFunction = new AsyncCallback(ReceiveComplete); CallbackWriteFunction = new AsyncCallback(SendComplete); // Set available flags IsAvailable = true; // Set the unique key for this object UniqueKey = NewUniqueKey(); }