tizen 2.3 release
[framework/web/wearable/wrt-security.git] / socket_connection / connection / SocketStream.h
1 /*
2  * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  * @file        SocketStream.h
18  * @author      Zofia Abramowska (z.abramowska@samsung.com)
19  * @version     1.0
20  * @brief       Header of socket stream class.
21  */
22
23 #ifndef SOCKETSTREAM_H_
24 #define SOCKETSTREAM_H_
25
26 #include <string>
27 #include <sys/socket.h>
28 #include <sys/select.h>
29 #include <dpl/serialization.h>
30 #include <dpl/log/log.h>
31
32 /*
33  * This class implements binary read/write from socket used for DPL serialization and deserialization
34  * It can read or write buffers of max *total* size 10kB.
35  * I does not maintain socket descriptor.
36  */
37
38 /*
39  * Throws SocketStreamException when buffer is null or its size exceeds max size or when
40  * there is an error during read or write.
41  */
42
43
44
45 class SocketStream : public DPL::IStream {
46 public:
47     class Exception
48     {
49       public:
50         DECLARE_EXCEPTION_TYPE(DPL::Exception, Base)
51         DECLARE_EXCEPTION_TYPE(Base, SocketStreamException)
52     };
53
54     explicit SocketStream(int socket_fd) : m_socketFd(socket_fd), 
55                                            m_bytesRead(0),
56                                            m_bytesWrote(0)
57     {
58         LogInfo("Created");
59     }
60     void Read(size_t num, void * bytes);
61     void Write(size_t num, const void * bytes);
62 private:
63     void throwWithErrnoMessage(std::string specificInfo);
64     int m_socketFd;
65     int m_bytesRead;
66     int m_bytesWrote;
67 };
68
69 #endif /* SOCKETSTREAM_H_ */