Exemplo n.º 1
0
        /// <summary>
        /// when Client Try to connect server
        /// </summary>
        /// <param name="o"></param>
        private void Acceptor(IAsyncResult o)
        {
            TcpListener server = o.AsyncState as TcpListener;

            try
            {
                OutBufferClient newClient = new OutBufferClient();
                newClient.NetWork = server.EndAcceptTcpClient(o);
                lstClient.Add(newClient);
                LogerHelper2.ToLog("Robot outBuffer  " + newClient.Name + " is connected now.", 0);
                //RobotoutBuffer Status.UpdateRobotoutBuffer Status("Connected");
                //isoutBuffer onLine = true;
                newClient.NetWork.GetStream().BeginRead(newClient.buffer, 0, newClient.buffer.Length, new AsyncCallback(TCPCallBack), newClient);
                server.BeginAcceptTcpClient(new AsyncCallback(Acceptor), server);//continue listening

                //send F999 to active outBuffer  QueryMessageListAndRunFirstMessage("DUTSTATUS");
                // byte[] tempdata = Encoding.ASCII.GetBytes("F999");
                //MessageReceived.BeginInvoke("", MainoutBuffer Message.ConvertByteToMessage(tempdata), null, null);//async data output
                //Server. QueryMessageListAndRunFirstMessage("DUTSTATUS");
            }
            catch (Exception ex)
            {
                LogerHelper2.ToLog("test method outBuffer Servers.Acceptor catch exception" + ex.Message, 3);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// outBuffer  Client send message to server
        /// </summary>
        /// <param name="ar"></param>
        private void TCPCallBack(IAsyncResult ar)
        {
            OutBufferClient client = (OutBufferClient)ar.AsyncState;

            try
            {
                if (client.NetWork.Connected)
                {
                    NetworkStream ns      = client.NetWork.GetStream();
                    byte[]        recdata = new byte[ns.EndRead(ar)];
                    if (recdata.Length > 0)
                    {
                        Array.Copy(client.buffer, recdata, recdata.Length);
                        if (recdata.Length == 1)
                        {
                            if (recdata[0] == 0x01)
                            {
                                _outBuffIsFree = true;
                            }
                            else
                            {
                                _outBuffIsFree = false;
                            }
                            if (_outBuffIsFree != OutBuffIsFree)
                            {
                                OutBuffIsFree = _outBuffIsFree;
                                if (OutBuffChanged != null)
                                {
                                    OutBuffChanged.BeginInvoke(client.Name, (_outBuffIsFree), null, null);//async data output
                                }
                            }
                        }
                        client.NetWork.GetStream().Write(_sendData, 0, _sendData.Length);
                        ns.BeginRead(client.buffer, 0, client.buffer.Length, new AsyncCallback(TCPCallBack), client);
                    }
                    else
                    {
                        client.DisConnect();
                        LogerHelper2.ToLog("outBuffer  is disconnected now", 2);
                        //outBuffer HeartBeatTimer.Stop();
                        //RobotoutBuffer Status.UpdateRobotoutBuffer Status("Disconnected");
                        //isoutBuffer onLine = false;
                        lstClient.Remove(client);
                    }
                }
            }
            catch (Exception ex)
            {
                LogerHelper2.ToLog("testmothod TCPCallBack catch exception " + ex.Message, 3);
                //RobotoutBuffer Status.UpdateRobotoutBuffer Status("Disconnected");
                //outBuffer HeartBeatTimer.Stop();
                client.DisConnect();
                //isoutBuffer onLine = false;
                lstClient.Remove(client);
            }
        }
Exemplo n.º 3
0
 private bool Send(byte[] data, OutBufferClient client)
 {
     try
     {
         client.NetWork.GetStream().Write(data, 0, data.Length);
     }
     catch (Exception ex)
     {
         LogerHelper2.ToLog("methond outBuffer Server.Send catch exception :" + client.Name + ex.Message, 3);
         client.DisConnect();
         return(false);
     }
     return(true);
 }