Esempio n. 1
0
        public CommandCore()
        {
            commandObj = CommandObj.Instance;
            socketObj  = GameObject.Find("NetworkManager").GetComponent <SocketObj>();

            ReflectRegisterCommandsNEvents();
        }
Esempio n. 2
0
        /// <summary>
        /// 等待处理新的连接
        /// </summary>
        private void GetAcceptTcpClient()
        {
            try
            {
                semap.WaitOne();
                TcpClient     _tclient = listener.AcceptTcpClient();
                Socket        _socket  = _tclient.Client;
                NetworkStream _stream  = new NetworkStream(_socket, true); //承载这个Socket
                SocketObj     _sks     = new SocketObj(_tclient.Client.RemoteEndPoint as IPEndPoint, _tclient, _stream);
                _sks.NewClientFlag = true;
                AddClientList(_sks);
                _sks.SkStream.BeginRead(recBuffer, 0, recBuffer.Length, new AsyncCallback(EndReader), _sks);

                if (_stream.CanWrite)
                {
                }

                semap.Release();
            }
            catch (Exception ex)
            {
                semap.Release();
                PusbServerMessage(SocketCode.NewClientConnectError, null, ex, null, null);
            }
        }
Esempio n. 3
0
        public void IncrementValue()
        {
            TournamentDao td   = new TournamentDao(database);
            var           list = td.FindAll();
            int           tId  = list[0].ID;

            Assert.IsNotNull(tId);

            PlayerDao pd    = new PlayerDao(database);
            var       plist = pd.FindAll();

            int p1id = plist[0].ID;
            int p2id = plist[1].ID;
            int p3id = plist[2].ID;
            int p4id = plist[3].ID;

            Match m = new Match(p1id, p2id, p3id, p4id, tId, 1, 1, false);

            int i = dao.Insert(m);


            SocketObj obj = new SocketObj();

            obj.match = i;
            obj.team  = 1;

            int?i1 = m.ResultPointsPlayer1;

            BLMatch.IncrementValue(obj);

            Match m1 = dao.FindById(i);
            int?  i2 = m1.ResultPointsPlayer1;

            Assert.AreEqual(i1, i2 - 1);
        }
        public override void OnMessage(string message)
        {
            SocketObj obj = Newtonsoft.Json.JsonConvert.DeserializeObject <SocketObj>(message);

            _connectedChatClients.Broadcast(_userId + " : " + message);

            BLMatch.IncrementValue(obj);
            //base.OnMessage(message);
        }
Esempio n. 5
0
        public SocketClient(Queue <Action> taskQueue, SocketObj _socketObj, object LockObject) : base(LockObject)
        {
            TaskQueue = taskQueue;
            socketObj = _socketObj;

            ReadBuffer       = new byte[SegmentSize];
            SendBuffer       = new byte[SegmentSize];
            packetSerializer = new PacketSerializer(1012);

            StopTask = true;
        }
Esempio n. 6
0
        public SocketServer(Queue <Action> taskQueue, SocketObj _socketObj, object LockObject) : base(LockObject)
        {
            TaskQueue       = taskQueue;
            socketObj       = _socketObj;
            ServerIsRunning = false;

            clientsManager = new SocketClientsManager();
            clientsManager.OnPreReceiveData += ClientsManager_OnPreReceiveData;
            clientsManager.OnReceiveData    += ClientsManager_OnReceiveData;
            clientsManager.OnClientDisposed += ClientsManager_OnClientDisposed;
        }
Esempio n. 7
0
 /// <summary>
 /// 断开,移除所有终端链接
 /// </summary>
 /// 时间:2016-04-12 19:19
 /// 备注:
 public void ClearAllClients()
 {
     if (ClientList != null)
     {
         for (int i = 0; i < ClientList.Count; i++)
         {
             SocketObj _socketClient = ClientList[i];
             ClientList.Remove(_socketClient);
             _socketClient.Close();
         }
     }
 }
    private void Awake()
    {
        socketObj = GameObject.Find("NetworkManager(Clone)")?.GetComponent <SocketObj>();
        if (socketObj == null)
        {
            return;
        }

        clientSender            = socketObj.socketClient;
        socketObj.OnDataInvoke += SocketObj_OnDataInvoke;

        carStatusUIObj = CoreLinker.GetComponent <CarStatusUIObj>();
    }
