int sendMessageToClient(AllGameInfo agi, int clientId){ /* 向客户端发送消息 */ ServerMessage sm; int fd; int res; char *fifoPath; if(clientId == 1) fifoPath = FIFO_CLIENT_1; else fifoPath = FIFO_CLIENT_2; /* 生成消息 */ gameInfoToServerMessage(agi, &sm, clientId); /* 打开端口 */ if((fd = openFifo(fifoPath, O_WRONLY)) == -1){ printf("open %s error\n", fifoPath); exit(0); }else{ printf("open %s over\n", fifoPath); } /* 发送消息 */ res = write(fd, &sm, sizeof(ServerMessage)); if(res == -1){ printf("write %s error\n",FIFO_CLIENT_1); exit(-1); }else{ printf("write bytes: %d\n", res); } /* 关闭端口 */ close(fd); return 0; }
void QGstreamerPlayerControl::setMedia(const QMediaContent &content, QIODevice *stream) { QMediaPlayer::State oldState = m_state; m_state = QMediaPlayer::StoppedState; m_session->stop(); if (m_bufferProgress != -1) { m_bufferProgress = -1; emit bufferStatusChanged(0); } if (m_stream) { closeFifo(); disconnect(m_stream, SIGNAL(readyRead()), this, SLOT(writeFifo())); m_stream = 0; } m_currentResource = content; m_stream = stream; m_seekToStartPending = false; QNetworkRequest request; if (m_stream) { if (m_stream->isReadable() && openFifo()) { request = QNetworkRequest(QUrl(QString(QLatin1String("fd://%1")).arg(m_fifoFd[0]))); } } else if (!content.isNull()) { request = content.canonicalRequest(); } m_session->load(request); if (m_fifoFd[1] >= 0) { m_fifoCanWrite = true; writeFifo(); } if (!request.url().isEmpty()) { if (m_mediaStatus != QMediaPlayer::LoadingMedia) emit mediaStatusChanged(m_mediaStatus = QMediaPlayer::LoadingMedia); m_session->pause(); } else { if (m_mediaStatus != QMediaPlayer::NoMedia) emit mediaStatusChanged(m_mediaStatus = QMediaPlayer::NoMedia); setBufferProgress(0); } emit mediaChanged(m_currentResource); if (m_state != oldState) emit stateChanged(m_state); }
SharedPtrNotNULL<std::fstream> createFifo(const boost::filesystem::path &p) { // fifo cannot exist yet if( exists(p) ) throw ExceptionFilesystemIO(SYSTEM_SAVE_LOCATION, p, "exists", "element already exists"); // directory path must be sane const path parent=Base::Filesystem::parentPath(p); if( parent.empty()==false ) { if( isDirectorySane(parent)==false ) throw ExceptionFilesystemIO(SYSTEM_SAVE_LOCATION, p, "isDirectorySane", "path looks suspicious - refusing to create fifo"); } // create new fifo if( mkfifo( p.string().c_str(), 0644 )!=0 ) throw ExceptionFilesystemIO(SYSTEM_SAVE_LOCATION, p, "mkfifo", "unable to create fifo queue"); // opens fifo return openFifo(p); }
int main(int argc, char ** argv){ state = WAITING; /* Mandatory arguments */ if( !argv[1] || !argv[2] || !argv[3] ) { fprintf(stderr,"server [server->client fifo] [client->server fifo] [password file]\n"); exit(1); } /* Open the server->client fifo */ fprintf(stderr,"Opening server->client fifo...\n"); outputChannel = openFifo(argv[1]); /* Open the client->server fifo */ fprintf(stderr,"Opening client->server fifo...\n"); inputChannel = openFifo(argv[2]); do{ switch(state){ case WAITING : state = handshake(); if(state == ERROR) //handshake error fprintf(stderr , "\n **** handshake falied **** \n\n"); else fprintf(stderr , "\n **** handshake succeded **** \n\n"); break; case HANDSHAKE : state = authentication(); break; case AUTHENTICATED : state = keyExchange(); break; case COMMUNICATION : state = communication(); break; case CLOSING : state = closeConnection(); outputChannel = openFifo(argv[1]); inputChannel = openFifo(argv[2]); if(state == ERROR) //handshake error fprintf(stderr , "\n **** closing falied **** \n\n"); else fprintf(stderr , "\n **** closing succeded **** \n\n"); break; case ERROR : fprintf(stderr , "\nError during the communication, force shutodown of the client\n\n"); closeConnection(); state = WAITING; outputChannel = openFifo(argv[1]); inputChannel = openFifo(argv[2]); fprintf(stderr , "\n **** closing succeded **** \n\n"); break; //close connection default : break; } }while(1); exit(0); }