/// <summary> /// This method manage the behaviour of the server when it receive a message. /// </summary> private void Handler(State so, int nBytes) { /* * Message id starting by 1xx are post message (message that ask for changing something on the server) * Message id starting by 2xx are answer message * Message id starting by 3xx are get message (message that ask to know something from the server) * * * Message id = 101 incoming request for connection * Message id =102 incoming dictionaries with servos target position and motor target speed * Message id = 201 answer to a connection request * Message id = 202 answer to the request for the motors position and speed. * Sending dictionaries with position for servos and speed for CCMotors * Message id = 301 asking for motors position and speed */ var rcvString = Encoding.ASCII.GetString(so.Buffer, 0, nBytes); if (!Message.IsMessage(rcvString)) { /* * This condition means the incoming message is not a message object */ switch (rcvString) { case "ping": SendTo(EndPointToIpEndPoint(_epFrom), "pong"); break; case "pong": if (_sendPing) { _tPong = DateTime.Now.Millisecond; _rcvPong = true; } break; default: if (_verbose) { Debug.Log(string.Format("RECV: {0}: {1}, {2}", _epFrom.ToString(), nBytes, rcvString)); } break; } } else if (!new Message(rcvString).CheckMessage()) { /* * This condition means the message is a corrupted Message object */ } else { /* * This condition means the message is a a Message object and it is not corrupted */ /* Write here the code to execute when a new Message is received */ var rcvMessage = new Message(rcvString); if (_remoteUser != null && EndPointToIpEndPoint(_epFrom).Equals(_remoteUser)) { /* * This condition means the source of the incoming message is the identified remote user */ switch (rcvMessage.id) { case 101: // Ask for Connection /* * The incoming message comes from an user already connected. */ SendTo(_remoteUser, new Message(201, "{" + '"' + "connection_status" + '"' + ": 1}").ToJson()); break; case 102: // Translate Json to dictionaries and set Controller values /* * The json string is translated to two dictionaries with target values * Then the Set Method of class Controller is called */ var target = new RobotCommandMessage(rcvMessage.message); Debug.Log(target.cc_motors_target_speed); Debug.Log(target.servomotors_target_position); _robotController.SetServoPos(target.servomotors_target_position); _robotController.SetDCMotorsSpeed(target.cc_motors_target_speed); break; case 301: // Ask for Connection /* * The incoming message must be an empty message with id 301. * Example request : * {"id": 301, "parity": 0, "len": 0, "message": ""} * If the password is correct then the default remote user is the origin of the request. */ SendTo(_remoteUser, new Message(202, new RobotDataMessage(_robotController.GetServosData(), _robotController.GetDCMotorData()).ToJson()).ToJson()); break; default: if (_verbose) { Debug.Log("Unknown id"); Debug.Log(rcvString); Debug.Log(rcvMessage.id); } break; } } else { /* * This condition means the source of the message is not the remote user */ switch (rcvMessage.id) { case 101: // Ask for Connection /* * The incoming message must have two keys "password" and "verbose". * "password" is the hashed password with SHA1 algorithm. * "verbose" tell the server if he must send a reply. Set the value to 1 for a reply else 0. * Example request : * {"id": 101, "parity": 1, "len": 71, "message": "{\"password\": \"a94a8fe5ccb19ba61c4c0873d391e987982fbbd3\" , \"verbose\": 1}"} * If the password is correct then the default remote user is the origin of the request. */ try { var temp = new ConnectionMessage(rcvMessage.message); if (temp.password.Equals(_hashPass)) { _remoteUser = EndPointToIpEndPoint(_epFrom); if (temp.verbose == 1) { SendTo(_remoteUser, new Message(201, "{" + '"' + "connection_status" + '"' + ": 1}").ToJson()); } } else { if (temp.verbose == 1) { SendTo(EndPointToIpEndPoint(_epFrom), new Message(201, "{" + '"' + "connection_status" + '"' + ": 0}").ToJson()); } } } catch { if (_verbose) { Debug.Log("Message format not correct for connection"); } } break; default: if (_verbose) { Debug.Log("Unknown id"); Debug.Log(rcvString); } break; } } } }