Skip to content

Commit

Permalink
STBL469 2024/10/14
Browse files Browse the repository at this point in the history
  Network:ソケット通信による操作に対応
  • Loading branch information
kumatan committed Oct 14, 2024
1 parent b3c4474 commit 64cd8fb
Show file tree
Hide file tree
Showing 10 changed files with 460 additions and 65 deletions.
3 changes: 3 additions & 0 deletions CHANGE.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
�X�V����
STBL469 2024/10/14
Network:�\�P�b�g�ʐM�ɂ�鑀��ɑΉ�

STBL468 2024/09/22
���W�X�^�_���v:POKEY�̕\���ɑΉ�

Expand Down
Binary file removed MDPlayer/MDPlayer-STBL467.exe
Binary file not shown.
Binary file modified MDPlayer/MDPlayer-STBL468.exe
Binary file not shown.
139 changes: 139 additions & 0 deletions MDPlayer/MDPlayerx64/MDServer/MDServer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Net.Security;

namespace MDPlayerx64.MDServer
{
//参考
//https://jp-seemore.com/sys/17648/
//https://tomosoft.jp/design/?p=6699

public class MDServer
{
ManualResetEvent SocketEvent = new ManualResetEvent(false);
IPEndPoint ipEndPoint;
Socket sock = null;
Thread tMain;
bool discon = false;
Action<string> remoteCallback;

public MDServer(int port,Action<string> remoteCallback)
{
this.remoteCallback = remoteCallback;
ipEndPoint = new IPEndPoint(IPAddress.Any, port);
}

public void init()
{
Debug.WriteLine("init ThreadID:" + Thread.CurrentThread.ManagedThreadId);
Debug.WriteLine("サーバー起動中・・・");
tMain = new Thread(new ThreadStart(Round));
tMain.Start();
}

private void Round()
{
while (true)
{
using (sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
sock.Bind(ipEndPoint);
sock.Listen(10);

try
{
Debug.WriteLine("Round ThreadID:" + Thread.CurrentThread.ManagedThreadId);
while (true)
{
SocketEvent.Reset();
sock.BeginAccept(new AsyncCallback(OnConnectRequest), sock);
SocketEvent.WaitOne();
}
}
catch (Exception ex)
{

}
}
if (discon) break;
}
}

private void OnConnectRequest(IAsyncResult ar)
{
try
{
Debug.WriteLine("OnConnectRequest ThreadID:" + Thread.CurrentThread.ManagedThreadId);
SocketEvent.Set();
Socket listener = (Socket)ar.AsyncState;
if (listener == null) return;
Socket handler = listener.EndAccept(ar);
EndPoint ep = handler.RemoteEndPoint;
if (ep != null) Debug.WriteLine(ep.ToString() + " joined");
StateObject state = new()
{
WorkSocket = handler
};
handler.BeginReceive(state.buffer, 0, StateObject.BUFFER_SIZE, 0, new AsyncCallback(ReadCallback), state);
}
catch
{
;
}
}

private void ReadCallback(IAsyncResult ar)
{
try
{
Debug.WriteLine("ReadCallback ThreadID:" + Thread.CurrentThread.ManagedThreadId);
StateObject state = (StateObject)ar.AsyncState;
if (state == null) return;
Socket handler = state.WorkSocket;
if (!handler.Connected) return;
int ReadSize = handler.EndReceive(ar);
if (ReadSize < 1)
{
EndPoint ep = handler.RemoteEndPoint;
if (ep != null) Debug.WriteLine(ep.ToString() + " disconnected");
return;
}
byte[] bb = new byte[ReadSize];
Array.Copy(state.buffer, bb, ReadSize);
string msg = Encoding.UTF8.GetString(bb);
remoteCallback(msg);
Debug.WriteLine(msg);
handler.BeginSend(bb, 0, bb.Length, 0, new AsyncCallback(WriteCallback), state);
}
catch (Exception)
{
}
}

private void WriteCallback(IAsyncResult ar)
{
if (ar == null) return;
Debug.WriteLine("WriteCallback ThreadID:" + Thread.CurrentThread.ManagedThreadId);
StateObject? state = (StateObject?)ar.AsyncState;
if (state == null) return;
Socket handler = state.WorkSocket;
handler?.EndSend(ar);
Debug.WriteLine("送信完了");
handler?.BeginReceive(state.buffer, 0, StateObject.BUFFER_SIZE, 0, new AsyncCallback(ReadCallback), state);
}

public void disConnect()
{
Debug.WriteLine("disConnect ThreadID:" + Thread.CurrentThread.ManagedThreadId);
discon = true;
sock?.Close();
}

}
}
16 changes: 16 additions & 0 deletions MDPlayer/MDPlayerx64/MDServer/StateObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace MDPlayerx64.MDServer
{
public class StateObject
{
public Socket WorkSocket { get; set; }
public const int BUFFER_SIZE = 1024;
internal byte[] buffer = new byte[BUFFER_SIZE];
}
}
35 changes: 35 additions & 0 deletions MDPlayer/MDPlayerx64/Setting.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System.Text;
using System.Text.Json.Serialization;
using System.Xml.Serialization;
#if X64
using MDPlayerx64.Properties;
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.Devices;
#else
using MDPlayer.Properties;
#endif
Expand Down Expand Up @@ -627,6 +629,20 @@ public int playMode
}
}

