本文整理汇总了C++中readn函数的典型用法代码示例。如果您正苦于以下问题:C++ readn函数的具体用法?C++ readn怎么用?C++ readn使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了readn函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: while
int Session::on_read_event()
{
int ret;
while (true)
{
if (!m_has_head)
{
ret = read_head();
if (ret < 0)
{
return -1;
}
if (ret == SESSION_RET_NO_DATA)
{
return 0;
}
if (m_msg_type == MSG_HEART_BEAT)
{
if (g_is_master)
{
update_active_time();
}
else
{
struct commu_msg msg;
msg.ip = get_cli_addr();
msg.msg = MSG_HEART_BEAT;
notify_master(msg);
}
continue;
}
m_has_head = true;
m_buff = (u_char *)glb_mempool()->get(m_msg_len);
}
int n_read = readn(m_sockfd, &m_buff[m_n_read], m_msg_len - m_n_read);
if (n_read < 0)
{
logerr("socket broken\n");
m_msg_len = 0;
m_has_head = false;
return -1;
}
m_n_read += n_read;
if (m_n_read == m_msg_len)
{
write_msg(m_buff, m_msg_len);
reset_buffer();
}
}
return 0;
}
示例2: vncrdstringx
/*
* on the server side of the negotiation protocol, we read
* the client response and then run the negotiated function.
* in some cases (e.g., TLS) the negotiated function needs to
* use v->datafd directly and be sure that no data has been
* buffered away in the Bio. since we know the client is waiting
* for our response, it won't have sent any until we respond.
* thus we read the response with vncrdstringx, which goes
* behind bio's back.
*/
char*
vncrdstringx(Vnc *v)
{
char tmp[4];
char *s;
ulong len;
assert(Bbuffered(&v->in) == 0);
if(readn(v->datafd, tmp, 4) != 4){
fprint(2, "cannot rdstringx: %r");
vnchungup(v);
}
len = LONG(tmp);
s = malloc(len+1);
assert(s != nil);
if(readn(v->datafd, s, len) != len){
fprint(2, "cannot rdstringx len %lud: %r", len);
vnchungup(v);
}
s[len] = '\0';
return s;
}
示例3: priv_sock_get_int
int priv_sock_get_int(int fd)
{
int the_int;
int ret;
ret = readn(fd, &the_int, sizeof(the_int));
if (ret != sizeof(the_int))
{
fprintf(stderr, "priv_sock_get_int error\n");
exit(EXIT_FAILURE);
}
return the_int;
}
示例4: sckClient_rcv
int sckClient_rcv(void *handle, unsigned char *out, int *outlen)
{
int ret = 0;
SckHandle *tmp = handle;
ret = read_timeout(tmp->sockfd, tmp->recvtime);
if(ret < 0){
if(ret == -1 && errno == ETIMEDOUT){
ret = SCK_ERRTIMEOUT;
printf("sckClient_rcv() err: %d\n", ret);
return ret;
}
return ret;
}
int netdatalen = 0;
ret = readn(tmp->sockfd, &netdatalen, 4);
if(ret == -1 ){
printf("func sckClient_rcv() first err: %d\n", ret);
return ret;
}else if(ret < 4){
ret = SCK_ERRPEERCLOSED;
printf("func sckClient_rcv() first err: %d\n", ret);
return ret;
}
int n;
n = ntohl(netdatalen);
ret = readn(tmp->sockfd, out, n);
if(ret == -1 ){
printf("func sckClient_rcv() second err: %d\n", ret);
return ret;
}else if(ret < n){
ret = SCK_ERRPEERCLOSED;
printf("func sckClient_rcv() second err: %d\n", ret);
return ret;
}
*outlen = n;
return 0;
}
示例5: priv_sock_get_result
char priv_sock_get_result(int fd)
{
char res;
int ret;
ret = readn(fd, &res, sizeof(res));
if (ret != sizeof(res))
{
fprintf(stderr, "priv_sock_get_result error\n");
exit(EXIT_FAILURE);
}
return res;
}
示例6: xread
static void
xread(Ioclust *c)
{
int n;
Xdata *dev;
dev = c->dev;
seek(dev->dev, (vlong)c->addr * Sectorsize, 0);
n = readn(dev->dev, c->iobuf, BUFPERCLUST*Sectorsize);
if(n < Sectorsize)
error("I/O read error");
c->nbuf = n/Sectorsize;
}
示例7: usc_mrcp_asr_destroy
int usc_mrcp_asr_destroy(HANDLE_MRCPINTF asr)
{
char buf[48];
off_t offset = 0;
PUSH_FUN(buf, offset, sizeof(buf));
PUSH_HEAD(buf, offset, sizeof(buf));
writen(asr, buf, offset + 4);
/*read return value*/
readn(asr, buf, 8);
close(asr);
return 0;
}
示例8: doread
char *
doread(Ram *r, int64_t off, int32_t cnt)
{
int n;
seek(tapefile, Tblock*r->addr + off, 0);
if (cnt > sizeof dblock.tbuf)
error("read too big");
n = readn(tapefile, dblock.tbuf, cnt);
if (n != cnt)
memset(dblock.tbuf + n, 0, cnt - n);
return dblock.tbuf;
}
示例9: readline
ssize_t readline(int sockfd, void *buf, size_t maxline)
{
int ret;
int nread;
char *bufp = (char *)buf;
int nleft = maxline;
while(nleft > 0)
{
ret = recv_peek(sockfd, bufp, nleft);
if(ret < 0)
return ret;
else if(ret == 0)
return 0;
nread = ret;
int i;
for(i=0; i<nread; i++){
if(bufp[i] == '\n') {
ret = readn(sockfd, bufp, i + 1);
if( ret != i+1)
exit(-1);
return ret;
}
}
if(nread > nleft)
exit(-1);
nleft -= nread;
ret = readn(sockfd, bufp, nread);
if(ret != nread)
exit(-1);
bufp += nread;
}
return -1;
}
示例10: readline
ssize_t readline(int fd, void *buf, size_t maxlen)
{
size_t nleft = maxlen - 1;
char *bufp = (char *)buf;
ssize_t total = 0;
ssize_t nread;
while (nleft > 0) {
//recv+peek为预读取
nread = recv_peek(fd, bufp, nleft);
if (nread <= 0)
return nread;
//检查此段内有无\n
int i;
for (i = 0; i < nread; ++i) {
if (bufp[i] == '\n') {
//找到\n后读取退出
size_t nsize = i + 1;
if (readn(fd, bufp, nsize) != nsize)
return -1;
bufp += nsize;
*bufp = '\0';
total += nsize;
return total;
}
}
//到此步说明没有找到\n,继续
if (readn(fd, bufp, nread) != nread)
return -1;
bufp += nread;
total += nread;
nleft -= nread;
}
//读取到最大长度,返回
*bufp = '\0';
return maxlen - 1;
}
示例11: while
ssize_t SocketIO::readline(char * buf, size_t maxlen)
{
size_t nleft = maxlen - 1;
char * pbuf = buf;
ssize_t nread;
size_t total = 0;
while(nleft > 0)
{
nread = recv_peek(pbuf, nleft);
if(nread <= 0)
return nread;
int i;
for(i = 0; i != nread; ++i)
{
//当找到'\n'
if(pbuf[i] == '\n')
{
size_t nsize = i + 1;
if(static_cast<ssize_t>(nsize)
!= readn(pbuf, nsize))
return -1;
pbuf += nsize;
total += nsize;
*pbuf = 0;
return total;
}
}
//当没有找到'\n'
if(readn(pbuf, nread) != nread)
return -1;
total += nread;
pbuf += nread;
nleft -= nread;
}
*pbuf = 0;
return maxlen - 1;
}
示例12: readline
char *
readline (int fd)
{
char * line;
char * ptr;
int i = 0;
int nread = 0;
int len = 0;
int buflen = 0;
if ((line = (char *) malloc (LINE_BUF_SIZE * sizeof (char))) == NULL)
return NULL;
ptr = line;
/* we accept \r\n and \n as endline. */
while (len < (MAX_LINE_BUF_SIZE-1) && (nread = readn (fd, ptr, 1)) == 1
&& *ptr != '\n') {
if (*ptr != '\r') {
len += nread;
ptr += nread;
buflen += nread;
if (buflen == LINE_BUF_SIZE) {
i ++;
if ((line = realloc (line, (i+1) * LINE_BUF_SIZE * sizeof (char))) == NULL) {
free (line);
return NULL;
}
ptr = line + i * LINE_BUF_SIZE;
buflen = 0;
}
}
}
/* EOF */
if (len == 0 && nread == 0) {
free (line);
return NULL;
}
if (nread == -1 || len >= MAX_LINE_BUF_SIZE) {
free (line);
return NULL;
}
line [len] = 0;
return line;
}
示例13: main
int main (int argc, char *argv[]) {
double C = 60 * log(2);
double PI = 2 * acos(0);
fread_unlocked(s, sizeof(char), 10 * 1024 * 1024, stdin);
readn();
int i, t = n;
for (I = 0; I < t; I += 1) {
readn_signed();
a = n;
readn_signed();
b = n;
theta = atan2 (b, a) / PI;
r = log( (double)( a * a + b * b ) );
written = 1;
for (k = 1; k <= 4; k += 1) {
if ( k * r <= C && fabs ( k * theta - (int)(k * theta + 0.5) ) < 0.00001 ) {
writen(k);
written = 101;
break;
}
}
if ( written != 101 ) {
out[++wcount] = 'T';
out[++wcount] = 'O';
out[++wcount] = 'O';
out[++wcount] = ' ';
out[++wcount] = 'C';
out[++wcount] = 'O';
out[++wcount] = 'M';
out[++wcount] = 'P';
out[++wcount] = 'L';
out[++wcount] = 'I';
out[++wcount] = 'C';
out[++wcount] = 'A';
out[++wcount] = 'T';
out[++wcount] = 'E';
out[++wcount] = 'D';
}
out[++wcount] = '\n';
}
fwrite_unlocked(out, 1, wcount + 1, stdout);
return 0;
}
示例14: elf
/* elf ----------------------------------------------------------------------*/
void
elf(int fd_s, int fd_d, Elf32_Word p_type, Elf32_Word p_flags,
Elf32_Addr* addr, int* size, int flag)
{
int i;
Elf32_Ehdr e_eh;
static Elf32_Phdr e_ph[32];
lseek(fd_s, 0, SEEK_SET); /* rewind */
readn(fd_s, &e_eh, sizeof(Elf32_Ehdr));
lseek(fd_s, e_eh.e_phoff, SEEK_SET);
readn(fd_s, (char*)e_ph, e_eh.e_phentsize * e_eh.e_phnum);
printf("#-----------------------------------------------------------------#\n");
printf("# [t]ype, [f]lag, [v]addr, [m]emsz, [s]filesz, [o]ffset #\n");
printf("# e_eh.e_phnum = %d\n", e_eh.e_phnum);
for (i = 0 ; i < e_eh.e_phnum ; i ++) {
printf("# ph[%d]: t = 0x%x, f = 0x%x, v = 0x%x, m = 0x%x, s = 0x%x o = %d\n",
i, e_ph[i].p_type, e_ph[i].p_flags, e_ph[i].p_vaddr, e_ph[i].p_memsz,
e_ph[i].p_filesz, e_ph[i].p_offset);
if (e_ph[i].p_type == p_type &&
e_ph[i].p_flags == p_flags) {
/* do everything ------------------------------------*/
if (flag) {
write_space(fd_d,
(int)(e_ph[i].p_vaddr - *addr) - *size);
}
lseek(fd_s, e_ph[i].p_offset, SEEK_SET);
readn(fd_s, buf, e_ph[i].p_filesz);
writen(fd_d, buf, e_ph[i].p_filesz);
*addr = e_ph[i].p_vaddr;
*size = e_ph[i].p_memsz;
printf("# virtual address = 0x%x, size = 0x%x\n", *addr, *size);
break;
}
}
}
示例15: sckServer_rcv
int sckServer_rcv(int connfd, unsigned char *out, int *outlen, int timeout){
int ret = 0;
printf("sckServer_rcv() connfd=%d\n", connfd);
ret = read_timeout(connfd, timeout);
if(ret < 0){
if(ret == -1 && errno == ETIMEDOUT){
ret = SCK_ERRTIMEOUT;
printf("sckServer_rcv() read timeout err: %d\n", ret);
return ret;
}
return ret;
}
int netdatalen = 0;
ret = readn(connfd, &netdatalen, 4);
if(ret == -1 ){
printf("func sckServer_rcv() first err: %d\n", ret);
return ret;
}else if(ret < 4){
ret = SCK_ERRPEERCLOSED;
printf("func sckServer_rcv() first err: %d\n", ret);
return ret;
}
int n;
n = ntohl(netdatalen);
ret = readn(connfd, out, n);
if(ret == -1 ){
printf("func sckServer_rcv() second err: %d\n", ret);
return ret;
}else if(ret < n){
ret = SCK_ERRPEERCLOSED;
printf("func sckServer_rcv() second err: %d\n", ret);
return ret;
}
*outlen = n;
return 0;
}