LEFT | RIGHT |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // UDP sockets | 5 // UDP sockets |
6 | 6 |
7 package net | 7 package net |
8 | 8 |
9 import ( | 9 import ( |
10 "os"; | 10 "os"; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 } | 66 } |
67 | 67 |
68 // UDPConn is the implementation of the Conn and PacketConn | 68 // UDPConn is the implementation of the Conn and PacketConn |
69 // interfaces for UDP network connections. | 69 // interfaces for UDP network connections. |
70 type UDPConn struct { | 70 type UDPConn struct { |
71 fd *netFD; | 71 fd *netFD; |
72 } | 72 } |
73 | 73 |
74 func newUDPConn(fd *netFD) *UDPConn { | 74 func newUDPConn(fd *netFD) *UDPConn { |
75 c := &UDPConn{fd}; | 75 c := &UDPConn{fd}; |
76 fd.getref(); | |
77 defer fd.delref(); | |
78 setsockoptInt(fd.sysfd, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1); | 76 setsockoptInt(fd.sysfd, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1); |
79 return c; | 77 return c; |
80 } | 78 } |
81 | 79 |
82 func (c *UDPConn) ok() bool { return c != nil && c.fd != nil } | 80 func (c *UDPConn) ok() bool { return c != nil && c.fd != nil } |
83 | 81 |
84 // Implementation of the Conn interface - see Conn for documentation. | 82 // Implementation of the Conn interface - see Conn for documentation. |
85 | 83 |
86 // Read reads data from a single UDP packet on the connection. | 84 // Read reads data from a single UDP packet on the connection. |
87 // If the slice b is smaller than the arriving packet, | 85 // If the slice b is smaller than the arriving packet, |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 } | 279 } |
282 if laddr == nil { | 280 if laddr == nil { |
283 return nil, &OpError{"listen", "udp", nil, errMissingAddress} | 281 return nil, &OpError{"listen", "udp", nil, errMissingAddress} |
284 } | 282 } |
285 fd, e := internetSocket(net, laddr.toAddr(), nil, syscall.SOCK_DGRAM, "d
ial", sockaddrToUDP); | 283 fd, e := internetSocket(net, laddr.toAddr(), nil, syscall.SOCK_DGRAM, "d
ial", sockaddrToUDP); |
286 if e != nil { | 284 if e != nil { |
287 return nil, e | 285 return nil, e |
288 } | 286 } |
289 return newUDPConn(fd), nil; | 287 return newUDPConn(fd), nil; |
290 } | 288 } |
LEFT | RIGHT |