Пример #1
0
 public MBMessage DealRequest(MBConnect c, MBMessage req)
 {
     MBMessage resp;
     if (req.FC == 0x01)
     {
         resp = AckReadCoil(req);
     }
     else if (
        req.FC == 0x05)
     {
         resp = AckWriteCoil(req);
     }
     else
     {
         resp = new MBMessage(req);
         resp.FC = (byte)(0x80 | req.FC);
         resp.SetBody(new byte[] { MBException.E01_ILLEGAL_FUNCTION });
     }
     return resp;
 }
Пример #2
0
 void CloseClient(MBConnect c)
 {
     string ip = c.IP;
     if (OnClientDisconnect != null)
     {
         OnClientDisconnect.Invoke(c);
     }
     MBConnect _removed;
     _connections.TryRemove(ip, out _removed);
     Log.InfoFormat("客户端断开: {0}", ip);
     try
     {
         c.Socket.Shutdown(SocketShutdown.Both);
         c.Socket.Close();
     }
     catch (Exception e)
     {
         Log.ErrorFormat("Closing error:{0}", e.Message);
     }
 }
Пример #3
0
 public ReceiveThread(MBServer server, MBConnect client)
 {
     this._client = client;
     this._server = server;
 }
Пример #4
0
 private MBMessage _OnMsgDealed(MBConnect c, MBMessage req)
 {
     MBMessage resp = null;
     if (OnMsgDealed != null)
     {
         resp = OnMsgDealed.Invoke(c, req);
     }
     return resp;
 }
Пример #5
0
        private void ListenClientConnect()
        {
            while (true)
            {
                if (_serverReady && _serverSocket != null)
                {
                    Socket clientSocket = _serverSocket.Accept();
                    clientSocket.IOControl(IOControlCode.KeepAliveValues, GetKeepOption(), null);
                    string ip = clientSocket.RemoteEndPoint.ToString();
                    MBConnect c = new MBConnect(clientSocket, ip);
                    _connections[ip] = c;
                    Log.InfoFormat("客户端已连接: {0} ", ip);
                    if (OnClientConnect != null)
                    {
                        OnClientConnect.Invoke(c);
                    }

                    ReceiveThread _client = new ReceiveThread(this, c);
                    _client.OnMsgDealed = _OnMsgDealed;
                    Thread t = new Thread(_client.DoWork);
                    t.Start();
                }
                else
                {
                    break;// 停止.
                }
                Thread.Sleep(10);
            }
        }
Пример #6
0
 private void UpdateMBClient(MBConnect c, ClientAction t)
 {
     if (lv_client.InvokeRequired)
     {
         UpdateMBClientCallback cb = new UpdateMBClientCallback(UpdateMBClient);
         this.Invoke(cb, new object[] { c, t });
     }
     else
     {
         ListViewItem item = null;
         if (t == ClientAction.ADD)
         {
             item = new ListViewItem(new string[10]); //#,ip,recv, send, elapse, recv_bytes, send_bytes
             _clientRows[c.IP] = item;
             lv_client.Items.Add(item);
         }
         else
         {
             item = _clientRows[c.IP];
         }
         if (t == ClientAction.ADD || t == ClientAction.UPDATE)
         {
             item.SubItems[ClientColumns.IP].Text = c.IP;
             item.SubItems[ClientColumns.REQ].Text = Convert.ToString(c.Req);
             item.SubItems[ClientColumns.ACK].Text = Convert.ToString(c.Ack);
             item.SubItems[ClientColumns.ELAPSE].Text = Convert.ToString(c.Elapsed);
             item.SubItems[ClientColumns.RECV].Text = Convert.ToString(c.Recv);
             item.SubItems[ClientColumns.SENT].Text = Convert.ToString(c.Sent);
             item.ImageKey = "green";
         }
         else
         {
             lv_client.Items.Remove(item);
         }
     }
 }
Пример #7
0
 private MBMessage OnMBClientRequest(MBConnect c, MBMessage req)
 {
     UpdateMBClient(c, ClientAction.UPDATE);
     if (_handler != null)
     {
         return _handler.DealRequest(c, req);
     }
     return null;
 }
Пример #8
0
 private void OnMBClientDisconnected(MBConnect c)
 {
     UpdateMBClient(c, ClientAction.REMOVE);
 }
Пример #9
0
 private void OnMBClientConnected(MBConnect c)
 {
     UpdateMBClient(c, ClientAction.ADD);
 }