Example #1
0
    public void Send(params object[] p)
    {
        if (!GameConfig.MpEnabled)
        {
            return;
        }

        string msg = "";

        foreach (object o in p)
        {
            if (msg.Length > 0)
            {
                msg += " ";
            }
            msg += o.ToString();
        }
        if (!msg.EndsWith("\n"))
        {
            msg += "\n";
        }
        byte[] encoded = Encoding.ASCII.GetBytes(msg);
        try {
            Stream.Write(encoded, 0, encoded.Length);
            Stream.Flush();
        } catch (System.IO.IOException) {
            Restart();
        }
        catch (Exception ex) {
            MpUtils.Log(MpUtils.LogLevel.Error, "MpClient",
                        "Unhandled exception in MpClient.Send of type {0}: {1}",
                        ex.GetType().Name, ex.Message);
        }
    }
Example #2
0
 private void SomeoneDropped(string cid)
 {
     if (OtherPlayers.ContainsKey(cid))
     {
         DelegateWork(() => {
             if (OtherPlayers.ContainsKey(cid))
             {
                 OtherPlayer op = OtherPlayers[cid];
                 GameObject.Destroy(op.Actor);
                 OtherPlayers.Remove(cid);
                 MpUtils.Log(MpUtils.LogLevel.Info, "MpHandler",
                             "Someone dropped out, was {0}, now {1} other players",
                             cid, OtherPlayers.Count);
             }
         });
     }
 }
Example #3
0
 private void SomeoneJoined(string cid)
 {
     if (!OtherPlayers.ContainsKey(cid))
     {
         DelegateWork(() => {
             if (!OtherPlayers.ContainsKey(cid))
             {
                 OtherPlayer op    = new OtherPlayer(cid);
                 OtherPlayers[cid] = op;
                 op.Actor          = UnityUtils.LoadResource <GameObject>("Prefabs/OtherPlayer", true);
                 MpUtils.Log(MpUtils.LogLevel.Info, "MpHandler",
                             "Someone joined {0}, now {1} other players",
                             cid, OtherPlayers.Count);
             }
         });
     }
 }
Example #4
0
    private void ReadSync()
    {
        string accum = "";

        byte[] buff = new byte[512];

        MpUtils.Log(MpUtils.LogLevel.Warning, "MpClient", "Now reading from connection");
        while (ContinueRunning)
        {
            try
            {
                IAsyncResult ar = Stream.BeginRead(buff, 0, buff.Length, null, null);
                while (!ar.IsCompleted && ContinueRunning)
                {
                    Thread.Sleep(200);
                }
                if (!ContinueRunning)
                {
                    return;
                }
                int    read    = Stream.EndRead(ar);
                string decoded = Encoding.ASCII.GetString(buff, 0, read);
                accum += decoded;
                while (accum.Contains("\n"))
                {
                    string nextLine = accum.Substring(0, accum.IndexOf("\n"));
                    accum = accum.Substring(accum.IndexOf("\n") + 1);
                    ParseAndDoleOut(nextLine);
                }
            }
            catch (System.IO.IOException)
            {
                MpUtils.Log(MpUtils.LogLevel.Warning, "MpClient", "Connection broken, no longer reading");
                ContinueRunning = false;
            }
            catch (Exception ex)
            {
                MpUtils.Log(MpUtils.LogLevel.Error, "MpClient",
                            "Unhandled exception in MpClient.ReadSync of type {0}: {1}",
                            ex.GetType().Name, ex.Message);
                ContinueRunning = false;
            }
        }
    }
Example #5
0
    private void ParseAndDoleOut(string feed)
    {
        List <string> feedParts = new List <string>(feed.Split(new char[] { ' ' }));

        if (feedParts.Count == 0)
        {
            return;
        }
        feedParts.Reverse();
        Stack <string> parts = new Stack <string>(feedParts);

        string cid = parts.Pop();

        if (cid == "identity")
        {
            Identity = parts.Pop();
            MpUtils.Log(MpUtils.LogLevel.Info, "MpClient",
                        "[MpClient] Now identity {0}", Identity);
        }
        else if (cid == "error")
        {
            MpUtils.Log(MpUtils.LogLevel.Warning, "MpClient",
                        "[MpClient] Server sent error: {0}", feed);
        }
        else if (cid == Identity)
        {
            /* ignore stuff about us, we already know yo */
        }
        else
        {
            string cmd = parts.Pop();
            if (cmd == "position")
            {
                if (OnPositionUpdate != null)
                {
                    float px = float.Parse(parts.Pop());
                    float py = float.Parse(parts.Pop());
                    float pz = float.Parse(parts.Pop());
                    OnPositionUpdate(cid, px, py, pz);
                }
            }
            else if (cmd == "join")
            {
                if (OnSomeoneJoined != null)
                {
                    OnSomeoneJoined(cid);
                }
            }
            else if (cmd == "drop")
            {
                if (OnSomeoneDropped != null)
                {
                    OnSomeoneDropped(cid);
                }
            }
            else
            {
                MpUtils.Log(MpUtils.LogLevel.Error, "MpClient",
                            "No handler for cmd {0} on cid {1}", cmd, cid);
            }
        }
    }