Esempio n. 1
0
 public override void OnOpen(USocket us)
 {
     if (onOpen != null)
     {
         onOpen();
     }
 }
Esempio n. 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);
        }
Esempio n. 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;
 }
Esempio n. 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);
 }
Esempio n. 5
0
 public override void OnClose(USocket us, bool fromRemote)
 {
     Log.Error("connection is closed, fromRemote=" + fromRemote);
     if (onClose != null)
     {
         onClose();
     }
 }
Esempio n. 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;
 }
Esempio n. 7
0
 public void Close()
 {
     if (socket != null)
     {
         socket.Close();
     }
     socket   = null;
     sender   = null;
     netState = NetState.Closed;
 }
Esempio n. 8
0
 public void Reset()
 {
     if (socket != null)
     {
         socket.Close();
     }
     socket   = new USocket(listener, protocol);
     sender   = new USocketSender(socket);
     netState = NetState.Inited;
 }
Esempio n. 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);
    }
Esempio n. 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;
                }
            };
        }
Esempio n. 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);
            }
        }
Esempio n. 12
0
    void Start()
    {
        uSocket = new USocket(DispatchNetEvent);

        TestSend();



        ////byte[] 字节数组
        ////反序列化 举例
        //UserRegisterC2S userRegisterC2S1= ProtobufHelper.FromBytes<UserRegisterC2S>(bufferEntity.proto);
    }
Esempio n. 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);
                }
            }
        }
Esempio n. 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;
            }
        }
    }
Esempio n. 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);
        }
Esempio n. 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);
            }
        }
Esempio n. 18
0
 public override void OnIdle(USocket us)
 {
 }
Esempio n. 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);
 }
Esempio n. 21
0
 public USocketSender(USocket socket)
 {
     this.socket = socket;
 }
 public override void OnIdle(USocket us)
 {
     Debug.LogWarning("连接超时");
 }
Esempio n. 23
0
 public override void OnError(USocket us, string err)
 {
     Debug.LogWarning("异常:" + err);
     errorHandle.Handel(new HttpException("异常:" + err,
                                          -1));
 }
Esempio n. 24
0
 public override void OnOpen(USocket us)
 {
     SocketOpenedEvent?.Invoke(us);
 }
Esempio n. 25
0
 public override void OnIdle(USocket us)
 {
     Debug.LogWarning("连接超时");
     errorHandle.Handel(new HttpException("连接超时",
                                          -1));
 }
Esempio n. 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);
 }
Esempio n. 29
0
 public void Reset()
 {
     socket   = new USocket(listener, protocol);
     sender   = new USocketSender(socket);
     netState = NetState.Inited;
 }
Esempio n. 30
0
 static void NetSystemInit()
 {
     uSocket = new USocket(DispatchNetEvent);
     Debug.Log("网络系统初始化完成!");
 }