/// <summary> Funciton to remove a socket from the list of sockets </summary> /// <param name="socketClient"> A reference to a socket to remove </param> public void RemoveSocket(SocketClient socketClient) { try { lock (SocketClientList) { // Remove ths client socket object from the list SocketClientList.Remove(socketClient); } } catch (Exception exception) { ErrorHandler(socketClient, exception); } }
/// <summary> Function to process and accept socket connection requests </summary> private void AcceptConnections() { Socket socket = null; try { IPAddress useIpAddress = null; IPHostEntry hostEntries = Dns.GetHostEntry(IpAddress); foreach (IPAddress address in hostEntries.AddressList) { // Find the IPv4 address first if (address.AddressFamily == AddressFamily.InterNetwork) { useIpAddress = address; break; } } // Now just use the first address if (useIpAddress == null) { useIpAddress = hostEntries.AddressList[0]; } IpAddress = useIpAddress.ToString(); // Create a new TCPListner and start it up TcpListener = new TcpListener(useIpAddress, Port); TcpListener.Start(); for (;;) { try { // If a client connects, accept the connection. socket = TcpListener.AcceptSocket(); } catch (System.Net.Sockets.SocketException e) { // Did we stop the TCPListener if (e.ErrorCode != 10004) { // Call the error handler ErrorHandler(null, e); ErrorHandler(null, new Exception("Waiting for new connection 1")); // Close the socket down if it exists if (socket != null) { if (socket.Connected) { socket.Dispose(); } } } else { ErrorHandler(null, new Exception("Shutting Down Accept Thread")); break; } } catch (Exception e) { // Call the error handler ErrorHandler(null, e); ErrorHandler(null, new Exception("Waiting for new connection 2")); // Close the socket down if it exists if (socket != null) { if (socket.Connected) { socket.Dispose(); } } } try { if (socket.Connected) { string remoteEndPoint = socket.RemoteEndPoint.ToString(); // Create a SocketClient object SocketClient clientSocket = new SocketClient(this, socket, (remoteEndPoint.Length < 15) ? string.Empty : remoteEndPoint.Substring(0, 15), Port, SizeOfRawBuffer, SizeOfByteBuffer, UserArg, new SocketClient.MESSAGE_HANDLER(MessageHandler), new SocketClient.CLOSE_HANDLER(CloseHandler), new SocketClient.ERROR_HANDLER(ErrorHandler)); // Add it to the list lock (SocketClientList) { SocketClientList.Add(clientSocket); } // Call the Accept Handler AcceptHandler(clientSocket); // Wait for a message clientSocket.Receive(); } } catch (Exception e) { // Call the error handler ErrorHandler(null, e); ErrorHandler(null, new Exception("Waiting for new connection 3")); } } } catch (Exception e) { // Call the error handler ErrorHandler(null, e); ErrorHandler(null, new Exception("Shutting Down Accept Thread")); // Close the socket down if it exists if (socket != null) { if (socket.Connected) { socket.Dispose(); } } } }
/// <summary> Called when a message is extracted from the socket </summary> /// <param name="socket"> The SocketClient object the message came from </param> private void MessageHandler(SocketClient socket) { try { // Store the raw message data in the buffer socket.MessageBuffer.Write(_SocketClient.RawBuffer, 0, _SocketClient.MessageLength); ProcessReceivedData(); } catch (Exception exception) { OnError("Message Handler", exception); } }
/// <summary> Called when a socket error occurs </summary> /// <param name="socket"> The SocketClient object the message came from </param> /// <param name="exception"> The reason for the error </param> private void ErrorHandler(SocketClient socket, Exception exception) { try { OnError("Error Handler", exception); } catch { } }
/// <summary> Called when a socket connection is closed </summary> /// <param name="socket"> The SocketClient object the message came from </param> private void CloseHandler(SocketClient socket) { try { _Status = ConnectionStatus.Closed; OnDisconnect(); // Wake up this thread if it is waiting EnquireDone.Set(); } catch (Exception exception) { OnError("Close Handler", exception); } }
/// <summary> Constructor for SMSC support </summary> public SmppClient(DataCodings defaultEncoding, SocketClient socketClient) { DefaultEncoding = defaultEncoding; _SocketClient = socketClient; // Mark the connection as open _Status = ConnectionStatus.Open; }
/// <summary> Constructor for ESME support </summary> public SmppClient(DataCodings defaultEncoding) { DefaultEncoding = defaultEncoding; // Create the socket client _SocketClient = new SocketClient(10485760, 0, this, new SocketClient.MESSAGE_HANDLER(MessageHandler), new SocketClient.CLOSE_HANDLER(CloseHandler), new SocketClient.ERROR_HANDLER(ErrorHandler)); _EnquireLinkTimer = new SynchronousTimer(new SynchronousTimer.SynchronousTimerHandler(SendEnquireLink), null, _EnqInterval); }