Пример #1
0
 public override void OnOpen(USocket us)
 {
     if (onOpen != null)
     {
         onOpen();
     }
 }
Пример #2
0
        public override void OnMessage(USocket us, ByteBuf bb)
        {
            if (onMessage != null)
            {
                onMessage();
            }
            int len = bb.ReadShort();

            if (bb.ReadableBytes() != len)
            {
                Log.Error("数据长度不对!");
                return;
            }
            MessageID msgId = (MessageID)bb.ReadShort();

            byte[]   data    = bb.ReadBytes(len - 2);
            IMessage msgData = MessageMap.GetMessageData(msgId, data);

            if (msgData == null)
            {
                return;
            }
            //Messenger.BroadcastAsync<IMessage>(msgId.ToString(), msgData);
            Messenger.BroadcastAsync(msgId.ToString(), (object)msgData);
        }
Пример #3
0
 void Init()
 {
     protocol = new LVProtocol();
     listener = new USocketListener(OnOpen, OnClose, OnMessage);
     socket   = new USocket(listener, protocol);
     sender   = new USocketSender(socket);
     netState = NetState.Inited;
 }
Пример #4
0
 public SocketChannel(string ip, int port, SocketListener listener, Protocol protocol, ISocketProtocol socketProtocol)
 {
     IP   = ip;
     Port = port;
     this.socketProtocol = socketProtocol;
     this.listener       = listener;
     socket = new USocket(listener, protocol);
 }
Пример #5
0
 public override void OnClose(USocket us, bool fromRemote)
 {
     Log.Error("connection is closed, fromRemote=" + fromRemote);
     if (onClose != null)
     {
         onClose();
     }
 }
Пример #6
0
 void Init()
 {
     networkHelper = (new GameObject("NetworkHelper")).AddComponent <NetworkHelper>();
     protocol      = new LVProtocol();
     listener      = new USocketListener(OnOpen, OnClose);
     socket        = new USocket(listener, protocol);
     sender        = new USocketSender(socket);
     netState      = NetState.Inited;
 }
Пример #7
0
 public void Close()
 {
     if (socket != null)
     {
         socket.Close();
     }
     socket   = null;
     sender   = null;
     netState = NetState.Closed;
 }
Пример #8
0
 public void Reset()
 {
     if (socket != null)
     {
         socket.Close();
     }
     socket   = new USocket(listener, protocol);
     sender   = new USocketSender(socket);
     netState = NetState.Inited;
 }
Пример #9
0
    public void CreateInit(string strIP, int nPort, System.Object objExtend = null)
    {
        _strIP = strIP;
        _nPort = nPort;

        _listener = new KKBaseListener(msgQueue, MsgFactory.Room);
        _protocal = new KKBaseProtocal();
        _socket   = new USocket(_listener, _protocal);

        _socket.Connect(_strIP, nPort);
    }
Пример #10
0
        public UnityShared()
        {
            new UnityServer();
            UBinder              = new USocket(9000);
            UBinder.DataArrived += (sock, data) =>
            {
                // data rcv from broadcast (request from other clients)
                switch (data.Type)
                {
                case "Unity.Packets.Search":
                    Trace.WriteLine("Got Unity search request");
                    var search = data.Decode <Unity.Packets.Search>();

                    var sr = new Packets.SearchResponse();
                    sr.Devices.AddRange(Broadcaster.Inst.ProxyDevices);
                    sr.Port = UnityServer.Inst.Port;
                    Trace.WriteLine("Sent response with " + sr.Devices.Count + " devices");
                    sock.Send(sr);
                    break;

                default:
                    Trace.WriteLine("Unknown Bind packet: " + data.Type);
                    break;
                }
            };
            UBinder.Bind();

            UBroadcaster              = new USocket(new IPEndPoint(IPAddress.Broadcast, 9000));
            UBroadcaster.DataArrived += (sock, data) =>
            {
                // data rcv from response to a broadcast packet
                // (response from other clients to self broadcast)

                switch (data.Type)
                {
                case "Unity.Packets.SearchResponse":
                    var search = data.Decode <Unity.Packets.SearchResponse>();
                    Trace.WriteLine("Got response from " + sock.IPAddress + " with " + search.Devices.Count + " devices");
                    for (int i = 0; i < search.Devices.Count; i++)
                    {
                        var dev = search.Devices[i];
                        dev.Port = search.Port;
                        dev.Host = sock.IPAddress.ToString();
                        Collector.Inst.OnDiscovered(dev);
                    }
                    break;

                default:
                    Trace.WriteLine("Unknown Bcast packet: " + data.Type);
                    break;
                }
            };
        }
Пример #11
0
        public override void OnMessage(USocket us, ByteBuf bb)
        {
            bb.ReaderIndex(us.getProtocol().HeaderLen());
            short cmd = bb.ReadShort();

            byte[] bs = bb.GetRaw();
            using (var stream = new MemoryStream(bs, bb.ReaderIndex(), bb.ReadableBytes()))
            {
                object obj = protocol.DecodeMessage(MessageQueueHandler.GetProtocolType(cmd), stream);
                MessageQueueHandler.PushQueue(cmd, obj);
            }
        }
Пример #12
0
    void Start()
    {
        uSocket = new USocket(DispatchNetEvent);

        TestSend();



        ////byte[] 字节数组
        ////反序列化 举例
        //UserRegisterC2S userRegisterC2S1= ProtobufHelper.FromBytes<UserRegisterC2S>(bufferEntity.proto);
    }
