aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/ignite/kernels/ignite_plain_client_windows.cc
blob: f78c9b36278fc623b1ef5a8e0a7335f026b4a553 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "ignite_plain_client.h"

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>

#pragma comment(lib, "Ws2_32.lib")
#pragma comment(lib, "Mswsock.lib")
#pragma comment(lib, "AdvApi32.lib")

#include "tensorflow/core/lib/core/errors.h"
#include "tensorflow/core/platform/logging.h"

namespace ignite {

PlainClient::PlainClient(std::string host, int port)
    : host(host), port(port), sock(INVALID_SOCKET) {}

PlainClient::~PlainClient() {
  if (IsConnected()) {
    tensorflow::Status status = Disconnect();
    if (!status.ok()) LOG(WARNING) << status.ToString();
  }
}

tensorflow::Status PlainClient::Connect() {
  WSADATA wsaData;
  addrinfo *result = NULL, *ptr = NULL, hints;

  int res = WSAStartup(MAKEWORD(2, 2), &wsaData);
  if (res != 0)
    return tensorflow::errors::Internal("WSAStartup failed with error: ", res);

  ZeroMemory(&hints, sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = IPPROTO_TCP;

  res =
      getaddrinfo(host.c_str(), std::to_string(port).c_str(), &hints, &result);
  if (res != 0)
    return tensorflow::errors::Internal("Getaddrinfo failed with error: ", res);

  for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
    sock = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
    if (sock == INVALID_SOCKET) {
      WSACleanup();
      return tensorflow::errors::Internal("Socket failed with error: ",
                                          WSAGetLastError());
    }

    res = connect(sock, ptr->ai_addr, (int)ptr->ai_addrlen);
    if (res == SOCKET_ERROR) {
      closesocket(sock);
      sock = INVALID_SOCKET;
      continue;
    }

    break;
  }

  freeaddrinfo(result);

  if (sock == INVALID_SOCKET) {
    WSACleanup();
    return tensorflow::errors::Internal("Unable to connect to server");
  }

  LOG(INFO) << "Connection to \"" << host << ":" << port << "\" established";

  return tensorflow::Status::OK();
}

tensorflow::Status PlainClient::Disconnect() {
  int res = shutdown(sock, SD_SEND);
  closesocket(sock);
  WSACleanup();

  if (res == SOCKET_ERROR)
    return tensorflow::errors::Internal("Shutdown failed with error: ",
                                        WSAGetLastError());
  else
    return tensorflow::Status::OK();
}

bool PlainClient::IsConnected() { return sock != INVALID_SOCKET; }

int PlainClient::GetSocketDescriptor() { return sock; }

tensorflow::Status PlainClient::ReadData(uint8_t *buf, int32_t length) {
  int recieved = 0;

  while (recieved < length) {
    int res = recv(sock, buf, length - recieved, 0);

    if (res < 0)
      return tensorflow::errors::Internal(
          "Error occured while reading from socket: ", res);

    if (res == 0)
      return tensorflow::errors::Internal("Server closed connection");

    recieved += res;
    buf += res;
  }

  return tensorflow::Status::OK();
}

tensorflow::Status PlainClient::WriteData(uint8_t *buf, int32_t length) {
  int sent = 0;

  while (sent < length) {
    int res = send(sock, buf, length - sent, 0);

    if (res < 0)
      return tensorflow::errors::Internal(
          "Error occured while writing into socket: ", res);

    sent += res;
    buf += res;
  }

  return tensorflow::Status::OK();
}

}  // namespace ignite