Repository navigation
Expand file tree
/
Copy pathsocket.hpp
More file actions
128 lines (115 loc) · 2.94 KB
/
Copy pathsocket.hpp
File metadata and controls
128 lines (115 loc) · 2.94 KB
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
// Copyright 2016 Yahoo Inc.
// Licensed under the terms of the Apache 2.0 license.
// Please see LICENSE file in the project root for terms.
#ifndef CAFFE_DISTRI_SOCKET_HPP_
#define CAFFE_DISTRI_SOCKET_HPP_
#include <stdio.h>
#include <map>
#include <string>
#include <vector>
#include "caffe/caffe.hpp"
#include "caffe/common.hpp"
#include "caffe/util/blocking_queue.hpp"
using std::vector;
using std::map;
using std::string;
namespace caffe {
class SocketChannel;
class SocketAdapter {
public:
volatile int port;
explicit SocketAdapter(vector<shared_ptr<SocketChannel> > * channels);
vector<shared_ptr<SocketChannel> > *channels;
void start_sockt_srvr();
string address(){
char self_name[256];
char port_buf[256];
gethostname(self_name, 256);
snprintf(port_buf, sizeof(port_buf), "%d", port);
string address = self_name;
address +=":";
address += port_buf;
return address;
}
};
enum message_type {DIFF, DATA};
class QueuedMessage {
public:
message_type type;
int size;
uint8_t* buffer;
QueuedMessage(message_type type, int size, uint8_t* buffer);
};
class SocketBuffer;
class SocketChannel {
private:
int connect_to_peer(string to_peer, string to_port);
public:
SocketChannel();
~SocketChannel();
void Connect(string peer);
int client_fd;
caffe::BlockingQueue<QueuedMessage*> receive_queue;
int serving_fd;
int port_no;
string peer_name;
size_t size;
};
class SocketBuffer {
public:
SocketBuffer(int rank, SocketChannel* channel,
uint8_t* buffer, size_t size, uint8_t* addr);
uint8_t* addr() const {
return addr_;
}
uint8_t* buffer() const {
return buffer_;
}
const size_t size() const {
return size_;
}
// Synchronously writes content to remote peer
void Write();
SocketBuffer* Read();
protected:
SocketChannel* channel_;
uint8_t* addr_;
uint8_t* buffer_;
/*const*/ size_t size_;
int rank;
};
class Socket {
public:
explicit Socket(const string &host, int port, bool listen);
~Socket();
int descriptor() { return fd_; }
shared_ptr<Socket> accept();
size_t read(void *buff, size_t size);
size_t write(void *buff, size_t size);
uint64_t readInt() {
// TODO loop for partial reads or writes
uint64_t value;
CHECK_EQ(read(&value, sizeof(uint64_t)), sizeof(uint64_t));
return value;
}
void writeInt(uint64_t value) {
CHECK_EQ(write(&value, sizeof(uint64_t)), sizeof(uint64_t));
}
string readStr() {
size_t size = readInt();
string str(size, ' ');
CHECK_EQ(read(&str[0], size), size);
return str;
}
void writeStr(const string &str) {
writeInt(str.size());
CHECK_EQ(write(const_cast<void*>(reinterpret_cast<const void *>
(str.c_str())), str.size()), str.size());
}
protected:
explicit Socket(int fd) : fd_(fd) { }
int fd_;
DISABLE_COPY_AND_ASSIGN(Socket);
};
} // namespace caffe
#endif