-public:
-
- /**
- * The default constructor does nothing
- * and should not be used.
- */
- InspSocket();
-
- /**
- * This constructor is used to associate
- * an existing connecting with an InspSocket
- * class. The given file descriptor must be
- * valid, and when initialized, the InspSocket
- * will be set with the given IP address
- * and placed in CONNECTED state.
- */
- InspSocket(int newfd, char* ip);
-
- /**
- * This constructor is used to create a new
- * socket, either listening for connections, or an outbound connection to another host.
- * Note that if you specify a hostname in the 'host' parameter, then there will be an extra
- * step involved (a nonblocking DNS lookup) which will cause your connection to be established
- * slower than if it was an IP. Therefore, use an IP address where it is available instead.
- * @param host The hostname to connect to, or bind to
- * @param port The port number to connect to, or bind to
- * @param listening true to listen on the given host:port pair, or false to connect to them
- * @param maxtime Number of seconds to wait, if connecting, before the connection times out and an OnTimeout() event is generated
- */
- InspSocket(std::string host, int port, bool listening, unsigned long maxtime);
-
- /**
- * This method is called when an outbound
- * connection on your socket is completed.
- * @return false to abort the connection, true to continue
- */
- virtual bool OnConnected();
-
- /**
- * This method is called when an error occurs.
- * A closed socket in itself is not an error,
- * however errors also generate close events.
- * @param e The error type which occured
- */
- virtual void OnError(InspSocketError e);
-
- /**
- * When an established connection is terminated,
- * the OnDisconnect method is triggered.
- */
- virtual int OnDisconnect();
-
- /**
- * When there is data waiting to be read on a
- * socket, the OnDataReady() method is called.
- * Within this method, you *MUST* call the Read()
- * method to read any pending data. At its lowest
- * level, this event is signalled by the core via
- * the socket engine. If you return false from this
- * function, the core removes your socket from its
- * list and erases it from the socket engine, then
- * calls InspSocket::Close() and deletes it.
- * @return false to close the socket
- */
- virtual bool OnDataReady();
-
- /**
- * When an outbound connection fails, and the
- * attempt times out, you will receive this event.
- * The method will trigger once maxtime seconds are
- * reached (as given in the constructor) just
- * before the socket's descriptor is closed.
- * A failed DNS lookup may cause this event if
- * the DNS server is not responding, as well as
- * a failed connect() call, because DNS lookups are
- * nonblocking as implemented by this class.
- */
- virtual void OnTimeout();
-
- /**
- * Whenever close() is called, OnClose() will be
- * called first. Please note that this means
- * OnClose will be called alongside OnError(),
- * OnTimeout(), and Close(), and also when
- * cancelling a listening socket by calling
- * the destructor indirectly.
- */
- virtual void OnClose();
-
- /**
- * Reads all pending bytes from the socket
- * into a char* array which can be up to
- * 16 kilobytes in length.
- */
- virtual char* Read();
-
- /**
- * Returns the IP address associated with
- * this connection, or an empty string if
- * no IP address exists.
- */
- std::string GetIP();
-
- /**
- * This function checks if the socket has
- * timed out yet, given the current time
- * in the parameter.
- * @return true if timed out, false if not timed out
- */
- bool Timeout(time_t current);
-
- /**
- * Writes a std::string to the socket. No carriage
- * returns or linefeeds are appended to the string.
- * @param data The data to send
- */
- virtual int Write(std::string data);
-
- /**
- * If your socket is a listening socket, when a new
- * connection comes in on the socket this method will
- * be called. Given the new file descriptor in the
- * parameters, and the IP, it is recommended you copy
- * them to a new instance of your socket class,
- * e.g.:
- *
- * MySocket* newsocket = new MySocket(newfd,ip);
- *
- * Once you have done this, you can then associate the
- * new socket with the core using Server::AddSocket().
- */
- virtual int OnIncomingConnection(int newfd, char* ip);
-
- /**
- * Changes the socket's state. The core uses this
- * to change socket states, and you should not call
- * it directly.
- */
- void SetState(InspSocketState s);