//连接GPS服务端 public void InitSocket(SocketParam sp) { ip = sp.Ip; port = sp.Port; tcpClient = new BekUtils.Communication.TCPUtil(); }
private void SampleThreadProc() { // TO DO : 这里要结合业务代码,获取IP、端口、串口等参数 string ip = string.Empty; int port = 0; string com = string.Empty; int rate = 0; string pwd = string.Empty; SampleGPS sampleGps = new SampleGPS(); SocketParam socketParam = new SocketParam(ip, port); sampleGps.InitSocket(socketParam); sampleGps.StartSample(); ComSampleBase sampleOBD = new SampleOBD(); ComParam comParam = new ComParam(com, rate, pwd); sampleOBD.SetCom(comParam); sampleOBD.StartSample(); while (true) { try { GpsData gpsData = new GpsData(); sampleGps.RecvData(ref gpsData); lock (lockGPS) { listGpsData.Add(gpsData); if (listGpsData.Count > sampleMaxCount) { listGpsData.RemoveAt(0); } } SampleData sampleData = new SampleData(); sampleOBD.RecvData(ref sampleData); lock (lockOBD) { listSampleData.Add(sampleData); if (listSampleData.Count > sampleMaxCount) { listSampleData.RemoveAt(0); } } } catch (Exception e) { logger.ErrorFormat("SampleThreadProc catch an error and continue, {0}", e.Message); } Thread.Sleep(BaseDefine.COMMON_VALUE_SLEEP_200); } }
void socket_Callback(IAsyncResult ar) { SocketParam para = ar.AsyncState as SocketParam; this.m_Mutex.WaitOne(); if (this.m_Tcp_S == null) { goto disconnected; } byte[] data = new byte[para.Socket.EndReceive(ar)]; if (data.Length == 0) { goto disconnected; } Buffer.BlockCopy(para.Buffer, 0, data, 0, data.Length); this.m_Datas.Enqueue(new ViewInfo(para.Socket.RemoteEndPoint) { Data = data }); this.setReciSum(data.Length); if (this.m_Model != null) { this.m_Model.OnClientDataArrived(para.Socket.RemoteEndPoint, data); } para.Socket.BeginReceive(para.Buffer, 0, 65536, SocketFlags.None, this.socket_Callback, para); release: this.m_Mutex.ReleaseMutex(); return; disconnected: if (!para.Socket.Connected) { goto release; } this.Invoke((Action)(() => this.comboBox2.Items.Remove(para.Socket.RemoteEndPoint))); this.m_Clients.Remove(para.Socket); if (this.m_Model != null) { this.m_Model.OnClientDisconnect(para.Socket.RemoteEndPoint); } this.comboBox2_SelectedValueChanged(null, EventArgs.Empty); goto release; }
// TCP 服务 void server_Callback(IAsyncResult ar) { this.m_Mutex.WaitOne(); if (this.m_Tcp_S == null) { goto release; } Socket socket = this.m_Tcp_S.EndAcceptSocket(ar); this.Invoke((Action)(() => this.comboBox2.Items.Add(socket.RemoteEndPoint))); this.m_Clients.Add(socket); SocketParam para = new SocketParam(socket); if (this.m_Model != null) { this.m_Model.OnClientConnect(socket.RemoteEndPoint); } socket.BeginReceive(para.Buffer, 0, 65536, SocketFlags.None, this.socket_Callback, para); this.m_Tcp_S.BeginAcceptSocket(this.server_Callback, this.m_Tcp_S); release: this.m_Mutex.ReleaseMutex(); }
// TCP 客户端 void client_Callback(IAsyncResult ar) { SocketParam para = ar.AsyncState as SocketParam; this.m_Mutex.WaitOne(); if (this.m_Tcp_C == null) { goto release; } try { byte[] data = new byte[para.Socket.EndReceive(ar)]; if (data.Length == 0) { goto release; } Buffer.BlockCopy(para.Buffer, 0, data, 0, data.Length); this.m_Datas.Enqueue(new ViewInfo(para.Socket.RemoteEndPoint) { Data = data }); this.setReciSum(data.Length); if (this.m_Model != null) { this.m_Model.OnClientDataArrived(data); } para.Socket.BeginReceive(para.Buffer, 0, 65536, SocketFlags.None, this.client_Callback, para); } catch { goto disconnected; } release: this.m_Mutex.ReleaseMutex(); return; disconnected: this.m_Mutex.ReleaseMutex(); this.Invoke((Action)(() => this.btnConnect_Click(null, null))); }
// 开始连接 private void btnConnect_Click(object sender, EventArgs e) { if (this.txtLocalPort.Enabled) { // 新建连接 if (string.IsNullOrEmpty(this.ipEditBox1.Text) || this.txtLocalPort.TextLength == 0) { MessageBox.Show("请正确填写【IP】和【端口】设置"); return; } switch (this.comboBox1.SelectedIndex) { case 0: // UDP this.m_Udp = new UdpClient(this.m_Point = new System.Net.IPEndPoint(this.ipEditBox1.IP, int.Parse(this.txtLocalPort.Text))); //uint IOC_IN = 0x80000000; //uint IOC_VENDOR = 0x18000000; //uint SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12; //this.m_Udp.Client.IOControl((int)SIO_UDP_CONNRESET, new byte[] { Convert.ToByte(false) }, null); this.m_Udp.Client.IOControl(-1744830452, new byte[] { 0 }, null); this.m_Udp.BeginReceive(this.udp_Callback, this.m_Udp); this.label5.Text = "远端IP:"; this.comboBox2.Hide(); this.ipEditBox2.Show(); this.label7.Show(); this.txtRemotePort.Show(); this.panel4.Show(); break; case 1: // Client this.m_Tcp_C = new TcpClient(); try { this.m_Tcp_C.Connect(this.m_Point = new System.Net.IPEndPoint(this.ipEditBox1.IP, int.Parse(this.txtLocalPort.Text))); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } SocketParam para = new SocketParam(this.m_Tcp_C.Client); this.m_Tcp_C.Client.BeginReceive(para.Buffer, 0, 65536, SocketFlags.None, this.client_Callback, para); break; default: // Server this.m_Tcp_S = new TcpListener(this.m_Point = new System.Net.IPEndPoint(this.ipEditBox1.IP, int.Parse(this.txtLocalPort.Text))); this.m_Tcp_S.Start(); this.m_Tcp_S.BeginAcceptSocket(this.server_Callback, this.m_Tcp_S); this.label5.Text = "客户端:"; this.comboBox2.Items.Clear(); this.comboBox2.Show(); this.ipEditBox2.Hide(); this.label7.Hide(); this.txtRemotePort.Hide(); this.panel4.Show(); break; } this.txtLocalPort.Enabled = this.ipEditBox1.Enabled = this.comboBox1.Enabled = false; this.btnConnect.Text = "断开(&N)"; this.chkAutoSend_CheckedChanged(null, null); } else { // 断开连接 this.StopThread(); // 停止自动发送 this.m_Mutex.WaitOne(); this.panel4.Hide(); switch (this.comboBox1.SelectedIndex) { case 0: // UDP this.m_Udp.Close(); (this.m_Udp as IDisposable).Dispose(); this.m_Udp = null; break; case 1: // Client this.m_Tcp_C.Close(); (this.m_Tcp_C as IDisposable).Dispose(); this.m_Tcp_C = null; break; default: // Server this.m_Mutex.ReleaseMutex(); for (int i = this.m_Clients.Count - 1; i >= 0; i--) { this.m_Clients[i].Dispose(); } this.m_Mutex.WaitOne(); this.comboBox2.Items.Clear(); this.m_Tcp_S.Stop(); this.m_Tcp_S = null; break; } this.m_Clients.Clear(); this.m_Mutex.ReleaseMutex(); this.txtLocalPort.Enabled = this.ipEditBox1.Enabled = this.comboBox1.Enabled = true; this.btnConnect.Text = "连接(&N)"; } }
private List <NormalParam> GetAllIbcParam() { List <NormalParam> list = new List <NormalParam>(); SocketParam sp = new SocketParam() { id = "c13", rev = 0, condition = new Condition() { marketid = "L", sporttype = new[] { 1 }, bettype = new[] { 1, 3, 5, 7, 8, 15 }, sorting = "n" } }; list.Add(new NormalParam() { Stage = (int)MatchStageEnum.Live, TakeMode = TakeMode.Push, SocketParam = sp }); sp = new SocketParam() { id = "c12", rev = 0, condition = new Condition() { marketid = "T", sporttype = new[] { 1 }, bettype = new[] { 1, 3, 5, 7, 8, 15 }, sorting = "n" } }; list.Add(new NormalParam() { Stage = (int)MatchStageEnum.Today, TakeMode = TakeMode.Push, SocketParam = sp }); sp = new SocketParam() { id = "c11", rev = 0, condition = new Condition() { marketid = "E", sporttype = new[] { 1 }, bettype = new[] { 1, 3, 5, 7, 8, 15 }, sorting = "n" } }; list.Add(new NormalParam() { Stage = (int)MatchStageEnum.Early, TakeMode = TakeMode.Push, SocketParam = sp }); // 其他运动参数 // 现在IBC不支持同时订阅其他运动,只能一种运动一个订阅 var ibcSportIds = new[] { 2, 3, 4, 5, 6, 7, 8, 9, 10, 25, 26, 43, 50 }; var otherLives = ibcSportIds.Select(id => new SocketParam() { id = "c23" + id, rev = 0, condition = new Condition() { marketid = "L", sporttype = new[] { id }, bettype = new[] { 1, 2, 3, 5, 7, 8, 12, 15, 20, 609, 610, 611 }, sorting = "n" } }).ToList(); otherLives.ForEach(p => list.Add(new NormalParam() { Stage = (int)MatchStageEnum.Live, TakeMode = TakeMode.Push, SocketParam = p })); var otherTodays = ibcSportIds.Select(id => new SocketParam() { id = "c22" + id, rev = 0, condition = new Condition() { marketid = "T", sporttype = new[] { id }, bettype = new[] { 1, 2, 3, 5, 7, 8, 12, 15, 20, 609, 610, 611 }, sorting = "n" } }).ToList(); otherTodays.ForEach(p => list.Add(new NormalParam() { Stage = (int)MatchStageEnum.Today, TakeMode = TakeMode.Push, SocketParam = p })); var otherEarlys = ibcSportIds.Select(id => new SocketParam() { id = "c21" + id, rev = 0, condition = new Condition() { marketid = "E", sporttype = new[] { id }, bettype = new[] { 1, 2, 3, 5, 7, 8, 12, 15, 20, 609, 610, 611 }, sorting = "n" } }).ToList(); otherEarlys.ForEach(p => list.Add(new NormalParam() { Stage = (int)MatchStageEnum.Early, TakeMode = TakeMode.Push, SocketParam = p })); return(list); }