Esempio n. 9
0
 /// <summary>
 /// 关闭会话
 /// </summary>
 public void Close()
 {
     try
     {
         if (SocketObj != null)
         {
             SocketObj.Close();
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
 }
 /// <summary>
 /// 连接到Server
 /// </summary>
 public void Connect()
 {
     try
     {
         client.Connect(ipEndPoint);
         stream = new NetworkStream(client.Client, true);
         sk     = new SocketObj(ipEndPoint, client, stream);
         sk.SkStream.BeginRead(recBuffer, 0, recBuffer.Length, new AsyncCallback(EndReader), sk);
         PusbClientMessage(SocketCode.ConnectSuccess, null, null, ipEndPoint, null);
     }
     catch (Exception ex)
     {
         PusbClientMessage(SocketCode.ConnectError, null, ex, ipEndPoint, null);
     }
 }
Esempio n. 11
0
        /// <summary>
        /// 断开,移除某个终端连接
        /// </summary>
        /// <param name="ip">IPEndPoint</param>
        /// 时间:2016-04-12 19:21
        /// 备注:
        public void ClearClient(IPEndPoint ip)
        {
            if (ClientList != null)
            {
                SocketObj _sockets = ClientList.Find(o =>
                {
                    return(o.Ip == ip);
                });

                if (_sockets != null)
                {
                    ClientList.Remove(_sockets);
                }
            }
        }
Esempio n. 12
0
        /// <summary>
        /// 向某一位客户端发送信息
        /// </summary>
        /// <param name="ip">客户端IP+端口地址</param>
        /// <param name="sendDataBuffer">发送的数据包</param>
        public void SendToClient(IPEndPoint ip, byte[] sendDataBuffer)
        {
            try
            {
                SocketObj _sks = ClientList.Find(o =>
                {
                    return(o.Ip == ip);
                });

                if (_sks != null)
                {
                    if (_sks.Client.Connected)
                    {
                        NetworkStream _stream = _sks.SkStream;

                        if (_stream.CanWrite)
                        {
                            byte[] _buffer = sendDataBuffer;
                            _stream.Write(_buffer, 0, _buffer.Length);
                        }
                        else
                        {
                            _stream = _sks.Client.GetStream();

                            if (_stream.CanWrite)
                            {
                                byte[] _buffer = sendDataBuffer;
                                _stream.Write(_buffer, 0, _buffer.Length);
                            }
                            else
                            {
                                ClientList.Remove(_sks);
                                PusbServerMessage(SocketCode.RemoveClientConnect, null, null, _sks.Ip, null);
                            }
                        }
                    }
                    else
                    {
                        PusbServerMessage(SocketCode.NoClinets, null, null, _sks.Ip, null);
                    }
                }
            }
            catch (Exception ex)
            {
                PusbServerMessage(SocketCode.SendDataError, null, ex, ip, null);
            }
        }
Esempio n. 13
0
    private void Start()
    {
        SendCamTexture  = false;
        CamQuality      = 3;
        CarStatusObject = GameObject.Find("CarStatusObject");

        objcarDevice = CarStatusObject.GetComponent <ObjectCarDevice>();
        tracerObj    = CarStatusObject.GetComponent <GPSTracerObj>();

        baseCarDivice = objcarDevice.Basecardivice;
        camManager    = GameObject.Find("CamManager").GetComponent <CamManager>();

        socketObj               = GameObject.Find("NetworkManager(Clone)").GetComponent <SocketObj>();
        clientSender            = socketObj.socketClient;
        socketObj.OnDataInvoke += SocketObj_OnDataInvoke;
        StartCoroutine(SendInitData());
    }
        /// <summary>
        /// 端口与Server的连接
        /// </summary>
        public void Disconnect()
        {
            SocketObj _sks = new SocketObj();

            if (client != null)
            {
                client.Client.Shutdown(SocketShutdown.Both);
                Thread.Sleep(10);
                client.Close();
                isClose = true;
                client  = null;
                PusbClientMessage(SocketCode.Disconnect, null, null, ipEndPoint, null);
            }
            else
            {
                PusbClientMessage(SocketCode.Uninitialized, null, null, ipEndPoint, null);
            }
        }
Esempio n. 15
0
        /// <summary>
        /// Adds the client list.
        /// </summary>
        /// <param name="sk">SocketObj</param>
        private void AddClientList(SocketObj sk)
        {
            SocketObj _sockets = ClientList.Find(o =>
            {
                return(o.Ip == sk.Ip);
            });

            if (_sockets == null)
            {
                ClientList.Add(sk);
            }
            else
            {
                ClientList.Remove(_sockets);
                ClientList.Add(sk);
            }

            PusbServerMessage(SocketCode.NewClientConnect, null, null, sk.Ip, null);
        }
        /// <summary>
        /// Ends the reader.
        /// </summary>
        /// <param name="ir">The ir.</param>
        private void EndReader(IAsyncResult ir)
        {
            SocketObj _sks = ir.AsyncState as SocketObj;

            try
            {
                if (_sks != null)
                {
                    if (isClose && client == null)
                    {
                        sk.SkStream.Close();
                        sk.SkStream.Dispose();
                        return;
                    }

                    _sks.Offset = _sks.SkStream.EndRead(ir);
                    byte[] _buffer = new byte[_sks.Offset];
                    Array.Copy(recBuffer, _buffer, _sks.Offset);

                    if (_buffer != null)
                    {
                        string _readString = Encoding.UTF8.GetString(_buffer);

                        if (string.Compare(_readString, "ServerOff", true) == 0)
                        {
                            PusbClientMessage(SocketCode.ServerClose, null, null, _sks.Ip, null);
                        }
                        else
                        {
                            PusbClientMessage(SocketCode.DataReceived, _buffer, null, _sks.Ip, null);
                        }
                    }

                    sk.SkStream.BeginRead(recBuffer, 0, recBuffer.Length, new AsyncCallback(EndReader), sk);
                }
            }
            catch (Exception ex)
            {
                PusbClientMessage(SocketCode.DataReceivedError, null, ex, ipEndPoint, null);
            }
        }
Esempio n. 17
0
        /// <summary>
        /// 异步接收发送的信息.
        /// </summary>
        /// <param name="ir">IAsyncResult</param>
        private void EndReader(IAsyncResult ir)
        {
            SocketObj _sks = ir.AsyncState as SocketObj;

            if (_sks != null && listener != null)
            {
                try
                {
                    if (_sks.NewClientFlag || _sks.Offset != 0)
                    {
                        _sks.NewClientFlag = false;
                        _sks.Offset        = _sks.SkStream.EndRead(ir);

                        if (_sks.Offset != 0)
                        {
                            byte[] _buffer = new byte[_sks.Offset];
                            Array.Copy(recBuffer, _buffer, _sks.Offset);
                            PusbServerMessage(SocketCode.DataReceived, _buffer, null, _sks.Ip, null);
                        }
                        else
                        {
                            ClientList.Remove(_sks);//移除连接终端
                            PusbServerMessage(SocketCode.ClientOffline, null, null, _sks.Ip, null);
                        }

                        _sks.SkStream.BeginRead(recBuffer, 0, recBuffer.Length, new AsyncCallback(EndReader), _sks);
                    }
                }
                catch (Exception ex)
                {
                    lock (syncRoot)
                    {
                        ClientList.Remove(_sks);
                        PusbServerMessage(SocketCode.DataReceivedError, null, ex, _sks.Ip, null);
                    }
                }
            }
        }