bool TCPClientPort::Connect(const char *host, unsigned port) { NarrowString<32> service; service.UnsafeFormat("%u", port); StaticSocketAddress address; if (!address.Lookup(host, service, AF_INET)) return false; SocketDescriptor s; if (!s.CreateTCP()) return false; #ifdef HAVE_POSIX s.SetNonBlocking(); #endif if (s.Connect(address)) { Set(std::move(s)); return true; } #ifdef HAVE_POSIX if (errno == EINPROGRESS) { connecting = std::move(s); io_thread->LockAdd(connecting.ToFileDescriptor(), Poll::WRITE, *this); StateChanged(); return true; } #endif return false; }
bool GenmeshAsset::SetSocketTransform(const char* socketName, const SocketDescriptor& desc) { iSkeletonSocket* sock = skeleton->FindSocket (socketName); if (!sock) return false; sock->GetFactory()->SetName(desc.find("Name")->second.second.c_str()); iSkeletonBone* bone = skeleton->FindBone(desc.find("Bone")->second.second.c_str()); if (bone) sock->SetBone(bone); csVector3 offset; if (sscanf(desc.find("Offset")->second.second.c_str(), "%f, %f, %f", &offset[0], &offset[1], &offset[2]) != 3) return false; csReversibleTransform trans = sock->GetTransform(); trans.SetOrigin(offset); sock->SetTransform(trans); csVector3 rot; if (sscanf(desc.find("Rotation")->second.second.c_str(), "%f, %f, %f", &rot[0], &rot[1], &rot[2]) != 3) return false; csReversibleTransform transr = sock->GetTransform(); csMatrix3 m; m *= csXRotMatrix3 (rot.x); m *= csYRotMatrix3 (rot.y); m *= csZRotMatrix3 (rot.z); transr.SetO2T(m); sock->SetTransform(transr); return true; }
bool TCPPort::OnSocketEvent(SocketDescriptor _socket, unsigned mask) { assert(listener.IsDefined()); if (_socket == listener) { /* connection should never be defined here */ assert(SocketPort::GetState() == PortState::FAILED); SocketDescriptor s = listener.Accept(); if (!s.IsDefined()) return true; #ifndef HAVE_POSIX /* reset the flag so we can wait for it atomically after SocketPort::Set() */ closed_trigger.Reset(); #endif SocketPort::Set(std::move(s)); #ifdef HAVE_POSIX /* disable the listener socket while the connection socket is active */ return false; #else /* for until the connection SocketThread finishes the connection; meanwhile, incoming connections are ignored */ closed_trigger.Wait(); SocketPort::Close(); /* now continue listening for incoming connections */ return true; #endif } else { /* this event affects the connection socket */ if (!SocketPort::OnSocketEvent(_socket, mask)) { /* the connection was closed; continue listening on incoming connections */ #ifdef HAVE_POSIX /* close the connection, unregister the event, and reinstate the listener socket */ SocketPort::Close(); io_thread->Add(listener.ToFileDescriptor(), Poll::READ, *this); #else /* we must not call SocketPort::Close() here because it may deadlock, waiting forever for this thread to finish; instead, wake up the listener thread, and let it handle the event */ closed_trigger.Signal(); #endif return false; } else /* continue reading from the connection */ return true; } }
bool SocketPort::OpenUDPListener(unsigned port) { SocketDescriptor s; if (!s.CreateUDPListener(port)) return false; Set(std::move(s)); return true; }
bool AnimeshAsset::SetSocketTransform(const char* socketName, const SocketDescriptor& desc) { uint sockf = animeshsprite->FindSocket(socketName); if (sockf == (uint)~0) return false; CS::Mesh::iAnimatedMeshSocket* sock = animeshstate->GetSocket(sockf); if (!sock) return false; sock->GetFactory()->SetName(desc.find("Name")->second.second.c_str()); int bone; sscanf(desc.find("Bone")->second.second.c_str(), "%d", &bone); sock->GetFactory()->SetBone(bone); csVector3 offset; if (sscanf(desc.find("Offset")->second.second.c_str(), "%f, %f, %f", &offset[0], &offset[1], &offset[2]) != 3) return false; //csReversibleTransform trans = sock->GetFactory()->GetTransform(); csReversibleTransform trans = sock->GetTransform(); trans.SetOrigin(offset); //sock->GetFactory()->SetTransform(trans); sock->SetTransform(trans); csVector3 rot; if (sscanf(desc.find("Rotation")->second.second.c_str(), "%f, %f, %f", &rot[0], &rot[1], &rot[2]) != 3) return false; //csReversibleTransform transr = sock->GetFactory()->GetTransform(); csReversibleTransform transr = sock->GetTransform(); csMatrix3 m; m *= csXRotMatrix3 (rot.x); m *= csYRotMatrix3 (rot.y); m *= csZRotMatrix3 (rot.z); transr.SetO2T(m); //sock->GetFactory()->SetTransform(transr); sock->SetTransform(transr); return true; }
gcc_pure bool IsConnected() const { // TODO: what if the socket is broken? return fd.IsDefined(); }
void Close() { socket.Close(); }
bool Open(const char *host) { return socket.CreateConnectUDP(host, "5597"); }
bool IsDefined() const { return socket.IsDefined(); }
int main(int argc, char **argv) { // Determine on which TCP port to connect to the server const char *tcp_port; if (argc < 2) { fprintf(stderr, "This program opens a TCP connection to a server which is assumed "); fprintf(stderr, "to be at 127.0.0.1, and sends artificial FlyNet vario data.\n\n"); fprintf(stderr, "Usage: %s PORT\n", argv[0]); fprintf(stderr, "Defaulting to port 4353\n"); tcp_port = "4353"; } else { tcp_port = argv[1]; } // Convert IP address to binary form SocketAddress server_address; if (!server_address.Lookup("127.0.0.1", tcp_port, AF_INET)) { fprintf(stderr, "Failed to look up address\n"); exit(EXIT_FAILURE); } // Create socket for the outgoing connection SocketDescriptor sock; if (!sock.CreateTCP()) { perror("Socket"); exit(EXIT_FAILURE); } // Connect to the specified server if (!sock.Connect(server_address)) { perror("Connect"); exit(EXIT_FAILURE); } PeriodClock start_clock; start_clock.Update(); PeriodClock pressure_clock; PeriodClock battery_clock; fixed pressure = fixed(101300); unsigned battery_level = 11; while (true) { if (pressure_clock.CheckUpdate(48)) { NarrowString<16> sentence; int elapsed_ms = start_clock.Elapsed(); fixed elapsed = fixed(elapsed_ms) / 1000; fixed vario = sin(elapsed / 3) * cos(elapsed / 10) * cos(elapsed / 20 + fixed(2)) * fixed(3); fixed pressure_vario = -vario * fixed(12.5); fixed delta_pressure = pressure_vario * 48 / 1000; pressure += delta_pressure; sentence = "_PRS "; sentence.AppendFormat("%08X", uround(pressure)); sentence += "\n"; sock.Write(sentence.c_str(), sentence.length()); } if (battery_clock.CheckUpdate(11000)) { NarrowString<16> sentence; sentence = "_BAT "; if (battery_level <= 10) sentence.AppendFormat("%X", battery_level); else sentence += "*"; sentence += "\n"; sock.Write(sentence.c_str(), sentence.length()); if (battery_level == 0) battery_level = 11; else battery_level--; } } return EXIT_SUCCESS; }