improve ChatVoice
This commit is contained in:
@@ -84,7 +84,8 @@ void ChatClientVoice::run() {
|
|||||||
if(cm->isRunning() && cm->isConnected()) {
|
if(cm->isRunning() && cm->isConnected()) {
|
||||||
|
|
||||||
if(ImGui::Button("Disconnect") && cm->isRunning()) {
|
if(ImGui::Button("Disconnect") && cm->isRunning()) {
|
||||||
cm->stopClient();
|
cm->sendReliable("srvcmd1", strlen("srvcmd1"));
|
||||||
|
//cm->stopClient();
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
@@ -105,11 +106,16 @@ void ChatClientVoice::run() {
|
|||||||
ImGui::SameLine();
|
ImGui::SameLine();
|
||||||
|
|
||||||
if(ImGui::Button("send")) {
|
if(ImGui::Button("send")) {
|
||||||
s.insert(s.begin(), 'o');
|
if(s == "/quit") {
|
||||||
s.insert(s.begin(), ' ');
|
cm->sendReliable("srvcmd1", strlen("srvcmd1"));
|
||||||
s.insert(s.begin(), '0');
|
cm->stopClient();
|
||||||
s.insert(s.begin(), 'a');
|
} else if(s == "/shutdown") {
|
||||||
|
cm->sendReliable("srvcmd2", strlen("srvcmd2"));
|
||||||
|
//cm->stopClient();
|
||||||
|
} else {
|
||||||
|
s.insert(0, "client");
|
||||||
cm->sendReliable(s.c_str(), (uint32) s.length());
|
cm->sendReliable(s.c_str(), (uint32) s.length());
|
||||||
|
}
|
||||||
s.clear();
|
s.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -130,9 +136,6 @@ void ChatClientVoice::run() {
|
|||||||
SDL_ClearAudioStream(mic);
|
SDL_ClearAudioStream(mic);
|
||||||
SDL_ResumeAudioStreamDevice(mic);
|
SDL_ResumeAudioStreamDevice(mic);
|
||||||
SDL_SetAudioStreamGain(mic, micVol);
|
SDL_SetAudioStreamGain(mic, micVol);
|
||||||
} else if(!cm->isConnected()) {
|
|
||||||
SDL_PauseAudioStreamDevice(mic);
|
|
||||||
SDL_ClearAudioStream(mic);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!SDL_AudioStreamDevicePaused(speaker)) {
|
if(!SDL_AudioStreamDevicePaused(speaker)) {
|
||||||
@@ -141,10 +144,6 @@ void ChatClientVoice::run() {
|
|||||||
SDL_ClearAudioStream(speaker);
|
SDL_ClearAudioStream(speaker);
|
||||||
SDL_ResumeAudioStreamDevice(speaker);
|
SDL_ResumeAudioStreamDevice(speaker);
|
||||||
SDL_SetAudioStreamGain(speaker, speakerVol);
|
SDL_SetAudioStreamGain(speaker, speakerVol);
|
||||||
} else if(!cm->isConnected()) {
|
|
||||||
SDL_PauseAudioStreamDevice(speaker);
|
|
||||||
SDL_ClearAudioStream(speaker);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int avail = 0;
|
static int avail = 0;
|
||||||
@@ -152,9 +151,10 @@ void ChatClientVoice::run() {
|
|||||||
if(micTest) {
|
if(micTest) {
|
||||||
|
|
||||||
avail = SDL_min(len, SDL_GetAudioStreamAvailable(mic));
|
avail = SDL_min(len, SDL_GetAudioStreamAvailable(mic));
|
||||||
|
if(avail > 10) {
|
||||||
avail = SDL_GetAudioStreamData(mic, buf, avail);
|
avail = SDL_GetAudioStreamData(mic, buf, avail);
|
||||||
|
|
||||||
SDL_PutAudioStreamData(speaker, buf, avail);
|
SDL_PutAudioStreamData(speaker, buf, avail);
|
||||||
|
}
|
||||||
|
|
||||||
} else if(cm->isConnected()){
|
} else if(cm->isConnected()){
|
||||||
// if not testing, send to server.
|
// if not testing, send to server.
|
||||||
@@ -176,6 +176,7 @@ void ChatClientVoice::run() {
|
|||||||
bool ChatClientVoice::onEvent(const Archimedes::Event& event) {
|
bool ChatClientVoice::onEvent(const Archimedes::Event& event) {
|
||||||
|
|
||||||
unsigned int type = app->getEventType(event);
|
unsigned int type = app->getEventType(event);
|
||||||
|
static ClientModule* cm;
|
||||||
|
|
||||||
/*if(type == app->getEventType("DataSentEvent")) {
|
/*if(type == app->getEventType("DataSentEvent")) {
|
||||||
//we did this?
|
//we did this?
|
||||||
@@ -186,19 +187,49 @@ bool ChatClientVoice::onEvent(const Archimedes::Event& event) {
|
|||||||
|
|
||||||
Archimedes::DataRecievedEvent& e = (Archimedes::DataRecievedEvent&) event;
|
Archimedes::DataRecievedEvent& e = (Archimedes::DataRecievedEvent&) event;
|
||||||
|
|
||||||
|
{ cm = (ClientModule*) moduleInstances[ClientModule()]; }
|
||||||
|
|
||||||
static std::string s;
|
static std::string s;
|
||||||
|
|
||||||
if(((char*)e.msg->m_pData)[0] == 'a' && ((char*)e.msg->m_pData)[1] == '0' && ((char*)e.msg->m_pData)[2] == ' ' && ((char*)e.msg->m_pData)[3] == 'o') {
|
if(std::string((const char*)e.msg->m_pData, 6) == "client") {
|
||||||
s = std::string((const char*)e.msg->m_pData + 4, e.msg->m_cbSize - 4);
|
s = std::string((const char*)e.msg->m_pData + 6, e.msg->m_cbSize - 6);
|
||||||
|
|
||||||
messages += "\n\n" + s;
|
messages += "\n\n" + s;
|
||||||
} else {
|
} else if(std::string((const char*)e.msg->m_pData, 6) == "server") {
|
||||||
|
s = std::string((const char*)e.msg->m_pData + 6, e.msg->m_cbSize - 6);
|
||||||
|
|
||||||
|
int serverCode = stoi(s);
|
||||||
|
|
||||||
|
switch(serverCode) {
|
||||||
|
case 0:
|
||||||
|
//start voice
|
||||||
|
SDL_ClearAudioStream(mic);
|
||||||
|
SDL_ClearAudioStream(speaker);
|
||||||
|
SDL_ResumeAudioStreamDevice(mic);
|
||||||
|
SDL_ResumeAudioStreamDevice(speaker);
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
//stop voice and clear
|
||||||
|
SDL_PauseAudioStreamDevice(mic);
|
||||||
|
SDL_PauseAudioStreamDevice(speaker);
|
||||||
|
SDL_ClearAudioStream(mic);
|
||||||
|
SDL_ClearAudioStream(speaker);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
//disconnect
|
||||||
|
cm->stopClient();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
} else /*if(std::string((const char*)e.msg->m_pData, 6) == "audio:")*/ {
|
||||||
s = "audio data";
|
s = "audio data";
|
||||||
|
|
||||||
SDL_PutAudioStreamData(speaker, e.msg->m_pData, e.msg->m_cbSize);
|
SDL_PutAudioStreamData(speaker, e.msg->m_pData, e.msg->m_cbSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cerr << "Client Recieved: " << s << std::endl;
|
//std::cerr << "Client Recieved: " << s << std::endl;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} else if(type == app->getEventType("ConnectionStatusChangedEvent")) {
|
} else if(type == app->getEventType("ConnectionStatusChangedEvent")) {
|
||||||
@@ -230,11 +261,6 @@ bool ChatClientVoice::onEvent(const Archimedes::Event& event) {
|
|||||||
|
|
||||||
case k_ESteamNetworkingConnectionState_Connected:
|
case k_ESteamNetworkingConnectionState_Connected:
|
||||||
//OnConnect
|
//OnConnect
|
||||||
SDL_ClearAudioStream(mic);
|
|
||||||
SDL_ClearAudioStream(speaker);
|
|
||||||
SDL_ResumeAudioStreamDevice(mic);
|
|
||||||
SDL_ResumeAudioStreamDevice(speaker);
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ bool ChatServerVoice::onEvent(const Archimedes::Event& event) {
|
|||||||
|
|
||||||
unsigned int type = app->getEventType(event);
|
unsigned int type = app->getEventType(event);
|
||||||
|
|
||||||
|
static ServerModule* sm;
|
||||||
/*if(type == app->getEventType("DataSentEvent")) {
|
/*if(type == app->getEventType("DataSentEvent")) {
|
||||||
//we did this?
|
//we did this?
|
||||||
return true;
|
return true;
|
||||||
@@ -20,30 +20,48 @@ bool ChatServerVoice::onEvent(const Archimedes::Event& event) {
|
|||||||
|
|
||||||
if(type == app->getEventType(Archimedes::DataRecievedEvent())) {
|
if(type == app->getEventType(Archimedes::DataRecievedEvent())) {
|
||||||
|
|
||||||
static ServerModule* sm; { sm = (ServerModule*) moduleInstances[ServerModule()]; }
|
{ sm = (ServerModule*) moduleInstances[ServerModule()]; }
|
||||||
|
|
||||||
|
|
||||||
Archimedes::DataRecievedEvent& e = (Archimedes::DataRecievedEvent&) event;
|
Archimedes::DataRecievedEvent& e = (Archimedes::DataRecievedEvent&) event;
|
||||||
static std::string s;
|
static std::string s;
|
||||||
|
|
||||||
if(((char*)e.msg->m_pData)[0] == 'a' && ((char*)e.msg->m_pData)[1] == '0' && ((char*)e.msg->m_pData)[2] == ' ' && ((char*)e.msg->m_pData)[3] == 'o') {
|
if(std::string((const char*)e.msg->m_pData, 6) == "client") {
|
||||||
s = std::string((const char*)e.msg->m_pData + 4, e.msg->m_cbSize - 4);
|
s = std::string((const char*)e.msg->m_pData + 6, e.msg->m_cbSize - 6);
|
||||||
|
|
||||||
if(s == "/quit") {
|
|
||||||
sm->disconnectClient(e.msg->m_conn);
|
|
||||||
} else if(s == "/shutdown") {
|
|
||||||
sm->stopServer();
|
|
||||||
app->end();
|
|
||||||
}
|
|
||||||
|
|
||||||
for(auto& it : sm->getClients()) {
|
for(auto& it : sm->getClients()) {
|
||||||
if(it.first != e.msg->m_conn)
|
if(it.first != e.msg->m_conn)
|
||||||
sm->sendReliable(it.first, e.msg->m_pData, e.msg->m_cbSize);
|
sm->sendReliable(it.first, e.msg->m_pData, e.msg->m_cbSize);
|
||||||
else
|
else
|
||||||
sm->sendReliable(e.msg->m_conn, "a0 o\nMessage sent\n", strlen("a0 o\nMessage sent\n"));
|
sm->sendReliable(e.msg->m_conn, "client\nMessage sent\n", strlen("client\nMessage sent\n"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
} else {
|
} else if(std::string((const char*)e.msg->m_pData, 6) == "srvcmd") {
|
||||||
|
s = std::string((const char*)e.msg->m_pData + 6, e.msg->m_cbSize - 6);
|
||||||
|
|
||||||
|
int cmdCode = stoi(s);
|
||||||
|
|
||||||
|
switch(cmdCode) {
|
||||||
|
case 0:
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
//disconnect
|
||||||
|
sm->disconnectClient(e.msg->m_conn);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
for(auto& it : sm->getClients()) {
|
||||||
|
sm->sendReliable(it.first, "server2", strlen("server2"));
|
||||||
|
}
|
||||||
|
sm->stopServer();
|
||||||
|
app->end();
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
} else /*if(std::string((const char*)e.msg->m_pData, 6) == "audio:")*/ {
|
||||||
s = "audio data";
|
s = "audio data";
|
||||||
|
|
||||||
for(auto& it : sm->getClients()) {
|
for(auto& it : sm->getClients()) {
|
||||||
@@ -52,17 +70,62 @@ bool ChatServerVoice::onEvent(const Archimedes::Event& event) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cerr << "Server Recieved: " << s << std::endl;
|
//std::cerr << "Server Recieved: " << s << std::endl;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
} /*else if(type == app->getEventType("ConnectionStatusChangedEvent")) {
|
} else if(type == app->getEventType("ConnectionStatusChangedEvent")) {
|
||||||
|
|
||||||
//Archimedes::ConnectionStatusChangedEvent& e = (Archimedes::ConnectionStatusChangedEvent&) event;
|
Archimedes::ConnectionStatusChangedEvent& e = (Archimedes::ConnectionStatusChangedEvent&) event;
|
||||||
|
|
||||||
|
{ sm = (ServerModule*) moduleInstances[ServerModule()]; }
|
||||||
|
|
||||||
|
unsigned int numClients;
|
||||||
|
(void)sm->getClients(&numClients);
|
||||||
|
|
||||||
|
switch(e.info->m_info.m_eState) {
|
||||||
|
|
||||||
|
case k_ESteamNetworkingConnectionState_None:
|
||||||
|
// NOTE: We will get callbacks here when we destroy connections. You can ignore these.
|
||||||
|
break;
|
||||||
|
|
||||||
|
case k_ESteamNetworkingConnectionState_ClosedByPeer:
|
||||||
|
case k_ESteamNetworkingConnectionState_ProblemDetectedLocally:
|
||||||
|
{
|
||||||
|
if(numClients < 2) {
|
||||||
|
for(auto& it : sm->getClients()) {
|
||||||
|
sm->sendReliable(it.first, "server1", strlen("server1"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case k_ESteamNetworkingConnectionState_Connecting:
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case k_ESteamNetworkingConnectionState_Connected:
|
||||||
|
//OnConnect
|
||||||
|
|
||||||
|
if(numClients == 2) {
|
||||||
|
for(auto& it : sm->getClients()) {
|
||||||
|
sm->sendReliable(it.first, "server0", strlen("server0"));
|
||||||
|
}
|
||||||
|
} else if(numClients > 2) {
|
||||||
|
|
||||||
|
sm->sendReliable(e.info->m_hConn, "server0", strlen("server0"));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
// Silences -Wswitch
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
}*/
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,7 +52,12 @@ class ServerModule : public Archimedes::Module {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::map<HSteamNetConnection, unsigned int> getClients() const { return clients; }
|
std::map<HSteamNetConnection, unsigned int> getClients(unsigned int* num = nullptr) const {
|
||||||
|
if(num) {
|
||||||
|
*num = numClients;
|
||||||
|
}
|
||||||
|
return clients;
|
||||||
|
}
|
||||||
|
|
||||||
void pollIncomingData();
|
void pollIncomingData();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user