Пример #13
0
    public virtual void SendMessage(int nPort, CMD_Base_Req req)
    {
        USocket socket = _socketPool.NewSocket(_listener, _protocal);

        socket.Connect(_strIP, nPort, (success) =>
        {
            if (!success)
            {
                return;
            }
            socket.Send(req.Serialize());
        });
    }
        public override void OnMessage(USocket us, ByteBuf bb)
        {
            bb.ReaderIndex(us.getProtocol().HeaderLen());
            int cmd = bb.ReadInt();

            Debug.Log("Socket Recive Message ID: " + cmd);
            byte[] bs = bb.GetRaw();
            using (var stream = new MemoryStream(bs, bb.ReaderIndex(), bb.ReadableBytes()))
            {
                if (MessageQueueHandler.GetProtocolType(cmd) != null)
                {
                    object obj = protocol.DecodeMessage(MessageQueueHandler.GetProtocolType(cmd), stream);
                    MessageQueueHandler.PushQueue((short)cmd, obj);
                }
            }
        }
Пример #15
0
    private void OnButtonClick(GameObject go)
    {
        //在这里监听按钮的点击事件
        Debug.LogWarning(go.name);
        switch (go.name)
        {
        case "open":
            if (socket != null)
            {
                if (socket.getStatus() != USocket.STATUS_CLOSED)
                {
                    socket.Close();
                    socket = null;
                }
            }
            SocketListner listner = new MyListner();
            Protocal      p       = new LVProtocal();
            socket = new USocket(listner, p);
            socket.Connect(ip, port);
            break;

        case "send":
            if (socket != null && socket.getStatus() == USocket.STATUS_CONNECTED)
            {
                AuthRequest auth = new AuthRequest();
                auth.ucenterId = 99;
                auth.userCode  = "zs";
//                auth.loginid = "vH0cVEb2R2nRZOa4nxQz0ZsnTGC5pvf4Fn_sOxhLCQYsjGFRIEA5Pe1eTOwtPjrS";
                auth.serverid = 1;
                //         auth.testing = true;
                this.Send(auth);
            }
            break;

        case "close":
            if (socket != null)
            {
                if (socket.getStatus() != USocket.STATUS_CLOSED)
                {
                    socket.Close();
                    socket = null;
                }
                break;
            }
        }
    }
Пример #16
0
        public override void OnMessage(USocket us, ByteBuf bb)
        {
            if (onMessage != null)
            {
                onMessage();
            }
            int len = bb.ReadShort();

            if (bb.ReadableBytes() != len)
            {
                Log.Error("数据长度不对!");
                return;
            }
            MessageID msgId = (MessageID)bb.ReadShort();

            byte[]   data    = bb.ReadBytes(len - 2);
            IMessage msgData = null;

            switch (msgId)
            {
            case MessageID.ReqUserLogin:
            case MessageID.ReqUserRegister:
                Log.Error("收到请求消息非法!");
                return;

            case MessageID.ResUserLogin:
                ResUserLoginMessage userLogin = ResUserLoginMessage.Parser.ParseFrom(data);
                msgData = userLogin;
                break;

            case MessageID.ResUserRegister:
                break;

            default:
                break;
            }
            if (msgData == null)
            {
                return;
            }
            Messenger.BroadcastAsync <IMessage>(msgId.ToString(), msgData);
        }
Пример #17
0
        public override void OnMessage(USocket us, ByteBuf bb)
        {
            int len = bb.ReadShort();

            if (bb.ReadableBytes() != len)
            {
                Log.Error("数据长度不对!");
                return;
            }

            ushort msgId = (ushort)bb.ReadShort();

            byte[]   data    = bb.ReadBytes(len - 2);
            IMessage message = DicParser.Dic[msgId].ParseFrom(data);

            if (onMessage != null)
            {
                onMessage(msgId, message);
            }
        }
Пример #18
0
 public override void OnIdle(USocket us)
 {
 }
Пример #19
0
 public override void OnError(USocket us, string err)
 {
     Log.Error("connection occured an error, err=" + err);
 }
 public override void OnClose(USocket us, bool fromRemote)
 {
     Debug.LogWarning(fromRemote ? "与服务器连接已断开" : "关闭连接");
     OnSocketClosed(us, fromRemote);
 }
Пример #21
0
 public USocketSender(USocket socket)
 {
     this.socket = socket;
 }
 public override void OnIdle(USocket us)
 {
     Debug.LogWarning("连接超时");
 }
Пример #23
0
 public override void OnError(USocket us, string err)
 {
     Debug.LogWarning("异常:" + err);
     errorHandle.Handel(new HttpException("异常:" + err,
                                          -1));
 }
Пример #24
0
 public override void OnOpen(USocket us)
 {
     SocketOpenedEvent?.Invoke(us);
 }
Пример #25
0
 public override void OnIdle(USocket us)
 {
     Debug.LogWarning("连接超时");
     errorHandle.Handel(new HttpException("连接超时",
                                          -1));
 }
Пример #26
0
 public override void OnClose(USocket us, bool fromRemote)
 {
     Debug.LogWarning(fromRemote ? "与服务器连接已断开" : "关闭连接");
     errorHandle.Handel(new HttpException(fromRemote ? "与服务器连接已断开" : "关闭连接",
                                          -1));
 }
 public override void OnOpen(USocket us)
 {
     OnSocketOpened(us);
 }
 public override void OnError(USocket us, string err)
 {
     Debug.LogWarning("异常:" + err);
     OnSocketError(us, err);
 }
Пример #29
0
 public void Reset()
 {
     socket   = new USocket(listener, protocol);
     sender   = new USocketSender(socket);
     netState = NetState.Inited;
 }
Пример #30
0
 static void NetSystemInit()
 {
     uSocket = new USocket(DispatchNetEvent);
     Debug.Log("网络系统初始化完成!");
 }