| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package main | 5 package main |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "fmt" | 8 "fmt" |
| 9 "log" | 9 "log" |
| 10 "net" | 10 "net" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 type MojoConnection struct { | 28 type MojoConnection struct { |
| 29 sendStream system.ProducerHandle | 29 sendStream system.ProducerHandle |
| 30 receiveStream system.ConsumerHandle | 30 receiveStream system.ConsumerHandle |
| 31 socket system.MessagePipeHandle | 31 socket system.MessagePipeHandle |
| 32 localAddr net.Addr | 32 localAddr net.Addr |
| 33 remoteAddr net.Addr | 33 remoteAddr net.Addr |
| 34 } | 34 } |
| 35 | 35 |
| 36 // Implements net.Conn. | 36 // Implements net.Conn. |
| 37 func (c *MojoConnection) Read(b []byte) (int, error) { | 37 func (c *MojoConnection) Read(b []byte) (int, error) { |
| 38 r, read := c.receiveStream.BeginReadData(system.MOJO_READ_DATA_FLAG_NONE
) |
| 39 if r != system.MOJO_RESULT_OK { |
| 40 return 0, fmt.Errorf("can't read: %v", r) |
| 41 } |
| 38 l := len(b) | 42 l := len(b) |
| 39 » r, read := c.receiveStream.BeginReadData(l, system.MOJO_READ_DATA_FLAG_N
ONE) | 43 » if (l > len(read)) { |
| 40 » if r != system.MOJO_RESULT_OK { | |
| 41 » » return 0, fmt.Errorf("can't read %v bytes: %v", l, r) | |
| 42 » } | |
| 43 » if l > len(read) { | |
| 44 l = len(read) | 44 l = len(read) |
| 45 } | 45 } |
| 46 copy(b, read[:l]) | 46 copy(b, read[:l]) |
| 47 if r := c.receiveStream.EndReadData(l); r != system.MOJO_RESULT_OK { | 47 if r := c.receiveStream.EndReadData(l); r != system.MOJO_RESULT_OK { |
| 48 return 0, fmt.Errorf("can't read %v bytes: %v", l, r) | 48 return 0, fmt.Errorf("can't read %v bytes: %v", l, r) |
| 49 } | 49 } |
| 50 return l, nil | 50 return l, nil |
| 51 } | 51 } |
| 52 | 52 |
| 53 // Implements net.Conn. | 53 // Implements net.Conn. |
| 54 func (c *MojoConnection) Write(b []byte) (int, error) { | 54 func (c *MojoConnection) Write(b []byte) (int, error) { |
| 55 r, buf := c.sendStream.BeginWriteData(system.MOJO_WRITE_DATA_FLAG_NONE) |
| 56 if r != system.MOJO_RESULT_OK { |
| 57 return 0, fmt.Errorf("can't write: %v", r) |
| 58 } |
| 55 l := len(b) | 59 l := len(b) |
| 56 r, buf := c.sendStream.BeginWriteData(l, system.MOJO_WRITE_DATA_FLAG_NON
E) | |
| 57 if r != system.MOJO_RESULT_OK { | |
| 58 return 0, fmt.Errorf("can't write %v bytes: %v", l, r) | |
| 59 } | |
| 60 if l > len(buf) { | 60 if l > len(buf) { |
| 61 l = len(buf) | 61 l = len(buf) |
| 62 } | 62 } |
| 63 copy(buf, b[:l]) | 63 copy(buf, b[:l]) |
| 64 if r := c.sendStream.EndWriteData(l); r != system.MOJO_RESULT_OK { | 64 if r := c.sendStream.EndWriteData(l); r != system.MOJO_RESULT_OK { |
| 65 return 0, fmt.Errorf("can't write %v bytes: %v", l, r) | 65 return 0, fmt.Errorf("can't write %v bytes: %v", l, r) |
| 66 } | 66 } |
| 67 return l, nil | 67 return l, nil |
| 68 } | 68 } |
| 69 | 69 |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 } | 238 } |
| 239 | 239 |
| 240 //export MojoMain | 240 //export MojoMain |
| 241 func MojoMain(handle C.MojoHandle) C.MojoResult { | 241 func MojoMain(handle C.MojoHandle) C.MojoResult { |
| 242 application.Run(&HttpServerDelegate{}, system.MojoHandle(handle)) | 242 application.Run(&HttpServerDelegate{}, system.MojoHandle(handle)) |
| 243 return C.MOJO_RESULT_OK | 243 return C.MOJO_RESULT_OK |
| 244 } | 244 } |
| 245 | 245 |
| 246 func main() { | 246 func main() { |
| 247 } | 247 } |
| OLD | NEW |