Created
July 9, 2013 23:34
-
-
Save anonymous/5962257 to your computer and use it in GitHub Desktop.
Server/Client mockup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PacketPool outPool; | |
void client(Address addr) | |
{ | |
Connection conn; | |
// Request a connection | |
ConnectionRequest request(0xDEADBEEF, 2000); | |
ConnectionRequest::Status status; | |
while ((status = request.status()) == ConnectionRequest::WAITING) { | |
std::this_thread::yield(); | |
} | |
if (status == ConnectionRequest::TIMEOUT) | |
fail("Connection request timeout"); | |
else if (status == ConnectionRequest::CONNECTED) | |
conn = request.getConnection(); | |
// Get the channel ID:s | |
channel_id mainIn, mainOut; | |
while (mainIn == 0 || mainOut == 0) { | |
conn.update(); | |
// Create channels from the tag '1' (->1) | |
mainIn = conn.channelInByTag(1); | |
mainOut = conn.channelOutByTag(1); | |
std::this_thread::yield(); | |
} | |
// Send a request | |
{ | |
// Send the requested filename to the main chanel (2->) | |
NetWriter outPacket(outPool.nextData(), outPool.nextSize()); | |
outPacket.write("image.png"); | |
conn.send(mainOut, outPool.allocate(outPacket.amountWritten())); | |
} | |
std::shared_ptr<RecvDataBufferService> srv; | |
while (true) { | |
conn.update(); | |
Packet packet; | |
while (!(packet = conn.receive(mainIn)).empty()) { | |
// Read the received channel (->3) | |
NetReader reader(packet.data(), packet.size()); | |
channel_id chan; | |
reader.read(chan); | |
// Create the receiving service | |
srv = conn.addSerivce(new RecvDataBufferService(chan)); | |
} | |
// If the service has been created, display status | |
if (srv != nullptr) { | |
if (srv->ready()) { | |
displayImage(srv->getData()); | |
} else { | |
displayText(srv->getBytesDownloaded()); | |
} | |
} | |
std::this_thread::yield(); | |
// When the service is created conn.update() will update it | |
// which will receive the file | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PacketPool outPool; | |
struct ConnectionData | |
{ | |
channel_id mainIn, mainOut; | |
}; | |
void new_client(Connection& conn) | |
{ | |
// Open main channels with the tag '1' (1->) | |
conn.data->mainIn = conn.openChannelIn(Channel::SEQUENTIAL, 1); | |
conn.data->mainOut = conn.openChannelOut(Channel::SEQUENTIAL, 1); | |
} | |
void server(unsigned short port) | |
{ | |
// Create the server | |
Server<ConnectionData> server; | |
server = Server<ConnectionData>(port, 0xDEADBEEF); | |
server.client_connect = new_client; | |
while (true) { | |
server.update(); | |
Packet packet; | |
for (const auto& conn : server.connections()) { | |
while (!(packet = conn.receive(conn.data->mainIn)).empty()) { | |
// Read the received filename (->2) | |
NetReader reader(packet.data(), packet.size()); | |
std::string file; | |
reader.read(file); | |
// Create the sending service | |
std::ifstream stream(file); | |
std::shared_ptr<SendStreamService> srv = conn.addService(new SendStreamService(std::move(stream))); | |
// Send the channel the service allocated (3->) | |
NetWriter writer(outPool.nextData(), outPool.nextSize()); | |
writer.write(srv->channel); | |
conn.send(conn.data->mainOut, outPool.allocate(writer.amountWritten())); | |
} | |
} | |
std::this_thread::yield(); | |
// When the service is created conn.update() will update it | |
// which will transfer the file to the client | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment