]> git.proxmox.com Git - mirror_edk2.git/blob - StdLib/BsdSocketLib/send.c
Add Socket Libraries.
[mirror_edk2.git] / StdLib / BsdSocketLib / send.c
1 /** @file
2 Implement the send API.
3
4 Copyright (c) 2011, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include <SocketInternals.h>
16
17
18 /**
19 Send data using a network connection.
20
21 The ::send routine queues data to the network for transmission.
22 The
23 <a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/send.html">POSIX</a>
24 documentation is available online.
25
26 @param [in] s Socket file descriptor returned from ::socket.
27
28 @param [in] buffer Address of a buffer containing the data to send.
29
30 @param [in] length Length of the buffer in bytes.
31
32 @param [in] flags Message control flags
33
34 @returns ::send returns the number of data bytes that were
35 sent and -1 when an error occurs. In the case of
36 an error, errno contains more details.
37
38 **/
39 ssize_t
40 send (
41 int s,
42 CONST void * buffer,
43 size_t length,
44 int flags
45 )
46 {
47 ssize_t LengthInBytes;
48 CONST UINT8 * pData;
49 struct __filedes * pDescriptor;
50 EFI_SOCKET_PROTOCOL * pSocketProtocol;
51 EFI_STATUS Status;
52
53 //
54 // Assume failure
55 //
56 LengthInBytes = -1;
57
58 //
59 // Locate the context for this socket
60 //
61 pSocketProtocol = BslFdToSocketProtocol ( s,
62 &pDescriptor,
63 &errno );
64 if ( NULL != pSocketProtocol ) {
65 //
66 // Send the data using the socket
67 //
68 pData = buffer;
69 do {
70 errno = 0;
71 Status = pSocketProtocol->pfnSend ( pSocketProtocol,
72 flags,
73 length,
74 pData,
75 (size_t *)&LengthInBytes,
76 NULL,
77 0,
78 &errno );
79 if ( EFI_ERROR ( Status )) {
80 LengthInBytes = -1;
81 break;
82 }
83
84 //
85 // Account for the data sent
86 //
87 pData += LengthInBytes;
88 length -= LengthInBytes;
89 // TODO: Add non-blocking check
90 } while (( 0 != length ) && ( EFI_NOT_READY == Status ));
91 }
92
93 //
94 // Return the number of data bytes sent, -1 for errors
95 //
96 return (INT32)LengthInBytes;
97 }