1 /* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  *     Copyright 2013 Couchbase, Inc.
4  *
5  *   Licensed under the Apache License, Version 2.0 (the "License");
6  *   you may not use this file except in compliance with the License.
7  *   You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *   Unless required by applicable law or agreed to in writing, software
12  *   distributed under the License is distributed on an "AS IS" BASIS,
13  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *   See the License for the specific language governing permissions and
15  *   limitations under the License.
16  */
17 #include <platform/platform_socket.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 
21 PLATFORM_PUBLIC_API
cb_initialize_sockets(void)22 void cb_initialize_sockets(void)
23 {
24     WSADATA wsaData;
25     if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0) {
26         fprintf(stderr, "Socket Initialization Error. Program aborted\r\n");
27         exit(EXIT_FAILURE);
28     }
29 }
30 
31 PLATFORM_PUBLIC_API
sendmsg(SOCKET sock, const struct msghdr *msg, int flags)32 int sendmsg(SOCKET sock, const struct msghdr *msg, int flags)
33 {
34     /* @todo make this more optimal! */
35     int ii;
36     int ret = 0;
37 
38     for (ii = 0; ii < msg->msg_iovlen; ++ii) {
39         if (msg->msg_iov[ii].iov_len > 0) {
40             int nw = send(sock, msg->msg_iov[ii].iov_base, (int)msg->msg_iov[ii].iov_len, flags);
41             if (nw > 0) {
42                 ret += nw;
43                 if (nw != msg->msg_iov[ii].iov_len) {
44                     return ret;
45                 }
46             } else {
47                 if (ret > 0) {
48                     return ret;
49                 }
50                 return nw;
51             }
52         }
53     }
54 
55     return ret;
56 }
57