| 1 | /* |
| 2 | ** CWConnection.h |
| 3 | ** |
| 4 | ** Copyright (c) 2001-2016 Ludovic Marcotte |
| 5 | ** |
| 6 | ** Author: Ludovic Marcotte <[email protected]> |
| 7 | ** |
| 8 | ** This library is free software; you can redistribute it and/or |
| 9 | ** modify it under the terms of the GNU Lesser General Public |
| 10 | ** License as published by the Free Software Foundation; either |
| 11 | ** version 2.1 of the License, or (at your option) any later version. |
| 12 | ** |
| 13 | ** This library is distributed in the hope that it will be useful, |
| 14 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | ** Lesser General Public License for more details. |
| 17 | ** |
| 18 | ** You should have received a copy of the GNU General Public License |
| 19 | ** along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | */ |
| 21 | |
| 22 | #ifndef _Pantomime_H_CWConnection |
| 23 | #define _Pantomime_H_CWConnection |
| 24 | |
| 25 | #include <sys/types.h> |
| 26 | |
| 27 | #import <Foundation/NSData.h> |
| 28 | #import <Foundation/NSString.h> |
| 29 | |
| 30 | /*! |
| 31 | @protocol CWConnection |
| 32 | @discussion This protocol defines a basic set of methods that classes |
| 33 | should implement. CWTCPConnection implements the protocol |
| 34 | to offer TCP connections support. An UDP implementation |
| 35 | will likely be added in a near future (for DNS requests). |
| 36 | */ |
| 37 | @protocol CWConnection |
| 38 | |
| 39 | /*! |
| 40 | @method initWithName: port: background: |
| 41 | @discussion This method is use to initialize a new connection |
| 42 | instance at the specified port. It can connect |
| 43 | in background if needed and use the default timeout |
| 44 | (60 seconds) when connecting. |
| 45 | @param theName The host name to connect to. |
| 46 | @param thePort The port to connect to. |
| 47 | @param theBOOL YES if we want to connect in background (non-blocking |
| 48 | way), NO if we want this call to be blocking until |
| 49 | we successfully connected to the host. |
| 50 | @result An instance implementing the CWConnection protocol, nil |
| 51 | if an error occurred, like DNS resolution. |
| 52 | */ |
| 53 | - (id) initWithName: (NSString *) theName |
| 54 | port: (unsigned int) thePort |
| 55 | background: (BOOL) theBOOL; |
| 56 | |
| 57 | /*! |
| 58 | @method initWithName: port: connectionTimeout: readTimeout: writeTimeout: background: |
| 59 | @discussion Same as -initWithName: port: background but it allows |
| 60 | you to specifed the proper connection / read / write |
| 61 | timeout values to use. |
| 62 | @param theName The host name to connect to. |
| 63 | @param thePort The port to connect to. |
| 64 | @param theConnectionTimeout The timeout to use when connecting to the host. |
| 65 | @param theReadTimeout The timeout to use when reading on the socket. |
| 66 | @param theWriteTimeout The timeout to use when writing on the socket. |
| 67 | @param theBOOL YES if we want to connect in background (non-blocking |
| 68 | way), NO if we want this call to be blocking until |
| 69 | we successfully connected to the host. |
| 70 | @result An instance implementing the CWConnection protocol, nil |
| 71 | if an error occurred, like DNS resolution. |
| 72 | */ |
| 73 | - (id) initWithName: (NSString *) theName |
| 74 | port: (unsigned int) thePort |
| 75 | connectionTimeout: (unsigned int) theConnectionTimeout |
| 76 | readTimeout: (unsigned int) theReadTimeout |
| 77 | writeTimeout: (unsigned int) theWriteTimeout |
| 78 | background: (BOOL) theBOOL; |
| 79 | |
| 80 | /*! |
| 81 | @method fd |
| 82 | @discussion This method is used to obtain the file descriptor |
| 83 | which is associated to our connection. |
| 84 | @result The file descriptor, -1 if the socket isn't yet connected. |
| 85 | */ |
| 86 | - (int) fd; |
| 87 | |
| 88 | /*! |
| 89 | @method isConnected |
| 90 | @discussion This method is used to verify if the socket is |
| 91 | in a connected state. |
| 92 | @result YES if the socket is in a connected state, NO otherwise. |
| 93 | */ |
| 94 | - (BOOL) isConnected; |
| 95 | |
| 96 | /*! |
| 97 | @method close |
| 98 | @discussion This method is used to close the connection to the host. |
| 99 | */ |
| 100 | - (void) close; |
| 101 | |
| 102 | /*! |
| 103 | @method read: length: |
| 104 | @discussion This method is used to read <i>len</i> bytes from the |
| 105 | socket and store them in <i>buf</i> |
| 106 | @param buf The buffer in which read bytes will be stored in. |
| 107 | @param len The number of bytes we want to try to read. |
| 108 | @result The number of bytes successfully read. |
| 109 | */ |
| 110 | - (ssize_t) read: (char *) buf |
| 111 | length: (size_t) len; |
| 112 | |
| 113 | /*! |
| 114 | @method write: length: |
| 115 | @discussion This method is used to write <i>len</i> bytes from |
| 116 | <i>buf</i> to the socket. |
| 117 | @param buf The bytes that we want to write to the socket. |
| 118 | @param len The number of bytes we want to try to write. |
| 119 | @result The number of bytes successfully written. |
| 120 | */ |
| 121 | - (ssize_t) write: (char *) buf |
| 122 | length: (size_t) len; |
| 123 | @end |
| 124 | |
| 125 | #endif // _Pantomime_H_CWConnection |