jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | * use this file except in compliance with the License. You may obtain a copy of |
| 6 | * 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, WITHOUT |
| 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | * License for the specific language governing permissions and limitations under |
| 14 | * the License. |
| 15 | */ |
| 16 | |
| 17 | #include "FreeValueMessage.h" |
| 18 | #include "HostChannel.h" |
| 19 | #include "scoped_ptr/scoped_ptr.h" |
| 20 | |
| 21 | FreeValueMessage::~FreeValueMessage() { |
| 22 | delete[] ids; |
| 23 | } |
| 24 | |
| 25 | FreeValueMessage* FreeValueMessage::receive(HostChannel& channel) { |
| 26 | int idCount; |
| 27 | if (!channel.readInt(idCount)) { |
| 28 | // TODO(jat): error handling |
| 29 | return 0; |
| 30 | } |
| 31 | // TODO: validate idCount |
| 32 | scoped_array<int> ids(new int[idCount]); |
| 33 | |
| 34 | for (int i = 0; i < idCount; ++i) { |
| 35 | if (!channel.readInt(ids[i])) return 0; |
| 36 | } |
| 37 | return new FreeValueMessage(idCount, ids.release()); |
| 38 | } |
| 39 | |
| 40 | bool FreeValueMessage::send(HostChannel& channel, int idCount, const int* ids) { |
| 41 | if (!channel.sendByte(TYPE)) return false; |
| 42 | if (!channel.sendInt(idCount)) return false; |
| 43 | for (int i = 0; i < idCount; ++i) { |
| 44 | if (!channel.sendInt(ids[i])) return false; |
| 45 | } |
| 46 | return true; |
| 47 | } |