private Network _network = new();
public Network network
{
get
{
return _network;
}

set
{
_network = value;
}
}

private Other _other = new();
public Other other
{
Expand Down Expand Up @@ -1434,6 +1450,24 @@ public OutputDevice Copy()
// }
//}

[Serializable]
public class Network
{
public bool useMDServer { get; set; }=false;
public int port { get; set; } = 11000;

public Network Copy()
{
Network network = new()
{
useMDServer = this.useMDServer,
port = this.port,
};

return network;
}
}

[Serializable]
public class Other
{
Expand Down Expand Up @@ -6092,6 +6126,7 @@ public Setting Copy()
setting.FileSearchPathList = this.FileSearchPathList;

setting.other = this.other.Copy();
setting.network = this.network.Copy();
setting.debug = this.debug.Copy();
setting.balance = this.balance.Copy();
setting.LatencyEmulation = this.LatencyEmulation;
Expand Down
18 changes: 18 additions & 0 deletions MDPlayer/MDPlayerx64/form/SYS/frmMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using MDPlayerx64.Driver;
using Driver.libsidplayfp.sidtune;
using System.Runtime.CompilerServices;
using MDPlayerx64.MDServer;

namespace MDPlayer.form
{
Expand Down Expand Up @@ -131,6 +132,7 @@ public partial class frmMain : Form
private object remoteLockObj = new();
private bool remoteBusy = false;
private List<string[]> remoteReq = new();
private MDServer mdsrv = null;

private double speedRatio = 1.0;

Expand All @@ -146,6 +148,12 @@ public frmMain()
log.debug = this.setting.debug.logDebug;
log.logLevel = this.setting.debug.logLevel;

if (this.setting.network.useMDServer)
{
mdsrv = new MDServer(this.setting.network.port,Remote);
mdsrv.init();
}

frmConsole = new frmConsole(setting);
if (setting.debug.ShowConsole) frmConsole.Show();

Expand Down Expand Up @@ -582,6 +590,11 @@ private void Remote(string line)
TsmiPause_Click(null, null);
break;
case "CLOSE":
if (this.InvokeRequired)
{
this.Invoke(Close);
break;
}
Close();
break;
case "LOOP":
Expand Down Expand Up @@ -1178,6 +1191,11 @@ private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
log.ForcedWrite("終了処理開始");
log.ForcedWrite("frmMain_FormClosing:STEP 00");

if (mdsrv != null)
{
mdsrv.disConnect();
}

frmPlayList.Stop();
frmPlayList.Save();

Expand Down
Loading

0 comments on commit 64cd8fb

Please sign in to comment.