public static void SendToNet <EventType>(FFPacket <EventType> netPacket, bool varifiedPacket) { // any Data scrambling which is based on type needs to happen here. It is unscrambled // in FFPacket <EventType> .Encrypt(ref netPacket.message); //FFClient.SendPacket(netPacket, varifiedPacket); return; }
public void SendNetPacket <MessageType>(FFPacket <MessageType> packet, bool varifiedPacket) { long senderId = packet.senderId; // Client Id packet.senderId = 0; // from Server, id: 0 MemoryStream ms = new MemoryStream(); BinaryFormatter bf = new BinaryFormatter(); FFSystem.InitBinaryFormatter(bf); FFPacket <MessageType> .Encrypt(ref packet.message); /*------------------------------*/ // Get client Data ClientData cData; // Valid Client Ids: 1 - MaxLong if (singleton._clientData.TryGetValue(senderId, out cData)) { } else if (senderId == -1) // -1 is for UnInitialized IDs { // Un Initialized ID will not be able to send UDP Packets to the correct client, but TCP should work. cData = clientData; Debug.Log("Recieved Uninitialized SenderId"); // debug } else // Bad ID { Debug.LogError("Error in FFServer.ClientSocket.SendNetPacket<" + typeof(MessageType).ToString() + ">(FFPacket<" + typeof(MessageType).ToString() + ">, bool " + "\nTried to Send a packet, but we couldn't find the senderId's ClientData." + "\nSenderId: " + senderId); return; } /*------------------------------*/ bf.Serialize(ms, packet); byte[] packetData = ms.GetBuffer(); ms.Flush(); ms.Dispose(); try { if (varifiedPacket == true) { //Debug.Log("Server Sent Packet via TCP of type :" + typeof(MessageType).ToString()); // debug cData.clientSocketTCP.socket.BeginSend(packetData, 0, packetData.Length, SocketFlags.None, new AsyncCallback(SendCallbackTCP), cData.clientSocketTCP); } else { //Debug.Log("Server Sent Packet via UDP of type :" + typeof(MessageType).ToString()); // debug cData.clientSocketUDP.udpClient.Send(packetData, packetData.Length, cData.clientSocketUDP.udpEndPointRemote); } } catch (Exception exp) { Debug.LogError("Error in FFServer.ClientSocket.SendNetPacket<" + typeof(MessageType).ToString() + ">(FFPacket<" + typeof(MessageType).ToString() + ">, bool)" + "\nException: " + exp.Message + "\nClient ID: " + clientData.clientId + "\nClient Name: " + clientData.clientName); return; } }