Example #1
0
        /// <summary>
        /// Creates a network stream for the method with the specified string name and returns the method info
        /// </summary>
        /// <param name="methodName">The name of the method to call from this class</param>
        /// <param name="receivers">The players on the network that will be receiving RPC</param>
        /// <param name="arguments">The list of arguments that will be sent for the RPC</param>
        /// <returns></returns>
        private int GetStreamRPC(string methodName, NetworkReceivers receivers, params object[] arguments)
        {
            foreach (KeyValuePair <int, KeyValuePair <MethodInfo, List <IBRPCIntercept> > > rpc in RPCs)
            {
                if (rpc.Value.Key.Name == methodName)
                {
                    if (!NetworkingManager.IsOnline)
                    {
                        return(rpc.Key);
                    }

                    getStreamBuffer.Clear();
                    ObjectMapper.MapBytes(getStreamBuffer, rpc.Key);

#if UNITY_EDITOR
                    int argCount = 0;
                    foreach (var param in rpc.Value.Key.GetParameters())
                    {
                        if (param.ParameterType != typeof(MessageInfo))
                        {
                            argCount++;
                        }
                    }

                    if (arguments.Length != argCount)
                    {
                        throw new NetworkException("The number of arguments [" + arguments.Length + "] provided for the " + methodName + " RPC call do not match the method signature argument count [" + rpc.Value.Key.GetParameters().Length + "]");
                    }
#endif

                    if (arguments != null && arguments.Length > 0)
                    {
                        ObjectMapper.MapBytes(getStreamBuffer, arguments);
                    }

                    bool buffered = receivers == NetworkReceivers.AllBuffered || receivers == NetworkReceivers.OthersBuffered;

                    rpcNetworkingStream.SetProtocolType(OwningNetWorker is CrossPlatformUDP ? Networking.ProtocolType.UDP : Networking.ProtocolType.TCP);
                    rpcNetworkingStream.Prepare(OwningNetWorker, NetworkingStream.IdentifierType.RPC, this, getStreamBuffer, receivers, buffered);

                    return(rpc.Key);
                }
            }

            throw new NetworkException(14, "No method marked with [BRPC] was found by the name " + methodName);
        }
        private void ConnectionLoop(object server)
        {
            while (true)
            {
                try
                {
                    TcpClient client = ((TcpListener)server).AcceptTcpClient();

                    if (Connections >= MaxConnections)
                    {
                        lock (writeMutex)
                        {
                            writeBuffer.Clear();
                            ObjectMapper.MapBytes(writeBuffer, "Max Players Reached On Server");

                            staticWriteStream.SetProtocolType(Networking.ProtocolType.TCP);
                            WriteAndClose(client, staticWriteStream.Prepare(
                                              this, NetworkingStream.IdentifierType.Disconnect, 0, writeBuffer, noBehavior: true));
                        }

                        return;
                    }

                    // TODO:  Set the name
                    string name = string.Empty;

                    NetworkingPlayer player = new NetworkingPlayer(ServerPlayerCounter++, client.Client.RemoteEndPoint.ToString(), client, name);

                    lock (clientMutex)
                    {
                        Players.Add(player);
                    }
                }
                catch (Exception exception)
                {
#if !BARE_METAL
                    UnityEngine.Debug.LogException(exception);
#endif
                    Disconnect();
                }
            }
        }
Example #3
0
        protected void StreamReceived(NetworkingPlayer sender, BMSByte bytes)
        {
            if (TrackBandwidth)
            {
                BandwidthIn += (ulong)bytes.Size;
            }

            lock (tmp)
            {
                bytes.MoveStartIndex(1);
                readStream.Reset();

                if (base.ProcessReceivedData(sender, bytes, bytes[0]))
                {
                    return;
                }

                // TODO:  Not needed after initialization
                readStream.SetProtocolType(Networking.ProtocolType.TCP);
                if (readStream.Consume(this, sender, bytes) == null)
                {
                    return;
                }

                CurrentStreamOwner = sender;

                // Do not process player because it is processed through the listener
                if (readStream.identifierType == NetworkingStream.IdentifierType.Player)
                {
                    if (!Connected)
                    {
                        OnConnected();
                    }

                    return;
                }

                if (!readStream.Ready)
                {
                    return;
                }

                // TODO:  These need to be done better since there are many of them
                if (readStream.Bytes.Size < 22)
                {
                    try
                    {
                        if (ObjectMapper.Compare <string>(readStream, "update"))
                        {
                            UpdateNewPlayer(sender);
                        }

                        if (ObjectMapper.Compare <string>(readStream, "disconnect"))
                        {
                            // TODO:  If this eventually sends something to the player they will not exist
                            Disconnect(sender);
                            return;
                        }
                    }
                    catch
                    {
                        throw new NetworkException(12, "Mal-formed defalut communication");
                    }
                }

                if (ReadStream(sender, readStream) && IsServer)
                {
                    RelayStream(readStream);
                }

                DataRead(sender, readStream);
            }
        }
        private void Listen(object sender, DoWorkEventArgs e)
        {
            while (true)
            {
                if (listenWorker.CancellationPending)
                {
                    e.Cancel = true;
                    break;
                }

                TcpListener tcpListener = (TcpListener)e.Argument;

                try
                {
                    for (int i = 0; i < Players.Count; i++)
                    {
                        if (!((TcpClient)Players[i].SocketEndpoint).Connected)
                        {
                            Players.RemoveAt(i--);
                        }
                    }

                    // Create a TCP socket.
                    // If you ran this server on the desktop, you could use
                    // Socket socket = tcpListener.AcceptSocket()
                    // for greater flexibility.
                    TcpClient tcpClient = tcpListener.AcceptTcpClient();

                    if (Connections >= MaxConnections)
                    {
                        lock (writeMutex)
                        {
                            writeBuffer.Clear();
                            ObjectMapper.MapBytes(writeBuffer, "Max Players Reached On Server");

                            staticWriteStream.SetProtocolType(Networking.ProtocolType.TCP);
                            WriteAndClose(tcpClient, staticWriteStream.Prepare(
                                              this, NetworkingStream.IdentifierType.Disconnect, 0, writeBuffer, noBehavior: true));
                        }

                        return;
                    }

                    // TODO:  Set the name
                    string name = string.Empty;

                    NetworkingPlayer player = new NetworkingPlayer(ServerPlayerCounter++, tcpClient.Client.RemoteEndPoint.ToString(), tcpClient, name);

                    lock (Players)
                    {
                        Players.Add(player);
                    }

                    listenWorker.ReportProgress(0, player);
                }
                catch (NetworkException exception)
                {
#if !BARE_METAL
                    UnityEngine.Debug.LogException(exception);
#endif
                    Disconnect();
                }
            }
        }