/// <summary> /// Receive data /// </summary> /// <param name="ar"></param> private void ReceiveCallback(IAsyncResult ar) { object result =null; Socket handler = ar.AsyncState as Socket; if (handler.Connected) { try { int read = handler.EndReceive(ar); if (read > 0) { ClientData data = getClient(handler); byte[] sizedBuffer = new byte[read]; Array.Copy(data.readbuf, sizedBuffer, read); NetworkPacket np = new NetworkPacket(sizedBuffer); this.OnDataReceived(new ServerDataReceivedEventArgs(np, data.id)); String Data = Encoding.UTF8.GetString(data.readbuf, 0, read); Data += "\n\r"; ArrayList resultArray = (ArrayList) JSON.Deserialize(Data); object objectForInvoke = null; foreach (object objectSelected in _listObject) { if (resultArray[0].ToString() == objectSelected.GetType().ToString()) { objectForInvoke = objectSelected; break; } } if (objectForInvoke != null) { MethodInfo methodInfo = objectForInvoke.GetType().GetMethod(resultArray[1].ToString()); if (methodInfo != null) { int indice = 2; ParameterInfo[] parameters = methodInfo.GetParameters(); int NumberOfParamaters = parameters.Length; List<object> parametersArray = new List<object>(); foreach (ParameterInfo TypeOfParameters in parameters) { if (TypeOfParameters.ParameterType == typeof(int)) { parametersArray.Add(Convert.ToInt32(resultArray[indice])); } else if (TypeOfParameters.ParameterType == typeof(uint)) { parametersArray.Add(Convert.ToUInt32(resultArray[indice])); } else if (TypeOfParameters.ParameterType == typeof(bool)) { parametersArray.Add(Convert.ToBoolean(resultArray[indice])); } else if (TypeOfParameters.ParameterType == typeof(String) || TypeOfParameters.ParameterType == typeof(string)) { parametersArray.Add(Convert.ToString(resultArray[indice])); } else if (TypeOfParameters.ParameterType == typeof(ushort)) { parametersArray.Add(Convert.ToUInt16(resultArray[indice])); } else if ((TypeOfParameters.ParameterType.BaseType == typeof(Enum))) { parametersArray.Add(Convert.ToInt32(resultArray[indice])); } else { parametersArray.Add(resultArray[indice]); } indice++; } object[] Arrayparameters = parametersArray.ToArray(); result = methodInfo.Invoke(objectForInvoke, Arrayparameters); if (result == null) { result = "void"; } ASCIIEncoding asen = new ASCIIEncoding(); np = new NetworkPacket(asen.GetBytes(result.ToString())); } else { PropertyInfo propertyInfo = objectForInvoke.GetType().GetProperty(resultArray[1].ToString()); if (propertyInfo != null) { if (resultArray.Count == 2) { result = propertyInfo.GetGetMethod().Invoke(objectForInvoke, null); if (result.GetType().BaseType == typeof(Enum)) { result = Convert.ToInt32(result); } } else { object[] Param = { Convert.ToUInt32(resultArray[2])}; if (propertyInfo.PropertyType.BaseType == typeof(Enum)) { Param[0] = Convert.ToInt32(Param[0]); } result = propertyInfo.GetSetMethod().Invoke(objectForInvoke, Param); if (result == null) { result = "void"; } else if (result.GetType().BaseType == typeof(Enum)) { result = Convert.ToInt32(result); } } ASCIIEncoding asen = new ASCIIEncoding(); np = new NetworkPacket(asen.GetBytes(JSON.Serialize(result))); } } } SendTo(data.id, np); data.readbuf = new byte[BUFFER_BYTESIZE]; handler.BeginReceive(data.readbuf, 0, data.readbuf.Length, SocketFlags.None, new AsyncCallback(this.ReceiveCallback), handler); } else { this.ClientDisconnect(handler); } } catch (SocketException) { this.ClientDisconnect(handler); } catch (Exception _e) { LogException(_e); this.ClientDisconnect(handler); } } else { this.ClientDisconnect(handler); } }
/// <summary> /// Send data to one client /// </summary> /// <param name="clientId"></param> /// <param name="np"></param> public void SendTo(int clientId, NetworkPacket np) { ClientData data = getClient(clientId); data.sendbuf = np.GetBuffer(); data.sock.BeginSend(data.sendbuf, 0, data.sendbuf.Length, SocketFlags.None, new AsyncCallback(SendCallBack), data.sock); }
/// <summary> /// Send data to all connected clients /// </summary> /// <param name="np"></param> public void SendToAll(NetworkPacket np) { byte[] _sendbuf; _sendbuf = np.GetBuffer(); foreach (ClientData cd in clients) { // ici on travail en asynchrone afin de limité le décalage entre les clients cd.sock.BeginSend(_sendbuf, 0, _sendbuf.Length, SocketFlags.None, new AsyncCallback(SendCallBack), cd.sock); } }
/// <summary> /// Initializes a new instance of the <see cref="ServerDataReceivedEventArgs"/> class. /// </summary> /// <param name="data"></param> /// <param name="clientId"></param> public ServerDataReceivedEventArgs(NetworkPacket data, int clientId) { this.packet = data; this.clientId = clientId; }