Ejemplo n.º 1
0
    public void Send(IMessage p)
    {
        if (!connected)
        {
            return;
        }

        try
        {
            NetworkStream stream = tcp.GetStream();
            if (stream.CanWrite)
            {
                int?id = Utils.GetID(p.GetType());
                if (id == null)
                {
                    return;
                }

                byte[] b = ProtobufEncoding.Encode(new Packet()
                {
                    Id = (int)id, Msg = p.ToString()
                }, false);

                stream.WriteAsync(b, 0, b.Length);
            }
        }
        catch (InvalidOperationException ex)
        {
            Disconnect();
        }
        catch (SocketException ex)
        {
            Disconnect();
        }
    }
Ejemplo n.º 2
0
        /// <summary>
        /// 发送数据给服务器
        /// </summary>
        public void Send(Google.Protobuf.IMessage msg)
        {
            if (IsConnected == false)
            {
                Debug.LogError("服务器未连接!");
                return;
            }

            if (null == msg)
            {
                Debug.LogError("消息不能为空!");
                return;
            }

            try
            {
                clientSocket.Send(ProtobufEncoding.Encode(msg));
            }
            catch
            {
                IsConnected = false;
                clientSocket.Shutdown(SocketShutdown.Both);
                clientSocket.Close();
            }
        }
Ejemplo n.º 3
0
        private void ReceiveMsg()
        {
            byte[] buffer = new byte[1024];
            int    len    = 0;

            while (true)
            {
                len = clientSocket.Receive(buffer);
                if (len <= 0)
                {
                    continue;
                }

                var result = ProtobufEncoding.Decode(buffer) as Message;
                Debug.Log(result.MsgType + ", " + result.Agree + ", " + result.Msg);

                // TODO: 消息分发
            }
        }
Ejemplo n.º 4
0
    private void Receive()
    {
        if (!connected)
        {
            return;
        }

        Byte[] bytes   = new Byte[1024];
        int    lengthl = 0;

        while (true)
        {
            try
            {
                if (!connected)
                {
                    return;
                }
                NetworkStream stream = tcp.GetStream();

                int length;

                while ((length = stream.Read(bytes, lengthl, bytes.Length - lengthl)) > 0)
                {
                    KeyValuePair <byte[], List <Google.Protobuf.IMessage> > pair = ProtobufEncoding.Decode(bytes);

                    if (pair.Key.Length > 0)
                    {
                        Array.Copy(pair.Key, 0, bytes, 0, pair.Key.Length);
                        lengthl = pair.Key.Length;
                        Array.Clear(bytes, lengthl, bytes.Length - lengthl);
                    }
                    else
                    {
                        Array.Clear(bytes, 0, bytes.Length);
                        lengthl = 0;
                    }

                    foreach (Packet p in pair.Value.Cast <Packet>())
                    {
                        IMessage msg = Utils.getPacket(p);

                        if (msg != null)
                        {
                            msg.ReadSync();
                        }
                    }
                }
            }
            catch (IOException ex)
            {
                Disconnect();
            }
            catch (SocketException ex)
            {
                Disconnect();
            }
            catch (InvalidOperationException ex)
            {
                Disconnect();
            }
        }
    }