OLD | NEW |
1 // Copyright 2010 The Go Authors. All rights reserved. | 1 // Copyright 2010 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 package net | 5 package net |
6 | 6 |
7 import ( | 7 import ( |
8 "errors" | 8 "errors" |
9 "io" | 9 "io" |
10 "os" | 10 "os" |
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 family int | 278 family int |
279 sotype int | 279 sotype int |
280 isConnected bool | 280 isConnected bool |
281 net string | 281 net string |
282 laddr Addr | 282 laddr Addr |
283 raddr Addr | 283 raddr Addr |
284 resultc [2]chan ioResult // read/write completion results | 284 resultc [2]chan ioResult // read/write completion results |
285 errnoc [2]chan error // read/write submit or cancel operation er
rors | 285 errnoc [2]chan error // read/write submit or cancel operation er
rors |
286 closec chan bool // used by Close to cancel pending IO | 286 closec chan bool // used by Close to cancel pending IO |
287 | 287 |
288 » // serialize access to Read and Write methods | 288 » // owned by client |
289 » rio, wio sync.Mutex | 289 » rdeadline int64 |
290 | 290 » rio sync.Mutex |
291 » // read and write deadlines | 291 » wdeadline int64 |
292 » rdeadline, wdeadline deadline | 292 » wio sync.Mutex |
293 } | |
294 | |
295 // deadline is a number of nanoseconds since 1970 or 0, if no deadline is set. | |
296 // For compatability, deadline has the same method set as fd_unix.go, but | |
297 // does not use atomic operations as it is not known if data races exist on | |
298 // these values. | |
299 // TODO(dfc,brainman) when we get a windows race builder, revisit this. | |
300 type deadline int64 | |
301 | |
302 func (d *deadline) expired() bool { | |
303 » t := d.value() | |
304 » return t > 0 && time.Now().UnixNano() >= t | |
305 } | |
306 | |
307 func (d *deadline) value() int64 { | |
308 » return int64(*d) | |
309 } | |
310 | |
311 func (d *deadline) set(v int64) { | |
312 » *d = deadline(v) | |
313 } | |
314 | |
315 func (d *deadline) setTime(t time.Time) { | |
316 » if t.IsZero() { | |
317 » » d.set(0) | |
318 » } else { | |
319 » » d.set(t.UnixNano()) | |
320 » } | |
321 } | 293 } |
322 | 294 |
323 func allocFD(fd syscall.Handle, family, sotype int, net string) *netFD { | 295 func allocFD(fd syscall.Handle, family, sotype int, net string) *netFD { |
324 netfd := &netFD{ | 296 netfd := &netFD{ |
325 sysfd: fd, | 297 sysfd: fd, |
326 family: family, | 298 family: family, |
327 sotype: sotype, | 299 sotype: sotype, |
328 net: net, | 300 net: net, |
329 closec: make(chan bool), | 301 closec: make(chan bool), |
330 } | 302 } |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
443 | 415 |
444 func (fd *netFD) Read(buf []byte) (int, error) { | 416 func (fd *netFD) Read(buf []byte) (int, error) { |
445 if err := fd.incref(false); err != nil { | 417 if err := fd.incref(false); err != nil { |
446 return 0, err | 418 return 0, err |
447 } | 419 } |
448 defer fd.decref() | 420 defer fd.decref() |
449 fd.rio.Lock() | 421 fd.rio.Lock() |
450 defer fd.rio.Unlock() | 422 defer fd.rio.Unlock() |
451 var o readOp | 423 var o readOp |
452 o.Init(fd, buf, 'r') | 424 o.Init(fd, buf, 'r') |
453 » n, err := iosrv.ExecIO(&o, fd.rdeadline.value()) | 425 » n, err := iosrv.ExecIO(&o, fd.rdeadline) |
454 if err == nil && n == 0 { | 426 if err == nil && n == 0 { |
455 err = io.EOF | 427 err = io.EOF |
456 } | 428 } |
457 return n, err | 429 return n, err |
458 } | 430 } |
459 | 431 |
460 // ReadFrom from network. | 432 // ReadFrom from network. |
461 | 433 |
462 type readFromOp struct { | 434 type readFromOp struct { |
463 bufOp | 435 bufOp |
(...skipping 16 matching lines...) Expand all Loading... |
480 } | 452 } |
481 if err := fd.incref(false); err != nil { | 453 if err := fd.incref(false); err != nil { |
482 return 0, nil, err | 454 return 0, nil, err |
483 } | 455 } |
484 defer fd.decref() | 456 defer fd.decref() |
485 fd.rio.Lock() | 457 fd.rio.Lock() |
486 defer fd.rio.Unlock() | 458 defer fd.rio.Unlock() |
487 var o readFromOp | 459 var o readFromOp |
488 o.Init(fd, buf, 'r') | 460 o.Init(fd, buf, 'r') |
489 o.rsan = int32(unsafe.Sizeof(o.rsa)) | 461 o.rsan = int32(unsafe.Sizeof(o.rsa)) |
490 » n, err = iosrv.ExecIO(&o, fd.rdeadline.value()) | 462 » n, err = iosrv.ExecIO(&o, fd.rdeadline) |
491 if err != nil { | 463 if err != nil { |
492 return 0, nil, err | 464 return 0, nil, err |
493 } | 465 } |
494 sa, _ = o.rsa.Sockaddr() | 466 sa, _ = o.rsa.Sockaddr() |
495 return | 467 return |
496 } | 468 } |
497 | 469 |
498 // Write to network. | 470 // Write to network. |
499 | 471 |
500 type writeOp struct { | 472 type writeOp struct { |
(...skipping 11 matching lines...) Expand all Loading... |
512 | 484 |
513 func (fd *netFD) Write(buf []byte) (int, error) { | 485 func (fd *netFD) Write(buf []byte) (int, error) { |
514 if err := fd.incref(false); err != nil { | 486 if err := fd.incref(false); err != nil { |
515 return 0, err | 487 return 0, err |
516 } | 488 } |
517 defer fd.decref() | 489 defer fd.decref() |
518 fd.wio.Lock() | 490 fd.wio.Lock() |
519 defer fd.wio.Unlock() | 491 defer fd.wio.Unlock() |
520 var o writeOp | 492 var o writeOp |
521 o.Init(fd, buf, 'w') | 493 o.Init(fd, buf, 'w') |
522 » return iosrv.ExecIO(&o, fd.wdeadline.value()) | 494 » return iosrv.ExecIO(&o, fd.wdeadline) |
523 } | 495 } |
524 | 496 |
525 // WriteTo to network. | 497 // WriteTo to network. |
526 | 498 |
527 type writeToOp struct { | 499 type writeToOp struct { |
528 bufOp | 500 bufOp |
529 sa syscall.Sockaddr | 501 sa syscall.Sockaddr |
530 } | 502 } |
531 | 503 |
532 func (o *writeToOp) Submit() error { | 504 func (o *writeToOp) Submit() error { |
(...skipping 11 matching lines...) Expand all Loading... |
544 } | 516 } |
545 if err := fd.incref(false); err != nil { | 517 if err := fd.incref(false); err != nil { |
546 return 0, err | 518 return 0, err |
547 } | 519 } |
548 defer fd.decref() | 520 defer fd.decref() |
549 fd.wio.Lock() | 521 fd.wio.Lock() |
550 defer fd.wio.Unlock() | 522 defer fd.wio.Unlock() |
551 var o writeToOp | 523 var o writeToOp |
552 o.Init(fd, buf, 'w') | 524 o.Init(fd, buf, 'w') |
553 o.sa = sa | 525 o.sa = sa |
554 » return iosrv.ExecIO(&o, fd.wdeadline.value()) | 526 » return iosrv.ExecIO(&o, fd.wdeadline) |
555 } | 527 } |
556 | 528 |
557 // Accept new network connections. | 529 // Accept new network connections. |
558 | 530 |
559 type acceptOp struct { | 531 type acceptOp struct { |
560 anOp | 532 anOp |
561 newsock syscall.Handle | 533 newsock syscall.Handle |
562 attrs [2]syscall.RawSockaddrAny // space for local and remote address
only | 534 attrs [2]syscall.RawSockaddrAny // space for local and remote address
only |
563 } | 535 } |
564 | 536 |
(...skipping 28 matching lines...) Expand all Loading... |
593 // Associate our new socket with IOCP. | 565 // Associate our new socket with IOCP. |
594 onceStartServer.Do(startServer) | 566 onceStartServer.Do(startServer) |
595 if _, err := syscall.CreateIoCompletionPort(s, resultsrv.iocp, 0, 0); er
r != nil { | 567 if _, err := syscall.CreateIoCompletionPort(s, resultsrv.iocp, 0, 0); er
r != nil { |
596 return nil, &OpError{"CreateIoCompletionPort", fd.net, fd.laddr,
err} | 568 return nil, &OpError{"CreateIoCompletionPort", fd.net, fd.laddr,
err} |
597 } | 569 } |
598 | 570 |
599 // Submit accept request. | 571 // Submit accept request. |
600 var o acceptOp | 572 var o acceptOp |
601 o.Init(fd, 'r') | 573 o.Init(fd, 'r') |
602 o.newsock = s | 574 o.newsock = s |
603 » _, err = iosrv.ExecIO(&o, fd.rdeadline.value()) | 575 » _, err = iosrv.ExecIO(&o, fd.rdeadline) |
604 if err != nil { | 576 if err != nil { |
605 closesocket(s) | 577 closesocket(s) |
606 return nil, err | 578 return nil, err |
607 } | 579 } |
608 | 580 |
609 // Inherit properties of the listening socket. | 581 // Inherit properties of the listening socket. |
610 err = syscall.Setsockopt(s, syscall.SOL_SOCKET, syscall.SO_UPDATE_ACCEPT
_CONTEXT, (*byte)(unsafe.Pointer(&fd.sysfd)), int32(unsafe.Sizeof(fd.sysfd))) | 582 err = syscall.Setsockopt(s, syscall.SOL_SOCKET, syscall.SO_UPDATE_ACCEPT
_CONTEXT, (*byte)(unsafe.Pointer(&fd.sysfd)), int32(unsafe.Sizeof(fd.sysfd))) |
611 if err != nil { | 583 if err != nil { |
612 closesocket(s) | 584 closesocket(s) |
613 return nil, err | 585 return nil, err |
(...skipping 22 matching lines...) Expand all Loading... |
636 | 608 |
637 var errNoSupport = errors.New("address family not supported") | 609 var errNoSupport = errors.New("address family not supported") |
638 | 610 |
639 func (fd *netFD) ReadMsg(p []byte, oob []byte) (n, oobn, flags int, sa syscall.S
ockaddr, err error) { | 611 func (fd *netFD) ReadMsg(p []byte, oob []byte) (n, oobn, flags int, sa syscall.S
ockaddr, err error) { |
640 return 0, 0, 0, nil, errNoSupport | 612 return 0, 0, 0, nil, errNoSupport |
641 } | 613 } |
642 | 614 |
643 func (fd *netFD) WriteMsg(p []byte, oob []byte, sa syscall.Sockaddr) (n int, oob
n int, err error) { | 615 func (fd *netFD) WriteMsg(p []byte, oob []byte, sa syscall.Sockaddr) (n int, oob
n int, err error) { |
644 return 0, 0, errNoSupport | 616 return 0, 0, errNoSupport |
645 } | 617 } |
OLD | NEW |