?? voudpconfig_msg.cpp
字號:
//// Copyright 2004 Alan Post//// This file is part of voudp.//// voudp is free software; you can redistribute it and/or modify it under the// terms of the GNU General Public License as published by the Free Software// Foundation; either version 2 of the License, or (at your option) any later// version.//// voudp is distributed in the hope that it will be useful, but WITHOUT ANY// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR// A PARTICULAR PURPOSE. See the GNU General Public License for more details.//// You should have received a copy of the GNU General Public License along with// voudp; if not, write to the Free Software Foundation, Inc., 59 Temple Place,// Suite 330, Boston, MA 02111-1307 USA//#include "voudpconfig_msg.h"#include "sender.h"#include <unistd.h>#include <stdio.h>const char *sock_path = "/var/tmp/voudpconfig";static ssize_t read_fully( int infd, char *buf, size_t to_read ){ size_t total_read = 0; while ( 1 ) { ssize_t nread = read( infd, buf + total_read, to_read - total_read ); if ( nread == -1 ) { perror( "read" ); return -1; } total_read += (size_t) nread; if ( nread == 0 || total_read == to_read ) return total_read; } assert( "not reachable" == NULL ); return 0;}static bool write_fully( const int &outfd, const char *buf, const ssize_t &to_write ){ ssize_t total_written = 0; ssize_t nwritten; while ( 1 ) { nwritten = write( outfd, buf + total_written, to_write - total_written ); if ( nwritten == -1 ) { perror( "write" ); return false; } total_written += nwritten; if ( total_written == to_write ) return true; } assert( "not reachable" == NULL ); return false;}bool decode_req_code( int fd, voudpconfig_msg::req_code_t &code ){ ssize_t nread = read_fully( fd, (char*) &code, sizeof( code )); return nread == sizeof( code );}bool decode_resp_code( int fd, voudpconfig_msg::resp_code_t &code ){ ssize_t nread = read_fully( fd, (char*) &code, sizeof( code )); return nread == sizeof( code );}bool decode_bool( int fd, bool &b ){ ssize_t nread = read_fully( fd, (char*) &b, sizeof( b )); return nread == sizeof( b );}bool decode_word( int fd, size_t &word ){ ssize_t nread = read_fully( fd, (char*) &word, sizeof( word )); return (size_t) nread == sizeof( word );}bool decode_name( int fd, std::string &name ){ size_t len; if ( ! decode_word( fd, len )) return false; char *str = new char[ len + 1 ]; ssize_t nread = read_fully( fd, str, len ); if ( (size_t) nread != len ) { delete str; return false; } str[ len ] = '\0'; name = str; delete str; return true;}bool decode_info( int fd, recipient_info_t &info ){ return decode_name( fd, info.host ) && decode_word( fd, info.port ) && decode_word( fd, info.max_packet_len ) && decode_word( fd, info.max_buf_ms );}bool send_bool( int fd, const bool &b ){ return write_fully( fd, (const char*) &b, sizeof( b ));}bool send_req_code( int fd, const voudpconfig_msg::req_code_t &code ){ return write_fully( fd, (const char*) &code, sizeof( code ));}bool send_resp_code( int fd, const voudpconfig_msg::resp_code_t &code ){ return write_fully( fd, (const char*) &code, sizeof( code ));}bool send_word( int fd, const size_t &word ){ return write_fully( fd, (const char*) &word, sizeof( word ));}bool send_name( int fd, const std::string &name ){ return send_word( fd, name.length()) && write_fully( fd, name.data(), name.length());}bool send_info( int fd, const recipient_info_t &info ){ return send_name( fd, info.host ) && send_word( fd, info.port ) && send_word( fd, info.max_packet_len ) && send_word( fd, info.max_buf_ms );}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -