A simple class to do line-based TCP communication. To be used with the NetServicesMin library-.
tcplinestream.h@1:28416a5a4ec5, 2011-02-18 (annotated)
- Committer:
- hlipka
- Date:
- Fri Feb 18 13:15:43 2011 +0000
- Revision:
- 1:28416a5a4ec5
- Parent:
- 0:f2727a16072f
added license
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hlipka | 1:28416a5a4ec5 | 1 | /* |
hlipka | 1:28416a5a4ec5 | 2 | * TcpLineStream library |
hlipka | 1:28416a5a4ec5 | 3 | * Copyright (c) 2010 Hendrik Lipka |
hlipka | 1:28416a5a4ec5 | 4 | * |
hlipka | 1:28416a5a4ec5 | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
hlipka | 1:28416a5a4ec5 | 6 | * of this software and associated documentation files (the "Software"), to deal |
hlipka | 1:28416a5a4ec5 | 7 | * in the Software without restriction, including without limitation the rights |
hlipka | 1:28416a5a4ec5 | 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
hlipka | 1:28416a5a4ec5 | 9 | * copies of the Software, and to permit persons to whom the Software is |
hlipka | 1:28416a5a4ec5 | 10 | * furnished to do so, subject to the following conditions: |
hlipka | 1:28416a5a4ec5 | 11 | * |
hlipka | 1:28416a5a4ec5 | 12 | * The above copyright notice and this permission notice shall be included in |
hlipka | 1:28416a5a4ec5 | 13 | * all copies or substantial portions of the Software. |
hlipka | 1:28416a5a4ec5 | 14 | * |
hlipka | 1:28416a5a4ec5 | 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
hlipka | 1:28416a5a4ec5 | 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
hlipka | 1:28416a5a4ec5 | 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
hlipka | 1:28416a5a4ec5 | 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
hlipka | 1:28416a5a4ec5 | 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
hlipka | 1:28416a5a4ec5 | 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
hlipka | 1:28416a5a4ec5 | 21 | * THE SOFTWARE. |
hlipka | 1:28416a5a4ec5 | 22 | */ |
hlipka | 1:28416a5a4ec5 | 23 | |
hlipka | 0:f2727a16072f | 24 | #ifndef __TCPLINESTREAM__H__ |
hlipka | 0:f2727a16072f | 25 | #define __TCPLINESTREAM__H__ |
hlipka | 0:f2727a16072f | 26 | |
hlipka | 0:f2727a16072f | 27 | #include "TCPSocket.h" |
hlipka | 0:f2727a16072f | 28 | #include "string" |
hlipka | 0:f2727a16072f | 29 | |
hlipka | 0:f2727a16072f | 30 | #define BUFSIZE 1024 |
hlipka | 0:f2727a16072f | 31 | |
hlipka | 0:f2727a16072f | 32 | using namespace std; |
hlipka | 0:f2727a16072f | 33 | |
hlipka | 0:f2727a16072f | 34 | class TCPLineStream |
hlipka | 0:f2727a16072f | 35 | { |
hlipka | 0:f2727a16072f | 36 | public: |
hlipka | 0:f2727a16072f | 37 | TCPLineStream(const char *host, int port, const char* _term="\r\n"); |
hlipka | 0:f2727a16072f | 38 | ~TCPLineStream(){close();}; |
hlipka | 0:f2727a16072f | 39 | bool open(); |
hlipka | 0:f2727a16072f | 40 | void close(); |
hlipka | 0:f2727a16072f | 41 | |
hlipka | 0:f2727a16072f | 42 | string readLine(int strLen=64); |
hlipka | 0:f2727a16072f | 43 | void sendLine(string line); |
hlipka | 0:f2727a16072f | 44 | |
hlipka | 0:f2727a16072f | 45 | private: |
hlipka | 0:f2727a16072f | 46 | void init(); |
hlipka | 0:f2727a16072f | 47 | void onTCPSocketEvent(TCPSocketEvent e); |
hlipka | 0:f2727a16072f | 48 | |
hlipka | 0:f2727a16072f | 49 | const char *_server; |
hlipka | 0:f2727a16072f | 50 | int _port; |
hlipka | 0:f2727a16072f | 51 | |
hlipka | 0:f2727a16072f | 52 | bool _connecting; |
hlipka | 0:f2727a16072f | 53 | bool _connected; |
hlipka | 0:f2727a16072f | 54 | bool _closed; |
hlipka | 0:f2727a16072f | 55 | TCPSocket *_socket; |
hlipka | 0:f2727a16072f | 56 | |
hlipka | 0:f2727a16072f | 57 | char _readbuf[BUFSIZE]; |
hlipka | 0:f2727a16072f | 58 | int _readpos; |
hlipka | 0:f2727a16072f | 59 | int _readlen; |
hlipka | 0:f2727a16072f | 60 | |
hlipka | 0:f2727a16072f | 61 | const char* _term; |
hlipka | 0:f2727a16072f | 62 | }; |
hlipka | 0:f2727a16072f | 63 | |
hlipka | 0:f2727a16072f | 64 | #endif |