Mercurial > projects > hoofbaby
comparison deps/Platinum/Source/Core/PltDatagramStream.cpp @ 0:3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
author | fraserofthenight |
---|---|
date | Mon, 06 Jul 2009 08:06:28 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3425707ddbf6 |
---|---|
1 /***************************************************************** | |
2 | | |
3 | Platinum - Datagram Stream | |
4 | | |
5 | Copyright (c) 2004-2008, Plutinosoft, LLC. | |
6 | All rights reserved. | |
7 | http://www.plutinosoft.com | |
8 | | |
9 | This program is free software; you can redistribute it and/or | |
10 | modify it under the terms of the GNU General Public License | |
11 | as published by the Free Software Foundation; either version 2 | |
12 | of the License, or (at your option) any later version. | |
13 | | |
14 | OEMs, ISVs, VARs and other distributors that combine and | |
15 | distribute commercially licensed software with Platinum software | |
16 | and do not wish to distribute the source code for the commercially | |
17 | licensed software under version 2, or (at your option) any later | |
18 | version, of the GNU General Public License (the "GPL") must enter | |
19 | into a commercial license agreement with Plutinosoft, LLC. | |
20 | | |
21 | This program is distributed in the hope that it will be useful, | |
22 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
24 | GNU General Public License for more details. | |
25 | | |
26 | You should have received a copy of the GNU General Public License | |
27 | along with this program; see the file LICENSE.txt. If not, write to | |
28 | the Free Software Foundation, Inc., | |
29 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
30 | http://www.gnu.org/licenses/gpl-2.0.html | |
31 | | |
32 ****************************************************************/ | |
33 | |
34 /*---------------------------------------------------------------------- | |
35 | includes | |
36 +---------------------------------------------------------------------*/ | |
37 #include "PltDatagramStream.h" | |
38 | |
39 /*---------------------------------------------------------------------- | |
40 | PLT_InputDatagramStream::PLT_InputDatagramStream | |
41 +---------------------------------------------------------------------*/ | |
42 PLT_InputDatagramStream::PLT_InputDatagramStream(NPT_UdpSocket* socket) : | |
43 m_Socket(socket) | |
44 { | |
45 } | |
46 | |
47 /*---------------------------------------------------------------------- | |
48 | PLT_InputDatagramStream::~PLT_InputDatagramStream | |
49 +---------------------------------------------------------------------*/ | |
50 PLT_InputDatagramStream::~PLT_InputDatagramStream() | |
51 { | |
52 } | |
53 | |
54 /*---------------------------------------------------------------------- | |
55 | PLT_InputDatagramStream::Read | |
56 +---------------------------------------------------------------------*/ | |
57 NPT_Result | |
58 PLT_InputDatagramStream::Read(void* buffer, | |
59 NPT_Size bytes_to_read, | |
60 NPT_Size* bytes_read /*= 0*/) | |
61 { | |
62 | |
63 if (bytes_read) *bytes_read = 0; | |
64 | |
65 if (bytes_to_read == 0) { | |
66 return NPT_SUCCESS; | |
67 } | |
68 | |
69 NPT_DataBuffer data_buffer(buffer, bytes_to_read, false); | |
70 | |
71 // read data into it now | |
72 NPT_SocketAddress addr; | |
73 NPT_Result res = m_Socket->Receive(data_buffer, &addr); | |
74 | |
75 // update info | |
76 m_Socket->GetInfo(m_Info); | |
77 m_Info.remote_address = addr; | |
78 | |
79 if (bytes_read) *bytes_read = data_buffer.GetDataSize(); | |
80 | |
81 return res; | |
82 } | |
83 | |
84 /*---------------------------------------------------------------------- | |
85 | PLT_OutputDatagramStream::GetInfo | |
86 +---------------------------------------------------------------------*/ | |
87 NPT_Result | |
88 PLT_InputDatagramStream::GetInfo(NPT_SocketInfo& info) | |
89 { | |
90 info = m_Info; | |
91 return NPT_SUCCESS; | |
92 } | |
93 | |
94 /*---------------------------------------------------------------------- | |
95 | PLT_OutputDatagramStream::PLT_OutputDatagramStream | |
96 +---------------------------------------------------------------------*/ | |
97 PLT_OutputDatagramStream::PLT_OutputDatagramStream(NPT_UdpSocket* socket, | |
98 NPT_Size size, | |
99 const NPT_SocketAddress* address) : | |
100 m_Socket(socket), | |
101 m_Address(address?new NPT_SocketAddress(address->GetIpAddress(), address->GetPort()):NULL) | |
102 { | |
103 m_Buffer.SetBufferSize(size); | |
104 } | |
105 | |
106 /*---------------------------------------------------------------------- | |
107 | PLT_OutputDatagramStream::~PLT_OutputDatagramStream | |
108 +---------------------------------------------------------------------*/ | |
109 PLT_OutputDatagramStream::~PLT_OutputDatagramStream() | |
110 { | |
111 delete m_Address; | |
112 } | |
113 | |
114 /*---------------------------------------------------------------------- | |
115 | PLT_OutputDatagramStream::Write | |
116 +---------------------------------------------------------------------*/ | |
117 NPT_Result | |
118 PLT_OutputDatagramStream::Write(const void* buffer, NPT_Size bytes_to_write, NPT_Size* bytes_written /* = NULL */) | |
119 { | |
120 // calculate if we need to increase the buffer | |
121 NPT_Int32 overflow = bytes_to_write - m_Buffer.GetBufferSize() + m_Buffer.GetDataSize(); | |
122 if (overflow > 0) { | |
123 m_Buffer.Reserve(m_Buffer.GetBufferSize() + overflow); | |
124 } | |
125 // copy data in place at the end of what we have there already | |
126 NPT_CopyMemory(m_Buffer.UseData() + m_Buffer.GetDataSize(), buffer, bytes_to_write); | |
127 m_Buffer.SetDataSize(m_Buffer.GetDataSize() + bytes_to_write); | |
128 | |
129 if (bytes_written) *bytes_written = bytes_to_write; | |
130 return NPT_SUCCESS; | |
131 } | |
132 | |
133 /*---------------------------------------------------------------------- | |
134 | PLT_OutputDatagramStream::Flush | |
135 +---------------------------------------------------------------------*/ | |
136 NPT_Result | |
137 PLT_OutputDatagramStream::Flush() | |
138 { | |
139 // send buffer now | |
140 m_Socket->Send(m_Buffer, m_Address); | |
141 | |
142 // reset buffer | |
143 m_Buffer.SetDataSize(0); | |
144 return NPT_SUCCESS; | |
145 } |